我想知道为什么 repiant() 方法不再按预期工作......例如:
public class Main extends JPanel implements ActionListener, MouseListener,MouseMotionListener{
private ArrayList<Node> nodes;
private ArrayList<Edge> edges;
private boolean AddNode;
private int no_Of_Nodes;
private int width = 30, height = 30;
public static void main(String[] args){
Main M = new Main();
M.Start();
}
public void Start() {
nodes = new ArrayList<Node>();
edges = new ArrayList<Edge>();
JFrame f = new JFrame("SFG");
JPanel main_Panel = new JPanel(new BorderLayout());
JPanel buttons = new JPanel();//Buttons Containser
JPanel draw = new JPanel();
ArrayList<JButton> bs = new ArrayList<JButton>();
JButton b1 = new JButton("Add Node");
b1.addActionListener(new Add_Node());
JButton b2 = new JButton("Add Edge");
b2.addActionListener(new Add_Edge());
JButton b3 = new JButton("Add Arc");
b3.addActionListener(new Add_Arc());
JButton b4 = new JButton("Clear all");
b4.addActionListener(new Clear());
JButton b5 = new JButton("Solve");
b5.addActionListener(new Solve());
Bs.add(b1);
Bs.add(b2);
Bs.add(b3);
Bs.add(b4);
Bs.add(b5);
for (int i = 0; i < bs.size(); i++) {
Buttons.add(bs.get(i));
}
Buttons.setBackground(Color.GRAY);
main_Panel.add(Buttons,BorderLayout.SOUTH);
draw.setBackground(Color.darkGray);
draw.addMouseMotionListener(this);
draw.addMouseListener(this);
main_Panel.add(Draw);
main_Panel.setBackground(Color.GRAY);
f.add(main_Panel);
f.setSize(1024, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
这些是方法
public void actionPerformed(ActionEvent arg0) {
this.repaint();
}
public class Add_Node implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Add Node");
addNode = true;
}
}
现在,当我添加节点并调用 repaint 时,绘制区域中不会出现任何内容:
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
if(addNode){
addNode(arg0);
addNode = !addNode;
}
System.out.println(nodes.size());
this.repaint();
}
private void addNode(MouseEvent arg0) {
// TODO Auto-generated method stub
int x = arg0.getX();
int y = arg0.getY();
Node n = new Node(No_Of_Nodes++);
n.setX_Pos(X);
System.out.println(x + " " + y);
n.setX_Pos(Y);
nodes.add(n);
}
这就是我的 Paint() 方法,它不再起作用了
public void paint(Graphics g){
super.paintComponents(g);
FontMetrics f = g.getFontMetrics();
int nodeHeight = Math.max(height, f.getHeight());
System.out.println("In repaint");
for (Node n : nodes) {
System.out.println(n.getX_Pos() + " " + n.getY_Pos());
int nodeWidth = Math.max(width, f.stringWidth(Integer.toString(n.getNode_ID()))+width/2);
g.setColor(Color.white);
g.fillOval(n.getX_Pos()-nodeWidth/2, n.getY_Pos()-nodeHeight/2, nodeWidth, nodeHeight);
g.setColor(Color.black);
g.drawOval(n.getY_Pos()-nodeWidth/2, n.getY_Pos()-nodeHeight/2, nodeWidth, nodeHeight);
g.drawString(Integer.toString(n.getNode_ID()) , n.getX_Pos()-f.stringWidth(Integer.toString(n.getNode_ID()))/2, n.getY_Pos()+f.getHeight()/2);
}
}
TIA,很抱歉问了这么长的问题:)
最佳答案
- 在您的
Start
方法中,您实际上从未将Main
添加到JFrame
中。根据您提供的上下文之外的代码片段,我“假设”您正在覆盖Main
类的paint
,这意味着,paint
永远不会被调用,因为它实际上并未附加到可显示组件 Start
不应创建JFrame
。您应该创建一个Main
实例,然后将其添加到JFrame
实例中,两者应该是分开的。应该创建 forMain
并将其添加到Main
本身- 您不应该重写
paint
(作为一般规则),但您绝对不应该通过调用其他paint
方法之一来规避绘画过程,例如paintComponents
。相反(再次“假设”您要重写Main
中的paint
),您应该重写paintComponent
方法并调用super.paintComponent
在进行任何自定义绘画之前
看看Performing Custom Painting和 Painting in AWT and Swing了解更多详情。
此外,您可能想通读 Code Conventions for the Java TM Programming Language ,这将使人们更容易阅读您的代码,也让您更轻松地阅读其他人
关于Java repaint() 在调用时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227146/