Java 形状不';t出现(直到JFrame最小化并恢复)
这里我想要的是,当点击按钮时,形状应该立即出现,但是形状不会出现,除非JFrame最小化并恢复Java 形状不';t出现(直到JFrame最小化并恢复),java,jframe,Java,Jframe,这里我想要的是,当点击按钮时,形状应该立即出现,但是形状不会出现,除非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(
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);
}
}
在这里,它看起来像是单击的,但不是最小化的
在这里,当JFrame被最小化并恢复时,它看起来像是一个绿色节点,我想您稍后会添加它吗?如果您直接修改类而不是Swing类,我认为您必须手动在Swing类/容器上发出repaint(),否则Swing不知道需要在屏幕上更新组件(Swing类在更改时自动执行此操作,因此您不必在调用add()时执行此操作)标记空间
相关:Repaint()vs.Revalidate()stackoverflow.com/questions/1097366/…也做一些搜索来了解Swing的重绘系统是如何工作的(很多教程)。-markspace我想您稍后会添加绿色节点吗?如果您直接修改类而不是Swing类,我认为您必须在Swing类/容器上手动发出
repaint()
,否则Swing不知道需要在屏幕上更新组件(Swing类在更改时会自动执行此操作,因此您不必在调用add()时执行此操作)
例如)。相关:Repaint()vs.Revalidate()也做一些搜索来了解Swing的重新绘制系统是如何工作的(有很多教程)。Thnks很多先生,,,它对我有用