import java.awt.Button; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class Test014 { public static void main(String[] args){ JFrame f = new JFrame("test"); Container con = f.getContentPane(); con.setLayout(new FlowLayout()); JButton b1 = new JButton("+"); JButton b2 = new JButton("-"); final JLabel label = new JLabel(""); con.add(b1); con.add(b2); con.add(label); f.setSize(400,200); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText("你按了 + "); } }); b2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText("你按了 - "); } }); } }
Java 编写两个监听器(java GUI)
最新推荐文章于 2024-08-02 18:22:25 发布