2013-05-03 5 views
1

여기에 문제가 생겼습니다. 배경 이미지를 추가 할 때까지 내 버튼이 잘 작동하고 있었고, 마우스를 버튼과 텍스트 필드 위에 올려 놓으려고 할 때 표시되었습니다. 내 레이블은 내 마우스가 클릭해도 관계없이 작동하지 않습니다. 코드를 클래스에 heres합니다. (INT 아이디)왜 마우스를 버튼 위에 올리면 마우스가 나타나야합니까?

public class launcher extends JFrame{ 
    private static final long serialVersionUID = 1L; 

    protected JPanel window = new JPanel(); 
    protected JFrame f = new JFrame("stackoverflow"); 

    private Rectangle rAncient, rMedieval, rModern, rFuture, rFinancial, rUpdate; 
    private JButton ancient, medieval, modern, future, financial, update; 
    private JLabel time, name; 
    private JTextField tName; 


    protected int width = 600; 
    protected int height = 400; 
    protected int button_width = 80; 
    protected int button_height = 40; 


    public launcher(int id) throws IOException{ 
     try{ 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
     setTitle("Choose your Path"); 
     setSize(new Dimension(width, height)); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     getContentPane().add(window); 
     setLocationRelativeTo(null); 
     setResizable(false); 
     setVisible(true); 
     window.setLayout(null); 
     if(id==0){ 
     drawButtons(); 
    } 
     repaint(); 
    } 
    private void drawButtons(){ 
     ancient = new JButton("Ancient"); 
     rAncient = new Rectangle(140, 150, 70, 40); 
     ancient.setBounds(rAncient); 
     window.add(ancient); 

     medieval = new JButton("Medieval"); 
     rMedieval = new Rectangle(220, 150, 80, 40); 
     medieval.setBounds(rMedieval); 
     window.add(medieval); 

     modern = new JButton("Modern"); 
     rModern = new Rectangle(310, 150, 70, 40); 
     modern.setBounds(rModern); 
     window.add(modern); 

     future = new JButton("Future"); 
     rFuture = new Rectangle(350, 150, 70, 40); 
     future.setBounds(rFuture); 
     window.add(future); 

     financial = new JButton("Financial"); 
     rFinancial = new Rectangle(390, 150, 70, 40); 
     future.setBounds(rFinancial); 
     window.add(financial); 

     update = new JButton("Update"); 
     rUpdate = new Rectangle(250, 300, 100, 50); 
     update.setBounds(rUpdate); 
     window.add(update); 

     time = new JLabel("Choose your desired time period"); 
     time.setBounds(220, 90, 200, 50); 
     window.add(time); 

     name = new JLabel("Name: "); 
     name.setBounds(210, 220, 200, 50); 
     window.add(name); 

     tName = new JTextField(); 
     tName.setBounds(250, 235, 150, 20); 
     window.add(tName); 
     tName.setText("Bob"); 

      ancient.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent a){ 
       System.out.println("Starting in Ancient mode..."); 
      } 
      }); 

      medieval.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent a){ 
       System.out.println("Starting in Medieval mode..."); 
      } 
      }); 

      modern.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent a){ 
       System.out.println("Starting in Modern mode..."); 
      } 
      }); 

      future.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent a){ 
       System.out.println("Starting in Future mode..."); 
      } 
      }); 

      financial.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent a){ 
       System.out.println("Starting in Financial mode..."); 
      } 
      }); 

      update.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent a){ 
       System.out.println("Opening browser to http://ubergamesproductions.weebly.com/adventure"); 
       try{ 
        String URL ="http://ubergamesproductions.weebly.com/adventure"; 
        java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL)); 
       }catch(Exception e){ 
        JOptionPane.showMessageDialog(null, e.getMessage()); 
       } 
      } 
      }); 
    } 
    public static void main(String[] args)throws IOException{ 
    new launcher(0); 
    } 
     private Image backgroundImage = ImageIO.read(new File("C:/Users/Samuels Laptop/Dropbox/Java/Questor/Launcher_UGP.png")); 
     public void paint(Graphics g) { 
     super.paint(g); 
     g.drawImage(backgroundImage, 0, 0, null); 
     } 
}; 

답변

3

공공 실행기는 {

사용 표준 자바 명명 규칙 IOException이 발생합니다. 클래스 이름은 대문자로 시작해야합니다.

window.setLayout(null); 
... 
ancient = new JButton("Ancient"); 
rAncient = new Rectangle(140, 150, 70, 40); 
ancient.setBounds(rAncient); 

null 레이아웃과 setBounds (...)를 사용하지 마십시오. 적절한 레이아웃 매니저를 사용하고 레이아웃 매니저가 그 일을하도록하십시오. 이 경우 JPanel과 기본 FlowLayout을 사용할 수 있습니다.

public void paint(Graphics g) { 
    super.paint(g); 
    g.drawImage(backgroundImage, 0, 0, null); 

최상위 컨테이너의 paint() 메서드를 재정의하지 마십시오. 귀하의 경우 귀하의 코드는 프레임과 모든 구성 요소를 그립니다. 그런 다음 이미지를 그립니다. 따라서 이미지는 구성 요소를 다룹니다. 이 버튼은 mouseEntered 이벤트에 응답하여 국경을 다시 칠()하여 갑자기 왜 나타나는지 보여줍니다.

대신 사용자 지정 그리기는 JPanel의 paintComponent (...) 메서드를 재정 의하여 수행됩니다. 그런 다음 프레임에 패널을 추가하고 패널에 구성 요소를 추가합니다. 자세한 내용은 Custom Painting에있는 스윙 튜토리얼의 섹션을 읽어보십시오. 시간을내어 전체 자습서를 읽으십시오. Layout Managers에 대한 섹션도 있습니다.

+1

'JFrame'은 'ImageObserver'입니다. –

+0

@AndrewThompson, 어떻게 질문이나 답변에 영향을 줍니까? ImageObserver는 이미지가 완전히로드 될 때 구성 요소가 다시 칠하는 것을 의미한다고 생각했습니다. 나는 그것이 여기의 문제라고 생각하지 않는다. – camickr

+0

나는 그렇게 생각하지 않는다. OTOH,'ImageObserver'를 가지고 있다면 왜 비동기 적으로로드 된 이미지를 가진 클래스를 사용하는 누군가를 기대하면서 그것을 사용하지 않을까요? 마치 더 이상 타이핑하지 않는 것입니다. –

관련 문제