简体   繁体   中英

how to handle multiple actions by JButtons

recently I decided to play a bit with JFrames, JPanels etc. in Java. As topic says, I do not know how to set different actions to different buttons. In order to practice, I wanted to write a tiny program that supposed to draw different shape while pressing different button. But, unfortunately it does not work - after hitting the button, painting shapes is not performed. Is there more efficient way to do that? Could someone point where the error is?

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();
    }
}
 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();
        }
    });

EDIT

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM