gpt4 book ai didi

java - 将 repaint() 方法与 actionPerformed 一起使用

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:41 26 4
gpt4 key购买 nike

当按下按钮并且图形 p 必须从头开始重新绘制所有内容时,如何使用 repaint() 方法?

谢谢。

  import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class fares extends JPanel{

private static final long serialVersionUID = 1L;
public static int xaxis1,xaxis2,yaxis3,yaxis4;

public ControlsB(square) {

final JButton btn1 = new JButton("Resize");

final Box b = Box.createHorizontalBox();
b.add(new JLabel("Please enter range: "));
Box b0 = Box.createVerticalBox();//create a vertical box to stack the controls

Box b1 = Box.createHorizontalBox(); // create a horizontal box for the x-axis

//x-axis
b1.add(new JLabel("mark "));
b1.add(new JLabel("for"));

f1.setMaximumSize(new Dimension(100,30));

b1.add(f1);
b1.add(new JLabel("till"));

f2.setMaximumSize(new Dimension(100,30));
b1.add(f2);

//y-axis
//this code is not in use at the moment

f4.setMaximumSize(new Dimension(100,30));
b2.add(f4);

b0.add(b1);

add(b);

btn1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent event){

f = Integer.parseInt(f1.getText());

invalidate();
validate();
paint(p);//this is not working...
}
});
b.add(btn1);
}
}

这是必须调用和重绘的代码:

import java.awt.*;

import javax.swing.*;

class Graph extends JPanel {

public static Graphics p;
private static final long serialVersionUID = 1L;
public static int f;
public static int g;

@Override
public Dimension getPreferredSize()
{
return (new Dimension(560,560));
}

public void paintComponent(Graphics p) {

super.paintComponent(p);

Graphics2D graph = (Graphics2D)p;

Dimension appletSize = this.getSize();
int appletHeight = (int)(appletSize.height);
int appletWidth = appletSize.width;

//change -ve num to +ve
int g3 = Math.abs(g);

int a1 = g3 + f;
int b1 = a1;


int d = (appletWidth / a1);
int e = (appletHeight / b1);

//draw y-axis numbers
//(+ve)
while(f != 0){
String s = String.valueOf(f);

m = m + b;
f = f - 1;
}
//(-ve)
m2 = y;
while(f2 != g-1)
m2 = m2 + b;
f2 = f2 - 1;
}
//draw x-axis numbers.
//(-ve)
while(g != 0){
String hy = String.valueOf(g);

n = n + a;
g = g + 1;
}
//(+ve)
n2 = x + a;
while(g2 != g3+1){
String w = String.valueOf(g2);
n2 = n2 + a;
g2 = g2 + 1;
}

BasicStroke aLine2 = new BasicStroke(1.0F,
BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
graph.setStroke(aLine2);

//notch on numbers and grid lines
//left to right, top to bottom notches
int v2 = -5;
int v5 = 0;
while(i <= a1-1){
p.setColor(Color.lightGray);//lightgray line
a = a + d;
b = b + e;
i = i + 1;
}

}
}

目前,调整大小按钮可以使用,但我需要调整窗口大小,以便图表响应给定的输入。基本上在调整图形大小时正在重新绘制/重新绘制...现在我需要自动完成此操作。

最佳答案

很难根据代码片段判断出什么问题,但看起来您的图表是在 Graph 对象上绘制的,称为 graph (如果我错了请纠正我),并且您似乎正在尝试重新绘制(或绘画——永远不要直接调用它!)你的 ControlsB 面板。如果是这样,那么您可能在错误的对象上调用了方法。也许您需要执行以下操作:

// graph is final so it may be used in an inner class
public ControlsB(Box box2, final Graph graph) {

// .....

btn1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent event){

f = Integer.parseInt(f1.getText());
g = Integer.parseInt(f2.getText());
System.out.println(f + " " + g);

// invalidate();
// validate();
// paint(p); ***** NEVER do this

graph.repaint();
}
});
}

还有:

  • 切勿直接在组件上调用 paint(...),除非在非常特殊的情况下(不是这样)。
  • 永远不要试图捕获一个组件的 Graphics 对象并用它来绘制,因为这通常会导致 NullPointerException 发生,而且它肯定不会起作用。
  • 一定要阅读标准 Swing 教程中有关如何在 Swing 中绘图的内容。您可能会从中学到很多东西。
  • 同样,如果您不能很快得到满意的答案,请考虑创建并发布一个 sscce .

关于java - 将 repaint() 方法与 actionPerformed 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11129608/

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