java吧 关注:1,225,715贴子:12,684,679
  • 3回复贴,共1

啊~刚开始学GUI碰到了点问题有大神可以帮忙看看么~

只看楼主收藏回复

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方法里却不能识别这些按钮呢


IP属地:广东1楼2015-10-15 12:21回复
    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(); }}


    IP属地:广东2楼2015-10-15 12:28
    回复
      啊啊啊木有大神么~


      IP属地:广东3楼2015-10-15 17:25
      回复
        啊啊啊木有大神么~


        IP属地:广东4楼2015-10-17 09:08
        回复