import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Square extends JApplet{ int size = 40; public void init() { JButton butSmall = new JButton("Small"); JButton butMedium = new JButton("Medium"); JButton butLarge = new JButton("Large"); JButton butMessage = new JButton("Say Hi!"); SquarePanel panel = new SquarePanel(this); JPanel butPanel = new JPanel(); butPanel.add(butSmall); butPanel.add(butMedium); butPanel.add(butLarge); butPanel.add(butMessage); add(butPanel, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); butSmall.addActionListener(new ButtonHandler(this)); butMedium.addActionListener(new ButtonHandler(this)); butLarge.addActionListener(new ButtonHandler(this)); butMessage.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Hiiii"); } }); }}class SquarePanel extends JPanel { Square theApplet; SquarePanel(Square app) { theApplet = app; } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.black); g.fillRect(20, 20, theApplet.size, theApplet.size); }}class ButtonHandler implements ActionListener{ Square theApplet; ButtonHandler(Square a){ theApplet=a; } public void actionPerformed(ActionEvent e){ if(e.getSource()==theApplet.butSmall) theApplet.size=10; if(e.getSource()==theApplet.butMedium) theApplet.size=30; if(e.getSource()==theApplet.butLarge) theApplet.size=50; theApplet.repaint(); }}
- -我就想请问一下各位大神们3个按钮都有监听,但是最下面的actionPerformed方法里却不能识别这些按钮呢
- -我就想请问一下各位大神们3个按钮都有监听,但是最下面的actionPerformed方法里却不能识别这些按钮呢