Originally posted by Lucky Singh:
Hi,
I am writing a simple Java swing application.
I have a class Student that calls my swing application.
public class Student
{
Student()
{
//call my swing application
}
}
public class SwingApplication extends JFrame
{
public void createGUI()
{
JFrame f = new JFrame("hello");
JButton but = new JButton("button");
f.add(but);
}
}
How do I call my createGUI() method()?
Where do I call it?
How do I compile and execute this program?
Thanks.
I should be like the code below.
public class SwingApplication
{
public static void createGUI()
{
JFrame f = new JFrame("hello");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton but = new JButton("button");
f.getContentPane().add(but);
f.pack();
f.setVisible(true);
}
public static void main(
String arg[]){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
} //End of main
} //End of class SwingApplication
This was the answer of your 1 and 2 question.
Now to compile this, Use
Javac SwingApplication.java
And to run this application.Use
java SwingApplication
But before doing all this , it's better to you to read some book
on swing.
