简体   繁体   English

形状不会出现(直到最小化并恢复JFrame)

[英]Shapes doesn't appear (until JFrame is minimized and restored)

Here what I want is that when the Buttons are clicked the shapes should appear immediately, but the shapes does not appear unless the JFrame is minimized and restored. 这里我想要的是,当单击“按钮”时,形状应该立即显示,但是除非最小化并恢复JFrame,否则形状不会出现。

package tutorial;
public class Tutorial extends JPanel{
JButton b1,b2,b3,b4,b5;

boolean nodeA, nodeB,communicate, key, acknowledge = false;
public  Tutorial(){

    b1 = new JButton("Node A");
    add(b1);
    b2 = new JButton("Node B");
    add(b2);
    b3 = new JButton("Communicate");
    add(b3);
    b4 = new JButton("send key");
    add(b4);
    b5 = new JButton("Request B");
    add(b5);
}

public void paintComponent(Graphics g){

    super.paintComponent(g);
    g.setColor(Color.black);
    g.fillRect(300, 100,100, 50);
    g.setColor(Color.red);
    g.drawString("KDC/KMC",320,130);


    b1.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            nodeA = true;
        }
    });

    if(nodeA == true){
        g.setColor(Color.green);
        g.fillOval(100, 300, 70, 70);
        g.setColor(Color.red);
        g.drawString("Node A", 113, 340); 
        g.drawString("Node A added",150,220);
        g.drawLine(150, 300, 300, 150);
    }

    b2.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            nodeB = true;    
        }
     });
    if(nodeB == true){
        g.setColor(Color.green);
        g.fillOval(530, 300, 70, 70);
        g.setColor(Color.red);
        g.drawString("Node B",545,340);
        g.drawString("Node B added",473,220);
        g.drawLine(550, 300, 400, 150); 
    }

    b3.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            if(nodeA == true && nodeB == true)
                communicate = true;
        }
     });
    if(communicate == true){
        g.drawString("A requests for B's Session Key", 230,260);
        g.drawLine(165, 310, 325, 150);
    }

    b4.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            if(communicate == true)
                key = true;    
        }
     });
    if(key == true){
        g.drawString("A's key is 4",210,175);
        g.drawString("B's key is 5",430,175);
    }

    b5.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            if(key == true)
                acknowledge = true;    
        }
     });
    if(acknowledge == true){
        g.drawLine(170,325,530,325);
        g.drawString("A sends part of session key to B", 260, 320);
        g.drawLine(170,350,530,350);
        g.drawString("if sessiion key match then B sent Acknowledgement", 215, 365);
  }}

public static void main(String[] args) {

    Tutorial t = new Tutorial();
    JFrame jf = new JFrame("Assignment");

    jf.setSize(1200,900);
    jf.add(t);
    jf.setVisible(true);        
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

} }

Here it looks like when clicked but not minimized enter image description here and here it looks like when JFrame is minimized and restored enter image description here 在这里看起来像单击但未最小化时此处输入图像描述 ,这里看起来像在最小化并恢复JFrame时在此处输入图像描述

I assume you do something to add that green node later? 我假设您稍后要添加该绿色节点? If you modify classes directly without them being Swing classes, I think you manually have to issue a repaint() on your Swing class/container or Swing doesn't know that the components needs to be updated on the screen (Swing classes do this automatically when they are changed, so you don't have to do it when you call add() for example). 如果您直接修改类而不将其作为Swing类,则我认为您必须手动在Swing类/容器上发出repaint(),否则Swing不知道需要在屏幕上更新组件(Swing类会自动执行此操作)当它们被更改时,例如,当您调用add()时,您不必这样做。 – markspace –标记空间

Related: Repaint() vs. Revalidate() stackoverflow.com/questions/1097366/… Do some searches too to understand how Swing's repaint system works (lots of tutorials). 相关:Repaint()与Revalidate()stackoverflow.com/questions/1097366/…也进行一些搜索以了解Swing的重画系统是如何工作的(很多教程)。 – markspace –标记空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM