我想知道为什么repiant()方法不再按预期工作...... Ex:
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并抱歉长期提问:)
答案 0 :(得分:2)
Start
方法中,您实际上从未将Main
添加到JFrame
。根据您提供的代码的上下文片段,我将“假设”您正在覆盖paint
类的Main
,这意味着,paint
将永远不会被调用,因为它实际上没有附加到可显示的组件Start
不应该创建JFrame
。您应该创建Main
的实例,然后将其添加到JFrame
的实例中,两者应该是分开的。应创建Main
for Main
并将其添加到paint
本身paint
(作为一般规则),但是您应该通过调用其他paintComponents
方法(例如paint
)来躲避绘制过程。相反(再次“假设”你在Main
中覆盖paintComponent
),你应该覆盖super.paintComponent
方法并在进行任何自定义绘画之前调用function myClickFunction(event:MouseEvent) {
if (clown.visible){
clown.visible = false;
elephant.visible = true;
}
}
请查看Performing Custom Painting和Painting in AWT and Swing了解详情。
此外,您可能希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码