 |
| Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 24th, 2008, 08:36 PM
|
|
Authorized User
|
|
Join Date: Dec 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
how to see a label
Hello
I hawe two clsses
I am tryin to see the label that say "CAN YOU SEE ME?"
Can anyone tell me whay can't I see it, please?
=============
First.java
=============
Code:
import javax.swing.*;
public class First extends JFrame {
Second second;
public static void main(String[] args) {
new First();
}
public First() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
second = new Second(this);
second.setSize(220, 150);
setLocation(400,200);
setSize(500, 200);
setVisible(true);
second.setLocationRelativeTo(this);
second.setVisible(true);
}
}
=============
Second.java
=============
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Second extends JDialog implements ActionListener {
JButton button;
public Second(Frame parent) {
super(parent, true);
button = new JButton("##### - = b u t t o n = - #####");
button.addActionListener(this);
add(button);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == button) odpriPovezavo();
}
public void odpriPovezavo() {
JDialog jDialog = new JDialog(this, "Do you see the label?", false);
jDialog.add(new JLabel("CAN YOU SEE ME?", JLabel.CENTER));
jDialog.setSize(300, 70);
jDialog.setLocationRelativeTo(this);
jDialog.setVisible(true);
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
jDialog.setVisible(false);
}
}
thank you for any help
|
|

February 29th, 2008, 02:31 PM
|
|
Friend of Wrox
|
|
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
If it were me, I'd just use a timer. Like this:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Second extends JDialog implements ActionListener {
JButton button;
// we need these in more than one method its ok to make them class attributes
JDialog jDialog;
Timer t;
public Second(Frame parent) {
super(parent, true);
button = new JButton("##### - = b u t t o n = - #####");
button.addActionListener(this);
add(button);
jDialog = new JDialog(this, "Do you see the label?", false);
t = new Timer(2000,this) ; // We have a sw4nky Swing timer now :-)
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == button) odpriPovezavo();
else if(jDialog.isVisible()==true) {
jDialog.setVisible(false);
t.stop(); // stop timer
}
}
public void odpriPovezavo() {
jDialog.getContentPane().add(new JLabel("CAN YOU SEE ME?", JLabel.CENTER));
jDialog.setSize(300, 70);
jDialog.setLocationRelativeTo(this);
jDialog.setVisible(true);
t.start(); // start timer
}
}
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
|
|

March 2nd, 2008, 10:33 AM
|
|
Authorized User
|
|
Join Date: Dec 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
what about like this:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Second extends JDialog implements ActionListener {
JButton button;
JDialog jDialog; //class attributes
public Second(Frame parent) {
super(parent, true);
button = new JButton("##### - = b u t t o n = - #####");
button.addActionListener(this);
add(button);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == button) odpriPovezavo();
else if(jDialog.isVisible() == true) {
jDialog.setVisible(false);
//t.stop(); // stop timer
}
}
public void odpriPovezavo() {
jDialog = new JDialog(this, "Do you see the label?", false);
jDialog.getContentPane().add(new JLabel("CAN YOU SEE ME?", JLabel.CENTER));
jDialog.setSize(300, 70);
jDialog.setLocationRelativeTo(this);
jDialog.setVisible(true);
Timer t = new Timer(2000, this) ; // We have a sw4nky Swing timer now :-)
t.setRepeats (false);
t.start(); // start timer
}
}
what do you mean with "sw4nky"
whay do we need (is it necessary):
t.stop(); // stop timer
|
|

March 4th, 2008, 06:15 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
I was trying to write s-w-a-n-k-y but, because w-a-n-k is a swearword in the opinion of the folks at wrox, it got "starred out". So the '4' is just a substitute for 'a' ;)
Nah, you don't really need the t.stop(); if you have setRepeats set false.
Cheers,
Charlie
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
|
|
 |