2014-04-11 3 views
2

ActionListener이 버튼을 사용하여 호출 할 때 어떤 작업에서도 실행되지 않는 코드에 문제가 있습니다. 전에이 작업을 수행 한 적이 있지만 이것은 처음으로 GridBagLayout을 시도한 것으로 올바르게 작동하지 않는 것 같습니다. GUI는 완벽하게 작동합니다. ActionListener을 작동 시키려면 어떻게해야합니까? 다음은 전체 수업입니다.JFrames의 ActionListener

package body; 

import java.awt.ComponentOrientation; 
import java.awt.Container; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class mainFrame extends JFrame implements ActionListener{ 

    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 

    static String string = "default"; 

    static JButton one, two, three, four, five, six, seven, eight, nine, zero; 
    static JButton plus, minus, multiply, divide, equals; 

    public void init() { 
     one.setActionCommand("one"); 
     one.addActionListener(this); 

     two.setActionCommand("two"); 
     two.addActionListener(this); 

     three.setActionCommand("three"); 
     three.addActionListener(this); 

     four.setActionCommand("four"); 
     four.addActionListener(this); 

     five.setActionCommand("five"); 
     five.addActionListener(this); 

     six.setActionCommand("six"); 
     six.addActionListener(this); 

     seven.setActionCommand("seven"); 
     seven.addActionListener(this); 

     eight.setActionCommand("eight"); 
     eight.addActionListener(this); 

     nine.setActionCommand("nine"); 
     nine.addActionListener(this); 

     zero.setActionCommand("zero"); 
     zero.addActionListener(this); 

     plus.setActionCommand("plus"); 
     plus.addActionListener(this); 

     minus.setActionCommand("minus"); 
     minus.addActionListener(this); 

     multiply.setActionCommand("multiply"); 
     multiply.addActionListener(this); 

     divide.setActionCommand("divide"); 
     divide.addActionListener(this); 

     equals.setActionCommand("equals"); 
     equals.addActionListener(this); 

    } 

    public static void addComponentsToPane(Container pane) { 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 

     pane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     if (shouldFill) { 
      c.fill = GridBagConstraints.HORIZONTAL; 
     } 

     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 

     JLabel display = new JLabel(string); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 0; 
     pane.add(display, c); 

     one = new JButton("1"); 
     c.weightx = 0.5; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 1; 
     pane.add(one, c); 

     two = new JButton("2"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 1; 
     c.gridy = 1; 
     pane.add(two, c); 

     three = new JButton("3"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 2; 
     c.gridy = 1; 
     pane.add(three, c); 

     plus = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 3; 
     c.gridy = 1; 
     pane.add(plus, c); 

     four = new JButton("4"); 
     c.weightx = 0.5; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(four, c); 

     five = new JButton("5"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 1; 
     c.gridy = 2; 
     pane.add(five, c); 

     six = new JButton("6"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 2; 
     c.gridy = 2; 
     pane.add(six, c); 

     minus = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 3; 
     c.gridy = 2; 
     pane.add(minus, c); 

     seven = new JButton("7"); 
     c.weightx = 0.5; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 3; 
     pane.add(seven, c); 

     eight = new JButton("8"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 1; 
     c.gridy = 3; 
     pane.add(eight, c); 

     nine = new JButton("9"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 2; 
     c.gridy = 3; 
     pane.add(nine, c); 

     multiply = new JButton("*"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 3; 
     c.gridy = 3; 
     pane.add(multiply, c); 

     zero = new JButton("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 4; 
     pane.add(zero, c); 

     equals = new JButton("Equals"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weighty = 1.0; 
     c.gridx = 1; 
     c.gridwidth = 2; 
     c.gridy = 4; 
     pane.add(equals, c); 

     divide = new JButton("/"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.anchor = GridBagConstraints.PAGE_END; 
     c.weightx = 0.5; 
     c.gridx = 3; 
     c.gridy = 4; 
     pane.add(divide, c); 
    } 

    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("Calculator"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 

     addComponentsToPane(frame.getContentPane()); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (e.getActionCommand() == "one") { 
      System.out.println("hello"); 
     } 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

[자바에서 문자열을 비교하는 방법] (http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)을 읽은 적이 있으십니까? 그것을 줘 봐 ;-) –

+0

좋은 생각 나는 전에 그것을 보지 못했다. 나는 당신이 actionPerformed 메소드에서 그것을 사용한다는 것을 의미한다고 생각하기 때문에 나는 그것을 시험해 보았고 불행히도 변경을 시도하지 않았다. 어쨌든 고마워. –

+0

'평등'을 사용하려고 했습니까? –

답변

1
== 게다가

equals은 당신이 결코 당신의 행동 명령을 초기화 init()를 호출하지 지적했다.

클래스는 이미 JFrame입니다. JFrame 클래스를 사용하거나 JFrame 인스턴스를 사용하십시오.

init() 메서드를 제대로 호출 할 수 있도록 위의 문제를 해결해야합니다.

private static void createAndShowGUI() { 
    mainFrame frame = new mainFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 
    addComponentsToPane(frame.getContentPane()); 
    frame.init();   <<<<<================== Don't forget about meeeee! 
    frame.pack(); 
    frame.setVisible(true); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    if ("one".equals(e.getActionCommand())) { 
     System.out.println("hello"); 
    } 
} 

또한 당신이 다음해야 자바 명명 규칙을보십시오. 클래스 이름은 대문자로 시작합니다. 그래서 mainFrameMainFrame

+0

귀하는 선생님이며 학자입니다. –