如何处理JButtons的多个动作

时间:2015-10-14 14:15:18

标签: java jframe jpanel paintcomponent

最近我决定在Java中使用JFrames,JPanels等。正如主题所说,我不知道如何为不同的按钮设置不同的动作。为了练习,我想写一个小程序,按下不同的按钮时应该画出不同的形状。但是,不幸的是它不起作用 - 按下按钮后,不会执行绘画形状。有更有效的方法吗?有人可以指出错误的位置吗?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUI {

    public JFrame frame;
    public JPanel panel;
    public JButton button1, button2;
    public final static String COMMAND_FOR_BUTTON_1 = "COMMAND_FOR_BUTTON_1";
    public final static String COMMAND_FOR_BUTTON_2 = "COMMAND_FOR_BUTTON_2";
    public int whatToDraw = 0;
    public paintComponent pc;

    public void initFrame() {
        frame = new JFrame();
        frame.setTitle("Let's paint something");
        frame.setSize(300, 400);        
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void initPanel() {
        panel = new JPanel();
    }

    public void initButtons() {
        button1 = new JButton("Square");
        button1.setActionCommand(COMMAND_FOR_BUTTON_1);
        button2 = new JButton("Circle");
        button2.setActionCommand(COMMAND_FOR_BUTTON_2);

        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {    
                if(e.getActionCommand().equals(COMMAND_FOR_BUTTON_1)) {
                    whatToDraw = 1;
                    pc.repaint();
                }
            }
        });

        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand().equals(COMMAND_FOR_BUTTON_2)) {
                    whatToDraw = 2;
                    pc.repaint();
                }
            }
        });
    }

    @SuppressWarnings("serial")
    class paintComponent extends JComponent{
        public void paint(Graphics g){
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Random rand = new Random();
            int radius = rand.nextInt(80)+40;

            if(whatToDraw == 1) {
                g2.setColor(Color.BLUE);
                g2.fillOval(rand.nextInt(getWidth()-radius), rand.nextInt(getHeight())-radius, radius, radius);
            } 
            if(whatToDraw == 2) {
                g2.setColor(Color.GREEN);
                g2.fillRect(rand.nextInt(getWidth()-radius), rand.nextInt(getHeight())-radius, radius, radius);
            }
        }
    }

    public void createGUI() {
        initFrame();
        initPanel();
        initButtons();
        pc = new paintComponent();
        panel.add(button1);
        panel.add(button2);
        frame.add(pc, BorderLayout.CENTER);
        frame.add(panel, BorderLayout.PAGE_START);
    }

    public static void main(String[] args) {
        GUI gui = new GUI();
        gui.createGUI();
    }
}

1 个答案:

答案 0 :(得分:1)

 public void paint(Graphics g){
        initButtons(); <------------------- delete
        Graphics2D g2 = (Graphics2D) g;
        if(whatToDraw == 1) {
            g2.setColor(Color.BLUE);
            g2.fillOval(30, 30, 30, 30);
            repaint(); <------------------- delete
        } 
        if(whatToDraw == 2) {
            g2.setColor(Color.GREEN);
            g2.fillRect(30, 30, 30, 30);
            repaint(); <------------------- delete
        }
    }

    @Override
        public void actionPerformed(ActionEvent e) {    
            if(e.getActionCommand().equals(COMMAND_FOR_BUTTON_1)) {//FredK suggested in comments you remove this line.
                whatToDraw = 1;
                pc.repaint()
            }
        }
    });

    button2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            whatToDraw = 2;//removed it would like like this, and do the same. Your solution would be neccessary if 2 buttons shared the same actionListener.
            pc.repaint();
        }
    });

修改

public void createGUI() {
    initFrame();
    initPanel(); //put your initButtons() method here
    pc = new paintComponent();
    panel.add(button1); //these could go in initPanel() too
    panel.add(button2); //^
    frame.add(pc, BorderLayout.CENTER);//your JFrame has a default layout of BorderLayout.
    frame.add(panel, BorderLayout.PAGE_START);
}