Java paint()不';添加面板和框架后的t工作
我正在制作一种可以创建形状的绘画,在我添加图层(面板和框架)之前效果很好,现在这些形状在按下按钮时不可见,我假设它在图层下?我尝试使用paintComponent和revalidate等,但仍然无法使其显示Java paint()不';添加面板和框架后的t工作,java,swing,Java,Swing,我正在制作一种可以创建形状的绘画,在我添加图层(面板和框架)之前效果很好,现在这些形状在按下按钮时不可见,我假设它在图层下?我尝试使用paintComponent和revalidate等,但仍然无法使其显示 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Main2 extends JPan
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();
}
}
基本上,按钮会更改布尔值,然后调用重新绘制,并基于布尔值绘制圆环或正方形,在添加框架和面板之前执行的代码您的绘制方法是Main2类的方法,但您从未向JFrame或进入JFrame的任何组件添加Main2实例,因此,Main2实例将永远不会显示,Swing绘制管理器也永远不会调用其绘制方法 首先,去掉这个变量,
panel=newjpanel()
和您使用的每个面板
,替换此
。这样,您将使用正确的Main2实例并将其添加到GUI中
其他问题:
- 您需要在覆盖的第一行调用super的等效绘制方法
- 覆盖paintComponent,而不是paint,并且是调用
super.paintComponenet(g)代码>在此覆盖中
- 你会想要学习和使用。变量名都应该以小写字母开头,而类名应该以大写字母开头。学习这一点并遵循这一点可以让我们更好地理解您的代码,也可以让您更好地理解其他人的代码
- 为了安全起见,请在您认为可能重写父方法(如paint)的任何方法上方添加
注释,以确保正确执行@Override
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;
}
}