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

时间:2017-07-14 06:49:42

标签: java swing

我是新手,我正在尝试制作绘图程序。这是我到目前为止所做的。

这是主要课程:

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

现在,我只想让绘图板在点击按钮时显示某种图形。其他一切都很好,但是当我点击任何按钮时,drawingPanel(白色区域)中没有任何图形显示出来。按钮的println语句仍然会被执行,但repaint()只是直接运行不起作用。我如何制作它,以便当我单击一个按钮时,调用paintComponent方法并在drawingPanel上显示图形,这是窗口上的白色区域?我一直坚持这个问题很长一段时间,所以非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

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

关于你的代码有很多其他的东西让我害怕,但我没有时间解决这个问题