简体   繁体   English

无法在actionListener中解析JButton

[英]JButton can't be resolved inside actionListener

I'm trying to make a simple GUI calculator with Eclipse The actionlistener is at the bottom, and eclipse says my ButtonAdd can't be resolved the error is around lines 125-131, Any help is appreciated! 我正在尝试使用Eclipse创建一个简单的GUI计算器。actionlistener位于底部,并且eclipse说我的ButtonAdd无法解决,错误在125-131行附近,我们将为您提供任何帮助! :D :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);

}
}

The GUI was made with WindowBuilder by the way GUI是通过WindowBuilder制作的

ButtonAdd is a local reference, meaning it can only be accessed inside the initialize method. ButtonAdd是本地引用,这意味着只能在initialize方法内部访问它。 A solution to this is to declare the ButtonAdd outside the method. 解决方案是在方法外部声明ButtonAdd

JButton ButtonAdd;

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

Also a tip, by java's standard naming conventions you usually start variables and references with a lower case letter, so ButtonAdd -> buttonAdd 另外,根据Java的标准命名约定,您通常以小写字母开头的变量和引用,因此ButtonAdd > buttonAdd

because it is not a member variable. 因为它不是成员变量。 make it one and it works 使其合而为一

JButton ButtonAdd = new JButton("Addition"); is inside another method therefore it is not visible to your action listener 在另一个方法中,因此它对操作侦听器不可见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM