2014-05-15 4 views
-2

내 자바 할당을하고 있는데 GUI와 같은 버튼을 GUI와 같은 클래스로 처리 할 수 ​​있습니다. GUI 파일의 모든 버튼을 정의했으며 액션을 추가하고 싶습니다. Listener 버튼을하지만 난 아래의 코드는, 액션 리스너 조치가 내부에 내가 그것을 추가 버튼 액션 리스너 클래스의 액션 청취자의 조치를 호출 버튼을 클릭 할 때 의미하는 버튼 액션 리스너 클래스를 추가 할자바에서 다른 클래스로 전달하는 변수

public static void mainLayout(){ 
    JPanel buttonPane = new JPanel(); 

    buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT)); 

    JToolBar toolbar = new JToolBar("Applications"); 

    JButton addplayer = new JButton ("AddPlayer"); 
    JButton placebet = new JButton("Place Bet"); 

    buttonPane.add(addplayer); 
    buttonPane.add(placebet); 

    frame.add(buttonPane, BorderLayout.PAGE_START); 
    frame.setSize(500,200); 
    frame.setVisible(true); 
} 

와 액션 리스너 클래스

public class addButtonActionListener { 

    public static void addPlayer() { 
     addplayer.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
      System.out.println("Add Success!!"); 
     } 
    }); 

    placebet.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("Place Bet Success!!"); 
     } 
    }); 
} 
+5

안녕하세요, Edmond Lee, 귀하의 궁금한 점은 무엇입니까? – DerMike

+0

DerMike 안녕하세요, 제 문제는 GUI 클래스에서 JButton을 클릭했을 때 두 번째 코드 묶음 인 addButtonActionListener 클래스에서 Action Listener를 호출하려고합니다. –

답변

0

명명 규칙을 따르면 코드를 훨씬 쉽게 읽을 수 있습니다. 둘째 : actionlistener를 만들려는 시도가 잘못되었습니다. 그 클래스는 actionListener를 구현하고 여러분이 제공하는 actionPerformed 메소드 내에 비지니스 로직을 가져야한다.

버튼에 추가 할 수 있습니다.

관련 문제