This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

The JLabel cannot be displayed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

}



}
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic