Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The JLabel cannot be displayed

 
wux bill
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem:
As I pressed the Button ,but the JLabel doesn't display;
the program is follow:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class Toocaiquan implements ActionListener{
Frame fr=new Frame();
Toocaiquan(){
init();}
JButton bt1=new JButton("石头");
JButton bt2=new JButton("剪刀");
JButton bt3=new JButton("布");
Panel p=new Panel();
//Te/xtArea te=new TextArea(200,200);
public void actionPerformed(ActionEvent e)
{
Random ra=new Random();
int c=ra.nextInt(2);

if(e.getSource()==bt1)

switch(c){
case 0:
p.add(new JLabel("你赢了"));
break;
case 1:
p.add(new JLabel("平了"));
break;
case 2:
p.add(new JLabel("你输了"));
break;
}
else if (e.getSource()==bt2)

switch(c){
case 0:
p.add(new JLabel("平了"));
break;
case 1:
p.add(new JLabel("你输了"));
break;
case 2:
p.add(new JLabel("你赢了"));

break;
}
else if (e.getSource()==bt3)

switch(c){
case 0:
p.add(new JLabel("你输了"));
break;
case 1:
p.add(new JLabel("你赢了"));
break;
case 2:
p.add(new JLabel("你输了"));
break;

}
}

public void init(){

//super("猜拳");
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);

p.add(bt1);
p.add(bt2);
p.add(bt3);
fr.add(p);
//fr.add(te);

fr.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
fr.setSize(600,500);
fr.setVisible(true);

}
//addWindowListener(new win());



public static void main (String args[]){

new Toocaiquan();

}



}
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I always construct my JLabel (empty or not) when initializing the gui ( in the constructor)
and then later, when I want to show something: myJLabel.setText("My text")!

Seems better.

Ko
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add components, you need to call invalidate() on the container, so that it will be layed out again.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ko Wey:
Hi,

I always construct my JLabel (empty or not) when initializing the gui ( in the constructor)
and then later, when I want to show something: myJLabel.setText("My text")!

Seems better.

Ko


He seems to want to add an arbitrary number of labels, at least that is
what is currently written.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Swing Questions should be posted in the Swing/etc forum.
2. Use code tags to make posted code more readable.
3. Don't mix Swing and AWT components. You used Frame and Panel instead of JFrame and JPanel.
4. Post minimal example programs that demonstrate your problem. The longer the code, the
fewer forum members will attempt it. In your case, the problem was that components added
dynamically weren't appearing. You need one button and no randomness to demonstrate that.
5. Suggestion: with JFrame, setDefaultCloseOperation is simpler than a window listener.
6. Finally: run this code as is, and then uncomment the commented line
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
If you add components, you need to call invalidate() on the container, so that it will be layed out again.


Container's add method calls invalidate for you (you can check by calling
isValid() before and after calling add(comp)).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding p.validate(); , before closing your actionPerformed() method.
validate() method cause a container to lay out its subcomponents again.
It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
For more information check java api.

regards,
Amrutha
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amrutha Chowdary:
Try adding p.validate(); , before closing your actionPerformed() method.
validate() method cause a container to lay out its subcomponents again.
It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
For more information check java api.


Umm, my previous post added a call to revalidate. Method revalidate is
the Swing take on the AWT method validate. Check out the API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic