JButton无法在actionListener中解析

时间:2016-11-16 15:07:38

标签: java eclipse swing

我试图用Eclipse制作一个简单的GUI计算器 actionlistener位于底部,eclipse表示我的ButtonAdd无法解析 错误是在线125-131,任何帮助表示赞赏! :d

package main;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionListener;

public class GUIApp {

private JFrame frame;
public JTextField txtNumber_1;
public JTextField txtNumber;
public JTextField textField;
public JButton ButtonAdd;
public JButton ButtonSub;
public JButton ButtonMulti;
public JButton ButtonDiv;
public JButton btnRng;
String num1;
String num2;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUIApp window = new GUIApp();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public GUIApp() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 * @return 
 */
public void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    frame.getContentPane().setLayout(gridBagLayout);

    ButtonAdd = new JButton("Addition");
    ButtonAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            num1 = txtNumber_1.getText();
            num2 = txtNumber.getText();
           if(ButtonAdd.getModel().isPressed())
            textField.setText(num1 + num2);
        }
    });
    GridBagConstraints gbc_ButtonAdd = new GridBagConstraints();
    gbc_ButtonAdd.insets = new Insets(0, 0, 5, 5);
    gbc_ButtonAdd.gridx = 0;
    gbc_ButtonAdd.gridy = 0;
    frame.getContentPane().add(ButtonAdd, gbc_ButtonAdd);

    ButtonSub = new JButton("Subtraction");
    GridBagConstraints gbc_ButtonSub = new GridBagConstraints();
    gbc_ButtonSub.insets = new Insets(0, 0, 5, 5);
    gbc_ButtonSub.gridx = 0;
    gbc_ButtonSub.gridy = 2;
    frame.getContentPane().add(ButtonSub, gbc_ButtonSub);

    txtNumber_1 = new JTextField();
    txtNumber_1.setText("Number 1");
    txtNumber_1.setToolTipText("insert a number");
    GridBagConstraints gbc_txtNumber_1 = new GridBagConstraints();
    gbc_txtNumber_1.insets = new Insets(0, 0, 5, 0);
    gbc_txtNumber_1.fill = GridBagConstraints.HORIZONTAL;
    gbc_txtNumber_1.gridx = 3;
    gbc_txtNumber_1.gridy = 2;
    frame.getContentPane().add(txtNumber_1, gbc_txtNumber_1);
    txtNumber_1.setColumns(10);

    ButtonMulti = new JButton("Multiplication");
    GridBagConstraints gbc_ButtonMulti = new GridBagConstraints();
    gbc_ButtonMulti.insets = new Insets(0, 0, 5, 5);
    gbc_ButtonMulti.gridx = 0;
    gbc_ButtonMulti.gridy = 4;
    frame.getContentPane().add(ButtonMulti, gbc_ButtonMulti);

    txtNumber = new JTextField();
    txtNumber.setText("Number 2");
    txtNumber.setToolTipText("insert other number");
    GridBagConstraints gbc_txtNumber = new GridBagConstraints();
    gbc_txtNumber.insets = new Insets(0, 0, 5, 0);
    gbc_txtNumber.fill = GridBagConstraints.HORIZONTAL;
    gbc_txtNumber.gridx = 3;
    gbc_txtNumber.gridy = 5;
    frame.getContentPane().add(txtNumber, gbc_txtNumber);
    txtNumber.setColumns(10);

    ButtonDiv = new JButton("Division");
    GridBagConstraints gbc_ButtonDiv = new GridBagConstraints();
    gbc_ButtonDiv.insets = new Insets(0, 0, 5, 5);
    gbc_ButtonDiv.gridx = 0;
    gbc_ButtonDiv.gridy = 6;
    frame.getContentPane().add(ButtonDiv, gbc_ButtonDiv);

    btnRng = new JButton("RNG");
    GridBagConstraints gbc_btnRng = new GridBagConstraints();
    gbc_btnRng.insets = new Insets(0, 0, 0, 5);
    gbc_btnRng.gridx = 0;
    gbc_btnRng.gridy = 8;
    frame.getContentPane().add(btnRng, gbc_btnRng);

    textField = new JTextField();
    textField.setToolTipText("output");
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.gridx = 3;
    gbc_textField.gridy = 8;
    frame.getContentPane().add(textField, gbc_textField);
    textField.setColumns(10);
}
public void actionPerformed(ActionEvent arg0) {
    String num1;
    String num2;
    num1 = txtNumber_1.getText();
    num2 = txtNumber.getText();
   if(ButtonAdd.getModel().isPressed())
    textField.setText(num1 + num2);

}
}

GUI是用WindowBuilder编写的

2 个答案:

答案 0 :(得分:2)

ButtonAdd是本地引用,这意味着它只能在initialize方法中访问。解决方法是在方法外声明ButtonAdd

JButton ButtonAdd;

public void initialize() {
   ButtonAdd = new JButton("Addition");
}
public void actionPerformed(ActionEvent arg0) {
   // here we can now access ButtonAdd
}

同样是一个提示,根据java的标准命名约定,您通常使用小写字母开始变量和引用,因此ButtonAdd - > buttonAdd

答案 1 :(得分:1)

因为它不是成员变量。使它成为一个,它的工作原理

JButton ButtonAdd = new JButton("Addition");在另一个方法中,因此您的动作侦听器

不可见