2013-09-02 1 views
1

JButton을 눌렀을 때 텍스트를 표시하는 응용 프로그램을 만들려고하지만 어떻게 작성 될지 모르겠다. 여기에 내 코드가있다. 누군가 응원하는 데 도움이되는 응용 프로그램입니다. 그게 도움이된다면 Eclipse를 사용하고 있습니다.JButton을 눌렀을 때 텍스트가 나타나게하는 방법

public LoveYou() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(200, 200, 900, 600); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(200, 200, 200, 200)); 
    contentPane.setLayout(new BorderLayout(5, 5)); 
    setContentPane(contentPane); 

    JButton btnNewButton = new JButton("Feeling down? Press me."); 
    btnNewButton.setActionCommand("enter"); 
    btnNewButton.addActionListener((ActionListener) this); 
    btnNewButton.setBackground(Color.green); 
    btnNewButton.setForeground(new Color(30, 144, 255)); 
    contentPane.add(btnNewButton, BorderLayout.CENTER); 

    public void onActionPerformed(ActionEvent e; { 
     boolean isClicked = false; 
     if(!isClicked){ 
      isClicked = true; 
      System.out.println("Someone loves YOU!"); 
      } 
     else { 
      System.out.println(""); 
    } 
    } 
}  

답변

0

btnNewButton.addActionListener((ActionListener) this);를 제거하고이 시도 :

btnNewButton.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      boolean isClicked = false; 
      if (!isClicked) { 
       isClicked = true; 
       System.out.println("Someone loves YOU!"); 
      } else { 
       System.out.println(""); 
      } 

     } 
    }); 
관련 문제