Java 为什么';调用repaint()时我的JPanel是否会更改?

Java 为什么';调用repaint()时我的JPanel是否会更改?,java,swing,event-listener,Java,Swing,Event Listener,我有下面的代码,我想帮助 public class Nodes extends JPanel implements ActionListener, MouseListener, MouseMotionListener { private static JPanel p1; private static JPanel p2; private JButton linkButton, nodeButton; private Vector nodeVector, lin

我有下面的代码,我想帮助

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();
    }   
}
公共类节点扩展JPanel实现ActionListener、MouseListener、MouseMotionListener{
私有静态JPanel p1;
私有静态jpanelp2;
私有JButton链接按钮、节点按钮;
私有向量nodeVector、linkVector;
专用矢量数据;
私有字符串s1、s2;
公共int x;
int pt1;
公共int pt2;
私有矩形节点;
私有布尔值f1,f2=false;
节点(){
JFrame F=新JFrame(“添加节点/添加链接”);
nodeVector=新向量();
linkVector=新向量();
数据=新向量();
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.setVisible(真);
F.设置尺寸(500500);
p1=新的JPanel();
p2=新的JPanel();
linkButton=新的JButton(“添加链接”);
nodeButton=newjbutton(“添加节点”);
linkButton.addActionListener(此);
nodeButton.addActionListener(此);
p2.addMouseListener(本);
p2.addMouseMotionListener(此);
p1.add(链接按钮);
p1.add(节点按钮);
F.添加(p1,南部边界布局);
F.添加(p2,边框布局。中心);
p2.背景(颜色为白色);
}
公共组件(图形g){
超级组分(g);
图形2d g2d=(图形2d)(g);
BuffereImage buffImage=新的BuffereImage(25,25,BuffereImage.TYPE_INT_RGB);
Graphics2D gg=buffImage.createGraphics();
gg.setColor(Color.GRAY);
gg.fillRect(0,0,25,25);
gg.设定行程(新基本行程(12.0f));
gg.setColor(Color.BLUE);
细绳(“+数据,10,10);
g2d.setPaint(新的TexturePaint(buffImage,新的矩形(25,25));
g、 drawRect(pt1、pt2、10、10);
重新油漆();
}
已执行的公共无效操作(操作事件e){
如果(例如getSource()==linkButton){
s1=JOptionPane.showInputDialog(null,“插入第一个节点号”);
s2=JOptionPane.showInputDialog(null,“插入第二个节点号”);
}
if(e.getSource()==nodeButton){
x=Integer.parseInt((JOptionPane.showInputDialog(null,“插入节点号”));
对于(inti=0;i
我试图通过调用
repaint()
来更新显示,但是
paintComponent()
方法不起作用,实际上没有任何变化。在正确的行为中,当我按下“添加节点”按钮,然后单击面板上的任意位置时,应绘制一个带有用户输入的数字的矩形


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

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

您的主类是JPanel,但它没有添加到JFrame及其面板/按钮/诸如此类

试着这样做:

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);
}
专用矢量数据;
公共int x;
int pt1;
公共int pt2;
私有矩形节点;
私有布尔值f1,f2=false;
私有JButton链接按钮、节点按钮;
节点()
{
JFrame F=新JFrame(“添加节点/添加链接”);
向量nodeVector,linkVector;
nodeVector=新向量();
linkVector=新向量();
数据=新向量();
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.setVisible(真);
F.设置尺寸(500500);
JPanel p1=新的JPanel();
linkButton=新的JButton(“添加链接”);
nodeButton=newjbutton(“添加节点”);
linkButton.addActionListener(此);
nodeButton.addActionListener(此);
addMouseListener(这个);
addMouseMotionListener(此);
p1.add(链接按钮);
p1.add(节点按钮);
F.添加(p1,南部边界布局);
F.添加(此,BorderLayout.CENTER);
挫折地面(颜色:白色);
}

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

省略
super.paintComponent(g)。不要叫重新油漆!完成了,但也没有work@JoopEggen省略super.paintComponent(g)-错误:JPanel在默认情况下是不透明的,因此必须完全填充其区域(super就是这样做的)。或者,子类可以接管该任务(节点不这样做)请学习java命名约定并遵守它们。按照原始海报:请忽略@joopeggen善意但误导的建议。我是否应该编写另一个类节点!!我怎样才能添加一个节点呢?您可以在构造函数中使用
F.add(this)
,但这是一个糟糕的设计。您应该让另一个类将节点添加到JFrame。