2012-04-14 104 views
2

我有下面一段代码,我想说明...为什么我调用repaint()时不会改变JPanel?

public class Nodes extends JPanel implements ActionListener, MouseListener, MouseMotionListener { 

    private static JPanel p1; 
    private static JPanel p2; 
    private JButton linkButton, nodeButton; 
    private Vector nodeVector, linkVector; 
    private Vector<Integer> data; 
    private String s1,s2; 
    public int x; 
    int pt1; 
    public int pt2; 
    private Rectangle Node; 
    private boolean f1, f2 = false; 

    Nodes() { 
     JFrame F = new JFrame ("Add Node/Add Link"); 
     nodeVector = new Vector(); 
     linkVector = new Vector(); 
     data = new Vector <Integer>(); 
     F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     F.setVisible (true); 
     F.setSize (500, 500); 
     p1 = new JPanel(); 
     p2 = new JPanel(); 
     linkButton = new JButton ("Add Link"); 
     nodeButton = new JButton ("Add Node"); 
     linkButton.addActionListener (this); 
     nodeButton.addActionListener (this); 
     p2.addMouseListener (this); 
     p2.addMouseMotionListener (this); 
     p1.add (linkButton); 
     p1.add (nodeButton); 
     F.add (p1,BorderLayout.SOUTH); 
     F.add (p2,BorderLayout.CENTER); 
     p2.setBackground (Color.WHITE); 
    } 

    public void paintComponent (Graphics g) { 
     super.paintComponent (g); 
     Graphics2D g2d = (Graphics2D)(g); 
     BufferedImage buffImage = new BufferedImage (25,25,BufferedImage.TYPE_INT_RGB); 
     Graphics2D gg = buffImage.createGraphics(); 
     gg.setColor (Color.GRAY); 
     gg.fillRect (0, 0, 25, 25); 
     gg.setStroke (new BasicStroke (12.0f)); 
     gg.setColor (Color.BLUE); 
     gg.drawString ("" + data, 10, 10); 
     g2d.setPaint (new TexturePaint (buffImage, new Rectangle (25,25))); 
     g.drawRect (pt1, pt2, 10, 10); 
     repaint(); 
    } 

    public void actionPerformed(ActionEvent e){     
     if(e.getSource()==linkButton){ 
     s1 = JOptionPane.showInputDialog (null, "Insert the first Node Number"); 
      s2 = JOptionPane.showInputDialog (null, "Insert the second Node Number"); 
     } 
     if (e.getSource() == nodeButton){ 
      x = Integer.parseInt ((JOptionPane.showInputDialog (null, "Insert the Node Number"))); 
      for (int i = 0 ; i < data.size(); i++){ 
       if (x == (int) data.get (i)) 
        x = Integer.parseInt (JOptionPane.showInputDialog (null, "Used before insert another number")); 
      } 
      data.add (x); 
      f1 = true; 
     } 
    } 

    public void mouseClicked (MouseEvent e){ 
      f2 = true; 
      if (f1){ 
       JOptionPane.showMessageDialog (null, "you clicked at" + e.getPoint()); 
       pt1 = e.getX(); 
       pt2 = e.getY(); 
       f1 = !f1; 
       repaint(); 
      } 
    } 

    public static void main (String[] args) { 
     new Nodes(); 
    } 
} 

我试图通过调用repaint()更新显示,但是paintComponent()方法是行不通的,并没有什么实际变化。在正确的行为中,当我按下“添加节点”按钮,然后单击面板上的任何位置,应该绘制一个带有用户输入数字的矩形。

任何人都可以请帮忙解释我的代码有什么问题。

+0

抛弃'super.paintComponent(g);'。不要打电话重画! – 2012-04-14 08:35:39

+0

完成,,,但也不起作用 – Beesan 2012-04-14 08:37:13

+0

如果你第一次没有gg代码,g.setColor和g.fillRect?如果可行,你可以寻找一个GraphicsConfiguration的createCompatibleImage。回答已经 – 2012-04-14 08:46:41

回答

5

您的主类是一个JPanel,但不会添加到JFrame及其面板/按钮/ whatnot。

试试这样说:

private Vector<Integer> data; 
public int x; 
int pt1; 
public int pt2; 
private Rectangle Node; 
private boolean f1, f2 = false; 
private JButton linkButton, nodeButton; 

Nodes() 
{ 
    JFrame F = new JFrame ("Add Node/Add Link"); 
    Vector nodeVector, linkVector; 
    nodeVector = new Vector(); 
    linkVector = new Vector(); 
    data = new Vector <Integer>(); 
    F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    F.setVisible (true); 
    F.setSize (500, 500); 

    JPanel p1 = new JPanel(); 
    linkButton = new JButton ("Add Link"); 
    nodeButton = new JButton ("Add Node"); 
    linkButton.addActionListener (this); 
    nodeButton.addActionListener (this); 
    addMouseListener (this); 
    addMouseMotionListener (this); 
    p1.add (linkButton); 
    p1.add (nodeButton); 
    F.add (p1, BorderLayout.SOUTH); 
    F.add (this, BorderLayout.CENTER); 
    setBackground (Color.WHITE); 
} 

许多属性可以是局部变量。 JPanel p2被删除并替换为this的JPanel,允许使用paintComponent方法。

4

您绝不会将节点添加到组件层次结构中,因此paintComponent永远不会被调用。

+0

我应该写另一个类节点! !我怎么可以添加一个节点 – Beesan 2012-04-14 08:54:04

+0

以及你可以在构造函数中使用'F.add(this)',但这是一个糟糕的设计。您应该收集另一个类,将节点添加到JFrame。 – 2012-04-14 09:08:55

+0

是的,它可以工作,我会试试 thnx mr Polet – Beesan 2012-04-14 09:19:22

相关问题