i正在工作一个可以绘制数学函数图的摆动应用程序。我正在使用GetGraghics函数,但我不知道如何删除和重新粉刷它们,因此我决定覆盖PaintComponent()方法来实现我在寻找的内容
我想做的是在用户单击按钮后,在面板中绘制功能图。但是似乎PaintCompnent()无法正常工作。我完全关注了堆栈溢出上的任何现有教程和类似问题。
请帮助我在整个晚上都陷入了这个问题:(
以下是用于绘制函数图的代码,但由于它不起作用,因此我仅离开了绘图坐标系进行测试的一部分,然后是我如何创建实例的代码,并尝试将其添加到我的面板中主要类
class drawfunction extends JPanel{
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(0, 200, 400, 200);
g.drawLine(200,0 , 200, 400);
}
}
然后是主类中的代码
JPanel panel = new JPanel();
panel.setBounds(14, 104, 400, 400);
contentPane.add(panel);
panel.setBackground(Color.white);
JButton btnNewButton = new JButton("View the graph");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//a= Integer.parseInt(cofficient_a.getText());
//b= Integer.parseInt(cofficient_b.getText());
//c= Integer.parseInt(cofficient_c.getText());
//d= Integer.parseInt(cofficient_d.getText());
//e= Integer.parseInt(cofficient_e.getText());
drawfunction a=new drawfunction();
panel.add(a);
});
任何人都可以告诉我我应该做什么来解决这个问题。谢谢!!!
两个基本的东西...
- 组件的默认优先大小为
0x0
,因此,在将其添加时,几乎任何布局管理器的控件都将其大小为0x0
(或非常接近) - 摇摆通常是懒惰的,当您添加或删除组件时,它不会更新UI,这可能会阻碍性能,因为UI不知道根据您的操作来更新UI,而是您需要致电
revalidate
和(大多数时候)repaint
以更新UI
例如...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JPanel center;
public TestPane() {
setLayout(new BorderLayout());
JButton btnNewButton = new JButton("View the graph");
center = new JPanel();
center.setPreferredSize(new Dimension(400, 400));
add(center);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
remove(center);
//a= Integer.parseInt(cofficient_a.getText());
//b= Integer.parseInt(cofficient_b.getText());
//c= Integer.parseInt(cofficient_c.getText());
//d= Integer.parseInt(cofficient_d.getText());
//e= Integer.parseInt(cofficient_e.getText());
center = new Drawfunction();
add(center);
revalidate();
repaint();
}
});
add(btnNewButton, BorderLayout.NORTH);
}
public class Drawfunction extends JPanel {
public Drawfunction() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.red);
g2d.drawLine(0, 200, 400, 200);
g2d.drawLine(200, 0, 200, 400);
g2d.dispose();
}
}
}
}