简体   繁体   English

添加面板和框架后,paint() 不起作用

[英]paint() doesn't work after panel and frame was added

I am making kind of my paint that creates shapes, it worked fine until I added layers(panel and frame), now the shapes aren't visible on button press I assume it is under the layers?i tried using paintComponent and revalidate etc and still couldn't get it to appear我正在制作一种创建形状的油漆,它工作正常,直到我添加了图层(面板和框架),现在按下按钮时形状不可见我假设它在图层下面?我尝试使用paintComponent并重新验证等和仍然无法让它出现

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main2 extends JPanel implements ActionListener {

    private static Square mySquare;
    private static Circle myCircle;
    private static Color myColor;
    public boolean SquareCheck;
    public boolean CircleCheck;
    JButton buttonSquare;
    JButton buttonCircle;
    JFrame frame;
    JPanel panel;

    public void paint(Graphics g) {
        if (SquareCheck) {
            g.setColor(myColor);
            g.fillRect(mySquare.x, mySquare.y, mySquare.width, mySquare.length);
        } else if (CircleCheck) {
            g.setColor(myColor);
            g.fillOval(myCircle.x, myCircle.y, myCircle.width, myCircle.length);
        }
    }

    public void start() {
        frame = new JFrame();
        panel = new JPanel();

        //setLayout(new BorderLayout());

        buttonSquare = new JButton("■");
        buttonCircle = new JButton("●");

        buttonSquare.setPreferredSize(new Dimension(200, 20));
        buttonCircle.setPreferredSize(new Dimension(200, 20));

        buttonCircle.addActionListener(this);
        buttonSquare.addActionListener(this);
        //add(buttonCircle, BorderLayout.NORTH);
        //add(buttonSquare, BorderLayout.SOUTH);
        JToggleButton red = new JToggleButton();
        panel.add(buttonCircle);
        panel.add(buttonSquare);
        frame.add(panel);
        frame.setSize(500, 500);
        frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == buttonSquare) {
            SquareCheck = true;
        } else if (e.getSource() == buttonCircle) {
            CircleCheck = true;
        }
        repaint();

    }
    public static void main(String[] args) {
        mySquare = new Square(30, 50, 50, 50);
        myCircle = new Circle(60, 100, 50, 50);
        myColor = Color.red;
        Main2 x = new Main2();
        x.start();

    }
}

Basiclly the buttons changes the boolean then the repaint is called and based on the boolean it draws either a cirlce or a square,the code worked before adding frame and panel基本上按钮更改布尔值然后调用重绘并根据布尔值绘制圆形或正方形,代码在添加框架和面板之前工作

Your paint method is a method of the Main2 class, but you never add a Main2 instance to the JFrame or to any component that goes into the JFrame, and so the Main2 instance will never be displayed, and the Swing painting manager will never call its paint method.您的绘制方法是 Main2 类的一个方法,但您永远不会向 JFrame 或任何进入 JFrame 的组件添加 Main2 实例,因此永远不会显示 Main2 实例,并且 Swing 绘画管理器永远不会调用其涂装方法。

For starters, get rid of this variable, panel = new JPanel();首先,去掉这个变量, panel = new JPanel(); and every place you use panel , substitute this .和你使用panel每个地方,替换this This way you'll be working with a correct Main2 instance and adding it to the GUI.这样,您将使用正确的 Main2 实例并将其添加到 GUI。

Other issues:其他事宜:

  • You need to call the super's equivalent painting method in your override on its first line您需要在第一行的覆盖中调用超级的等效绘画方法
  • Override paintComponent, not paint, and yes call super.paintComponenet(g);覆盖paintComponent,而不是paint,并且是调用super.paintComponenet(g); in this override在这个覆盖
  • You will want to learn and use Java naming conventions .您将需要学习和使用Java 命名约定 Variable names should all begin with a lower letter while class names with an upper case letter.变量名应全部以小写字母开头,而类名应以大写字母开头。 Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.学习这一点并遵循这一点将使我们能够更好地理解您的代码,并使您更好地理解其他人的代码。
  • For safety's sake, add the @Override annotation above any method that you think may be overriding a parent method (such as paint), to make sure that you are doing it correctly.为了安全起见,在您认为可能覆盖父方法(例如paint)的任何方法上方添加@Override注释,以确保您正确执行此操作。

For example:例如:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class Main2 extends JPanel implements ActionListener {

    private static Square mySquare;
    private static Circle myCircle;
    private static Color myColor;
    private JToggleButton buttonSquare;
    private JToggleButton buttonCircle;
    JFrame frame;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (buttonSquare.isSelected()) {
            g.setColor(myColor);
            g.fillRect(mySquare.x, mySquare.y, mySquare.width, mySquare.length);
        } 
        if (buttonCircle.isSelected()) {
            g.setColor(myColor);
            g.fillOval(myCircle.x, myCircle.y, myCircle.width, myCircle.length);
        }
    }

    public Main2() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buttonSquare = new JToggleButton("■");
        buttonCircle = new JToggleButton("●");

        buttonCircle.addActionListener(this);
        buttonSquare.addActionListener(this);

        this.add(buttonCircle);
        this.add(buttonSquare);
        frame.add(this);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    public static void main(String[] args) {
        mySquare = new Square(30, 50, 50, 50);
        myCircle = new Circle(60, 100, 50, 50);
        myColor = Color.red;
        new Main2();
    }
}

class MyShape {
    public int x, y, width, length;

    public MyShape(int x, int y, int width, int length) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.length = length;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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