How do you pass variable values from one class to another?
I have two classes. One of them generates a random number and also creates an instance of a LabelFrame. I want the actual JLabel in the LabelFrame to display the value of the random number. Unfortunately, I can not figure out how to get to work. How do I pass the value of the random number generated in ShowTick.java to the JLabel in LabelFrame.java?
Here is my code..
ShowTick.java Text
import java.util.*;
import javax.swing.*;
import java.awt.FlowLayout;
public class ShowTick
{
public static void main (String[] args)
{
int iTick;
iTick = getTick();
System.out.println(iTick);
LabelFrame lf = new LabelFrame();
lf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lf.setSize(275, 180);
lf.setVisible(true);
}
public static int getTick()
{
int intRet;
Random randomNumbers = new Random();
intRet = 1 + randomNumbers.nextInt(2);
return intRet;
}
}
LabelFrame.java text
import java.util.*;
import javax.swing.*;
import java.awt.FlowLayout;
public class LabelFrame extends JFrame
{
private JLabel label1;
public LabelFrame()
{
super("JLabel Test");
setLayout(new FlowLayout());
label1 = new JLabel("Lbl w/ text");
label1.setText("Hi");
add(label1);
}
}
Solved the problem...here's the answer
Edit the line in ShowTick where I create the instance of the LabelFrame as follows:
LabelFrame lf = new LabelFrame(iTick);
In the LabelFrame class, edit the Public class so that it takes a parameter, then edit the label settext as follows:
public LabelFrame(int somenum)
label1.setText("" + somenum);
Now it works.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks