gpt4 book ai didi

java - repaint() 方法未按预期运行

转载 作者:行者123 更新时间:2023-11-29 03:22:48 25 4
gpt4 key购买 nike

我有 2 个按钮,一个重置按钮和一个计算按钮。
重置按钮的唯一目的是重新绘制名为 p1JPanel
计算按钮的用途是进行计算并更新 JLabel

问题是,当按下重置按钮时,接着是计算按钮JPanel 被重新绘制,但它不应该被重新绘制(请参阅下面的重新绘制代码calculateButtonActionListener() 中不存在方法。

我想知道为什么会发生这种情况,以及在按下此按钮时我能做些什么来停止重绘 JPanel(重置按钮的功能完全符合预期,通过重新绘制面板)。

public class DrawCircles extends JFrame {


//the following are x and y locations of the centers of the circles
int center1X;
int center1Y;
int center2X;
int center2Y;
int center3X;
int center3Y;

public DrawCircles(){


final CircleDraw c = new CircleDraw(); //create a circledraw object to get the area of the triangle between them

final JPanel p1 = new JPanel(); //first panel to hold all other panels making the layout
JPanel buttonPanel = new JPanel();

p1.setLayout(new BorderLayout()); //set the layout of the panel to a border layout

JButton areaTriangle = new JButton("Calculate area of triangle");
JButton perimeterTriangle = new JButton("Calculate perimeter of triangle");
JButton reset = new JButton("Reset");

buttonPanel.setLayout(new BoxLayout(buttonPanel,0));
buttonPanel.add(areaTriangle);
buttonPanel.add(Box.createRigidArea(new Dimension(15,0)));
buttonPanel.add(perimeterTriangle);
buttonPanel.add(Box.createRigidArea(new Dimension(15,0)));
buttonPanel.add(reset); //add a button that says reset

reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
p1.repaint(); //redraw the circles and triangle
areaLabel.setText(""); //clear the label

}
});


calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {

areaLabel.setText("Area is "+ String.valueOf(2.0*c.getArea()));

}
});

resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
areaLabel.setText("");
}
});

add(p1);


}


public class CircleDraw extends JPanel {

int radius;
double s;
double area;

public CircleDraw(){

}

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);


Random rand = new Random(System.currentTimeMillis());
center1X=rand.nextInt(507);
center1Y=rand.nextInt(320);
center2X=rand.nextInt(507);
center2Y=rand.nextInt(320);
center3X=rand.nextInt(507);
center3Y=rand.nextInt(320);

//draw the 3 circles
g.drawOval(center1X, center1Y, 100,100);
g.drawOval(center2X, center2Y,100,100);
g.drawOval(center3X, center3Y, 100, 100);

//connect the centers of the circles with lines
g.drawLine(center1X+50, center1Y+50, center2X+50, center2Y+50);
g.drawLine(center2X+50, center2Y+50, center3X+50, center3Y+50);
g.drawLine(center3X+50, center3Y+50, center1X+50, center1Y+50);


}


}
public static void main(String[] args) {
DrawCircles frame = new DrawCircles();
frame.setSize(700,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);


}


}

最佳答案

你声明:

The issue is that when the reset button is pressed, followed by the calculate button the panel is repainted and it should not be. (See code below where the repaint method is not present in the ActionListener() for calculateButton). I am wondering why this is happening, and what I can do to stop the panel from being repainted when this button is pressed. (The reset button functions exactly as expected, by repainting the panel).

根据您目前发布的代码,不可能猜出哪里出了问题。我敦促您考虑创建并发布 minimal example program这使我们能够亲自了解您的问题。

但话虽如此,我要补充一点,您永远无法完全控制何时绘制或不绘制组件,因为许多绘制是由响应操作系统的 JVM 驱动的。这是程序逻辑不应永远驻留在paint(Graphics g)paintComponent(Graphics g) 方法重写中的原因之一。

所以你的问题实际上是一个XY Problem伪装。你问的是如何控制组件的重新绘制,而你应该问的是如何从一种绘制方法中获取你的程序逻辑,事实上,这是我对你问题解决方案的猜测——确保你的绘制方法仅用于绘画。


编辑
是的,您在 paintComponent 方法中有程序逻辑,特别是这段代码:

Random rand = new Random(System.currentTimeMillis());
center1X=rand.nextInt(507);
center1Y=rand.nextInt(320);
center2X=rand.nextInt(507);
center2Y=rand.nextInt(320);
center3X=rand.nextInt(507);
center3Y=rand.nextInt(320);

将它从 paintComponent 中取出并放在它自己的方法中,这样您就可以控制它何时被调用。


编辑2
例如,您可以这样做:

public class CircleDraw extends JPanel {
private int radius;
private double s;
private double area;
private Random rand = new Random(); // make this a field

// call this when you want to change the random images
public void randomizeDrawing() {
center1X = rand.nextInt(507);
center1Y = rand.nextInt(320);
center2X = rand.nextInt(507);
center2Y = rand.nextInt(320);
center3X = rand.nextInt(507);
center3Y = rand.nextInt(320);
repaint();
}

// and only do painting in paintComponent
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// draw the 3 circles
g.drawOval(center1X, center1Y, 100, 100);
g.drawOval(center2X, center2Y, 100, 100);
g.drawOval(center3X, center3Y, 100, 100);

// connect the centers of the circles with lines
g.drawLine(center1X + 50, center1Y + 50, center2X + 50, center2Y + 50);
g.drawLine(center2X + 50, center2Y + 50, center3X + 50, center3Y + 50);
g.drawLine(center3X + 50, center3Y + 50, center1X + 50, center1Y + 50);
}

关于java - repaint() 方法未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22647251/

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