• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing a simple Java swing

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there are several solutions. I suggest you take a good book on swing.
To get something to try, look at this:

//file Student.java
import java.awt.*;
import javax.swing.*;

public class Student
{
Student()
{
//call my swing application
new SwingApplication();
}


class SwingApplication extends JFrame
{

public SwingApplication(){
super();
createGUI();
}

private void createGUI()
{
JFrame f = new JFrame("hello");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JButton but = new JButton("button");
getContentPane().add(but,BorderLayout.SOUTH);
pack();
setVisible(true);
}
}

public static void main(String[]args){new Student();}

}


Here your main-class is Student, and your swing-class(SwingApplication) is an innerclass of Student. Mark the use of a constructor.
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you people are doing with the problem.

Extending frame and creating object of frame too.

I am not clear what you want to do with this application.

 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies to Mr. Singh and thanks to MR.Gupta!
My code snippet should be read as:

import java.awt.*;
import javax.swing.*;

public class Student
{
Student()
{
//call my swing application
new SwingApplication();
}


class SwingApplication extends JFrame
{

public SwingApplication(){
super("Hello");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JButton but = new JButton("button");
getContentPane().add(but,BorderLayout.SOUTH);
pack();
setVisible(true);
}

}

public static void main(String[]args){new Student();}

}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!