gpt4 book ai didi

java - 如何通过 JButton 处理多个操作

转载 作者:行者123 更新时间:2023-12-01 11:04:03 25 4
gpt4 key购买 nike

最近我决定尝试一下 Java 中的 JFrame、JPanel 等。正如主题所说,我不知道如何为不同的按钮设置不同的操作。为了练习,我想编写一个小程序,该程序应该在按下不同按钮时绘制不同的形状。但是,不幸的是它不起作用 - 点击按钮后,不会执行绘制形状。有更有效的方法吗?有人能指出错误在哪里吗?

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

编辑

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

关于java - 如何通过 JButton 处理多个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33127791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com