gpt4 book ai didi

java - 内部类事件处理程序无法访问顶级类字段。为什么?

转载 作者:行者123 更新时间:2023-12-01 19:17:49 25 4
gpt4 key购买 nike

这是一个非常琐碎的问题。但 Eclipse IDE 告诉我它无法将 button1 解析为任何变量。不知道为什么会发生这种情况。我是否正确使用了e.getSource()?这是代码:

package SimpleCRUD;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;

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

@SuppressWarnings("serial")
public class CRUDFrame extends JFrame {
public CRUDFrame(){
super("AppCRUD");
setLayout(new GridLayout(0,1,4,15));
//setTitle("Hello");
JLabel label1;
JButton button1, button2, button3, button4;
label1 = new JLabel(" Telephone Book" , JLabel.CENTER);
label1.setFont (new Font("fallan", 1, 25));
label1.setToolTipText("What?");
button1 = new JButton(" Show Contacts ");
button2 = new JButton(" Add Contact ");
button3 = new JButton(" Update Number in a Contact ");
button4 = new JButton(" Delete a Contact ");

add(label1);
add(button1);
add(button2);
add(button3);
add(button4);
ButtonListener onClick = new ButtonListener();
button1.addActionListener(onClick);
button2.addActionListener(onClick);
button3.addActionListener(onClick);
button4.addActionListener(onClick);

}


class ButtonListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(CRUDFrame.this, e.getActionCommand(), "Event fired", JOptionPane.PLAIN_MESSAGE);
if(e.getSource() == button1)
System.out.println("HI!");
}
}

}

最佳答案

您在构造函数中而不是在类主体中声明 button1

因此它仅在构造函数执行期间可用。

简单修复:

package SimpleCRUD;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;

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

@SuppressWarnings("serial")
public class CRUDFrame extends JFrame {

// now it's not in constructor
JButton button1, button2, button3, button4;

public CRUDFrame(){
super("AppCRUD");
setLayout(new GridLayout(0,1,4,15));
//setTitle("Hello");
JLabel label1;

label1 = new JLabel(" Telephone Book" , JLabel.CENTER);
label1.setFont (new Font("fallan", 1, 25));
label1.setToolTipText("What?");
button1 = new JButton(" Show Contacts ");
button2 = new JButton(" Add Contact ");
button3 = new JButton(" Update Number in a Contact ");
button4 = new JButton(" Delete a Contact ");

add(label1);
add(button1);
add(button2);
add(button3);
add(button4);
ButtonListener onClick = new ButtonListener();
button1.addActionListener(onClick);
button2.addActionListener(onClick);
button3.addActionListener(onClick);
button4.addActionListener(onClick);

}


class ButtonListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(CRUDFrame.this, e.getActionCommand(), "Event fired", JOptionPane.PLAIN_MESSAGE);
if(e.getSource() == button1)
System.out.println("HI!");
}
}

}

关于java - 内部类事件处理程序无法访问顶级类字段。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5807462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com