我想知道为什么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;
}
}
现在在这里,当我添加节点并调用重绘时,在绘制区域中什么都没有出现:
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
。基于该出您所提供的,我留下来“承担”你要重写代码方面片断paint
中的Main
类,这种方式,paint
将永远不会像它实际上没有连接到显示组件调用Start
不应该创建一个JFrame
。您应创建的实例,Main
然后将其添加到的实例中JFrame
,两者应分开。forMain
应该创建并添加到Main
自身中paint
(作为一般规则),但是您不应通过调用其他paint
方法之一来反抗绘画过程paintComponents
。相反,(再次“假设”你覆盖paint
的Main
),你应该重写paintComponent
方法和呼叫super.paintComponent
之前,你做任何定制油画有关更多详细信息,请参阅在AWT和Swing中执行自定义绘画和绘画。
另外,您可能希望通读Java TM编程语言的代码约定,这将使人们更容易阅读您的代码,并使您更容易阅读其他代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句