import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class myAction extends JFrame implements ActionListener {
myAction() {
super("Lab 4 Question 1");
Container c = getContentPane();
JPanel p = new JPanel();
JPanel p1 = new JPanel();
JButton b = new JButton("Button 1");
JButton b1 = new JButton("Button 2");
JButton b2 = new JButton("Button 3");
Font newFont = new Font("SERIF", Font.BOLD, 16);
b1.setFont(newFont);
b1.addActionListener(this);
JLabel l = new JLabel("Hello.");
p.add(b);
p.add(b1);
p.add(b2);
p1.add(l);
c.add(p);
c.add(p1);
c.setLayout(new FlowLayout());
setSize(300, 300);
show();
}
public static void actionPerformed (ActionEvent e) {
if(e.getSource().equals(b))
{
this.l.setText("Testing");
}
}
public static void main(String[] args) {
myAction output = new myAction();
}
}
如何让我的 JButton b1 更改我的 JLabel l 的值?我对编程很陌生,所以对于你们注意到的任何错误,我深表歉意!每当我单击按钮时,我只需要更改它,我认为我做对了,但找不到我的符号,我很确定我不应该将它们传递给方法:S