File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes The JLabel cannot be displayed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "The JLabel cannot be displayed" Watch "The JLabel cannot be displayed" New topic
Author

The JLabel cannot be displayed

wux bill
Greenhorn

Joined: Nov 10, 2005
Posts: 5
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

Joined: Sep 08, 2003
Posts: 67
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
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
If you add components, you need to call invalidate() on the container, so that it will be layed out again.


Ping & DNS - my free Android networking tools app
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jul 11, 2001
Posts: 14112
Moving to Swing/AWT...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sowjanya Chowdary
Ranch Hand

Joined: Aug 22, 2005
Posts: 35
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

Joined: Sep 16, 2005
Posts: 1780
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The JLabel cannot be displayed