如果您回答,请张贴示例代码。我的代码是:
package Program;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Screen extends JFrame {
public Screen() {
setVisible(true);
setSize(600,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setAlwaysOnTop(true);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setLayout(null);
JLabel lbl = new JLabel();
lbl.setForeground(Color.green);
lbl.setBounds(300,100,60,60);
JButton btn1 = new JButton();
btn1.setText("Click");
btn1.setBackground(Color.MAGENTA);
btn1.setBounds(200,200,70,50);
btn1.addActionListener();
panel.add(btn1);
panel.add(lbl);
add(panel);
setContentPane(panel);
}
ActionListener kk = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lbl.setText("blablabla, I know its local, but I don´t know how to fix it");
}
};
}
在我的Main方法/类中,我创建了一个这个类的对象,请帮助。我已经说过我知道它是本地的,但我不知道如何修复它。
您没有链接ActionListener
对象和按钮。试试这个:
package Program;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Screen extends JFrame {
public Screen() {
setVisible(true);
setSize(600,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setAlwaysOnTop(true);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setLayout(null);
JLabel lbl = new JLabel();
lbl.setForeground(Color.green);
lbl.setBounds(300,100,60,60);
JButton btn1 = new JButton();
btn1.setText("Click");
btn1.setBackground(Color.MAGENTA);
btn1.setBounds(200,200,70,50);
ActionListener kk = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lbl.setText("blablabla, I know its local, but I don´t know how
to fix it");
}
};
btn1.addActionListener(kk);
panel.add(btn1);
panel.add(lbl);
add(panel);
setContentPane(panel);
}
}
:不久或
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lbl.setText("blablabla, I know its local, but I don´t know how
to fix it");
}
});