2011-01-14 2 views
2

다른 단추의 actionListener가있는 단추에 actionListener를 만들려고하는데 어떤 방법으로도 그 단추를 찾아 낼 수 없습니다. 나는 두번째 버튼에 대해 행동을 취하려고 노력하고 있지만, 나는 그것을 이해할 수 없었다. 누군가가 나를 도와 준다면 고맙겠습니다!ActionListener 문제

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 
import java.io.*; 
import java.util.*; 

public class basic implements ActionListener{ 

    public static void main(String[] args) { 
     basic process = new basic(); 
    } 

    public basic(){ 



      JFrame fan = new JFrame("Scheme"); 


      JPanel one = new JPanel(new BorderLayout()); 
      fan.add(one); 

     JPanel uno = new JPanel(); 
      uno.setLayout(new BoxLayout(uno, BoxLayout.Y_AXIS)); 
      JButton addB = new JButton("first choice"); 


     addB.setAlignmentX(Component.CENTER_ALIGNMENT); 
      uno.add(addB); 

      addDButton.setActionCommand("hehe"); 
      addDButton.addActionListener(this); 

     one.add(uno,BorderLayout.CENTER); 

      fan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
      fan.setSize(500,700); 
      fan.setLocationByPlatform(true); 
      fan.setVisible(true); 
} 

      public void actionPerformed(ActionEvent evt) { 

    JPanel markP = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20)); 
    JDialog dialog = new JDialog((JFrame)null); 
    dialog.getContentPane().add(markP,BorderLayout.CENTER); 

    if (evt.getActionCommand().equals("hehe")) { 


    JLabel title = new JLabel("Proceed"); 
    title.setFont(new Font("Arial",Font.BOLD,15)); 
    markP.add(title,BorderLayout.NORTH); 



    JButton exit = new JButton("Exit"); 
    markP.add(exit); 


//here i want to create another actionListener on the exit button only without affecting the other content which is in the button "addB " so that when i click on the addB button the J dialog pops up, and than when i click on exit button the program will return to the menu.I couldn't figure it out. 



    dialog.toFront(); 
    dialog.setModal(true); 
    dialog.pack(); // 
    dialog.setLocationRelativeTo(null); // 
       dialog.setVisible(true); 

} 

// here the code goes on but the problem is that of the actionListener which is concerned. 

답변

0
JButton exit = new JButton("Exit"); 
exit.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent evt) { 
     // you code here 
    } 
}); 

당신은 더 나은 변수 이름을 사용한다 : 여기에 아래의 코드입니다. 당신의 코드

+0

감사를 사용하여 작업의 소스를 확인하는 경우는 같은 ActionListener를 사용할 수 있습니다. 그것은 효과가있다! – thegamer

0

따라야하는 것은 쉽지 않다 당신이 도움말 및 주석에 대한

if (evt.getSource().equals(addDButton) { original code } 
else { code for the other button } 
+0

도움을 주셔서 감사합니다! – thegamer