如何在点击按钮时重新绘制 JPanel?



我是荡秋千的新手,目前正在尝试制作绘图程序。这就是我目前所拥有的。

这是主要类:

import java.util.*;
import static java.lang.System.*;
import java.awt.*;
import javax.swing.*;
public class DrawingBoard {
static JPanel buttonPanel;
static JPanel drawingPanel;
public static void main(String[] args) {
JFrame window = new JFrame();
window.setSize(500, 500);
window.setTitle("Drawing Board");
window.getContentPane().setBackground(Color.black);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new FlowLayout());
buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.setPreferredSize(new Dimension(80, 500));
buttonPanel.setBackground(Color.lightGray);
DrawingComponent drawingComp = new DrawingComponent();
drawingComp.setButtonPanel();
window.getContentPane().add(buttonPanel);
drawingPanel = new JPanel();
drawingPanel.setPreferredSize(new Dimension(420, 500));
drawingPanel.setBackground(Color.white);
drawingPanel.add(drawingComp);
window.getContentPane().add(drawingPanel);
window.pack();
window.setVisible(true);
}
}

这是绘图类:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import static java.lang.System.*;
public class DrawingComponent extends JComponent implements ActionListener {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
super.paintComponents(g);
g.setColor(Color.red);
g.fillRect(50, 50, 150, 150);
}
public  void setButtonPanel() {
JLabel colors = new JLabel("Colors", SwingConstants.CENTER);
DrawingBoard.buttonPanel.add(colors);
JButton red = new JButton("Red");
DrawingBoard.buttonPanel.add(red);
red.addActionListener(this);
JButton blue = new JButton("Blue");
DrawingBoard.buttonPanel.add(blue);
blue.addActionListener(this);
JButton green = new JButton("Green");
DrawingBoard.buttonPanel.add(green);
green.addActionListener(this);
JButton yellow = new JButton("Yellow");
DrawingBoard.buttonPanel.add(yellow);
yellow.addActionListener(this);
JButton orange = new JButton("Orange");
DrawingBoard.buttonPanel.add(orange);
orange.addActionListener(this);
JButton pink = new JButton("Pink");
DrawingBoard.buttonPanel.add(pink);
pink.addActionListener(this);
JButton purple = new JButton("Purple");
DrawingBoard.buttonPanel.add(purple);
purple.addActionListener(this);
JButton black = new JButton("Black");
DrawingBoard.buttonPanel.add(black);
black.addActionListener(this);
JLabel tools = new JLabel("Tools", SwingConstants.CENTER);
DrawingBoard.buttonPanel.add(tools);
JButton erase = new JButton("Erase");
DrawingBoard.buttonPanel.add(erase);
erase.addActionListener(this);
JButton drawLine = new JButton("Lines");
DrawingBoard.buttonPanel.add(drawLine);
drawLine.addActionListener(this);
JButton clear = new JButton("Clear");
DrawingBoard.buttonPanel.add(clear);
clear.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Red")) {
out.println("red");
repaint();
}
else if(e.getActionCommand().equals("Blue")) {
out.println("blue");
repaint();
}
else if(e.getActionCommand().equals("Green")) {
out.println("green");
repaint();
}
else if(e.getActionCommand().equals("Yellow")) {
out.println("yellow");
repaint();
}
else if(e.getActionCommand().equals("Orange")) {
out.println("orange");
repaint();
}
else if(e.getActionCommand().equals("Pink")) {
out.println("pink");
repaint();
}
else if(e.getActionCommand().equals("Purple")) {
out.println("purple");
repaint();
}
else if(e.getActionCommand().equals("Black")) {
out.println("black");
repaint();
}
else if(e.getActionCommand().equals("Erase")) {
out.println("erase");
repaint();
}
else if(e.getActionCommand().equals("Lines")) {
out.println("lines");
repaint();
}
else if(e.getActionCommand().equals("Clear")) {
out.println("clear");
repaint();
}
}
}

这是输出图片的链接。 https://i.stack.imgur.com/GEwga.png

现在,我只希望绘图板在单击按钮时显示某种图形。其他一切都很好,但是当我单击任何按钮时,绘图面板(白色区域)中没有像预期的那样显示图形。按钮的 println 语句仍然被执行,但 repaint() 只是直接不起作用。如何使当我单击调用paintComponent方法的按钮并在绘图面板上显示图形时,该图形是窗口上的白色区域?我已经在这个问题上停留了一段时间,所以帮助将不胜感激。

drawingPanel = new JPanel();更改为'drawingPanel = new JPanel(new BorderLayout());

DrawingComponent的默认首选大小为0x0,因此当您将其添加到JPanel(默认情况下使用FlowLayout)时,它的大小会变为0x0,因此您不会看到任何绘制的内容

然后改变...

public void paint(Graphics g) {
super.paintComponents(g);
g.setColor(Color.red);
g.fillRect(50, 50, 150, 150);
}

自。。。

@Override
protected void paintComponent(Graphics g) {
System.out.println("Hello");
super.paintComponent(g); 
g.setColor(Color.red);
g.fillRect(10, 10, 150, 150);
}

你的代码还有很多其他的东西让我害怕,但我没有时间解决这一切

最新更新