• (2 Pages)
  • +
  • 1
  • 2

Basic GUI in JAVA (using JFrames) discussing some aspects in creating Graphical User Interface in Java Rate Topic: ***** 1 Votes

Poll: Poll Question (28 member(s) have cast votes)

What do you think of the GUI in JAVA?

  1. Excellent (21 votes [75.00%])

    Percentage of vote: 75.00%

  2. Not my choice (7 votes [25.00%])

    Percentage of vote: 25.00%

Vote Guests cannot vote

#1 jeremejazz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 48
  • Joined: 23-April 10

Posted 20 December 2010 - 07:50 PM

*
POPULAR

Here we will be discussing some aspects in creating Graphical User Interface in Java. :rolleyes:

Creating our First Window
In order to create windows in Java, we are going to need the components located in javax.swing. Well, java.awt also contains window components although they appear to be unstable especially when it comes to different operating environments.

So we start by creating our first window:
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class FrameDemo{

public static void main(String args[]){
JFrame myFrame = new JFrame("This is my frame");
myFrame.setSize(300,400);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}



which would produce an output like this:
Posted Image



Code Explanation
On our first lines of code, we have just imported the library classes which we will use. First is JFrame and the next is WindowConstants. JFrame is used for creating windows. WindowConstants constants needed in creating windows.

On our main method, we create an instance of the JFrame by giving it an object name and a title as well:
JFrame myFrame = new JFrame("This is my frame");


Then we set what will happens when we close the window(such as clicking on the close button):

myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


we set this in order to make sure that our window exits the moment we close it. Other constants can be used if preferred which are namely:

  • DO_NOTHING_ON_CLOSE - nothing happens when you click the close button.
  • HIDE_ON_CLOSE - The window dissapears but it is still there. You may show this using the setVisible() method
  • DISPOSE_ON_CLOSE - destroys the object created.


try them by replacing the code with these constants to observe the effects. I suggest you use an Integrated Development Environment (IDE) first when you're going to try this. Most IDE's have a stop tool that lets you terminate a process.

And out last line:
myFrame.setVisible(true);


by default, the frame is set by false. Unless you include this code, you won't be seeing it. You may also use this code for hiding frames. We will be using this on our further examples.

Getting Inside
Inheriting our Class

So far, what we were doing on the previous examples was merely just creating a window. If you really want to manipulate a window, you have to build it from inside. One way to do this is to make your class 'be' the window. This method is called, inheritance-which is making your class inherit all properties as well as behaviors. This can be done with the use of the extends keyword.
But before we start, I would like to first introduce to you some container classes:
  • JFrame - the basic java application window. It has a title bar and a provision for adding a menu
  • JDialog - This usually appears as a prompt for inputting data as well as displaying them.
  • JApplet - This is designed to run embedded in a web page. You can draw and add menu components here
  • JPanel - used as a container that can be created within a window. Takes also some behaviors of JApplet where you can also draw and add some components except menu types.


Now let us proceed to our program. First we apply these principles to our previous program. The code should look like this:
import javax.swing.JFrame;
public class FrameDemo2 extends JFrame{
/**
* Constructor - This method automatically run whenever an object of FrameDemo2 is created.
**/
public FrameDemo2(){
super("Behold, our new frame");
}

public static void main(String args[]){
FrameDemo2 myFrame = new FrameDemo2();
myFrame.setSize(300,400);
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}

The advantage of this code is encapsulation, and designing modular, reusable, and extensible code For example, we could then extend the MyFrame class to further customize it. And this way, we can also set up the JFrame without cramming all the code into one .java File or class (which an author of a book I read called, "The Giant Objects Syndrome").
The super() function is for calling the constructor of the base class(the class inherited from) which specifies the title. This is just for giving your frame a default title. I almost forgot, you can set the title of the frame by using the setTitle() property:
myFrame.setTitle("This is the customized title");



Reference:
Beginning Java 2 by Ivor Horton
Thinking in Java by Bruce Eckel
You can also find this topic in Squidoo.com(Basic GUI in JAVA)


Is This A Good Question/Topic? 6
  • +

Replies To: Basic GUI in JAVA (using JFrames)

#2 mittu.thefire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-December 10

Posted 28 December 2010 - 04:56 AM

but when i executed ur 1st program
i got the error as " Exception in thread "main" java.lang.NoSuchMethodError: main "


please help me

This post has been edited by macosxnerd101: 28 December 2010 - 08:54 AM
Reason for edit:: Removed the quote. No need to quote the person above you.

Was This Post Helpful? 0
  • +
  • -

#3 jeremejazz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 48
  • Joined: 23-April 10

Posted 28 December 2010 - 06:29 PM

View Postmittu.thefire, on 28 December 2010 - 03:56 AM, said:

but when i executed ur 1st program
i got the error as " Exception in thread "main" java.lang.NoSuchMethodError: main "


please help me



Sir I believe there might be something incomplete with your code especially on the declaration of your main method.. did you correctly declared it as public static void main(String args[])?

this message usually comes out when there is something wrong with that
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Daniel*


Reputation:

Posted 13 February 2011 - 10:13 AM

You need to call
System.exit(0);


You may add an action listener like this:
win.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent evt) {
        System.exit(0);
    }
});


A complete tutorial on this is available here:

<Removed spam link>

This post has been edited by macosxnerd101: 24 February 2011 - 06:58 PM

Was This Post Helpful? 1

#5 jeremejazz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 48
  • Joined: 23-April 10

Posted 24 February 2011 - 06:45 PM

View PostDaniel, on 13 February 2011 - 10:13 AM, said:

You need to call
System.exit(0);


Is that neccessary? There is already a setDefaultCloseOperation()
where the parameters are set to EXIT_ON_CLOSE..
Was This Post Helpful? 2
  • +
  • -

#6 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 67
  • Joined: 15-April 09

Posted 27 February 2011 - 01:27 AM

Hey jereme, shouldn't it be String[] args and not String args[]? I think that's why some people were having problems.
Was This Post Helpful? 1
  • +
  • -

#7 jeremejazz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 48
  • Joined: 23-April 10

Posted 27 February 2011 - 11:44 PM

View PostMaverickDavidian, on 27 February 2011 - 01:27 AM, said:

Hey jereme, shouldn't it be String[] args and not String args[]?


They both work fine.. Their arrangement is optional... The String[] args[] just makes array declaration sound more logical --"a String array named args"
Was This Post Helpful? 0
  • +
  • -

#8 psubill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 18-September 09

Posted 19 April 2011 - 02:11 PM

Worked great for me! Can you tell how to make the window with a shape in it , like a square or circle
Was This Post Helpful? 0
  • +
  • -

#9 WinkyCode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 155
  • Joined: 23-February 11

Posted 21 April 2011 - 11:49 PM

A fine tutorial. Sadly I got an error.

I inputed your code as you specified, in other words as this:

public class FrameDemo2 extends JFrame{
/**
* Constructor - This method automatically run whenever an object of FrameDemo2 is created.
**/
public FrameDemo2(){
super("Behold, our new frame");
}

public static void main(String args[]){
FrameDemo2 myFrame = new FrameDemo2();
myFrame.setSize(300,400);
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}



Now, when running it by Command Prompt, it gives out this:

Quote

C:\SchooleJava>javac FrameDemo2.java
FrameDemo2.java:1: cannot find symbol
symbol: class JFrame
public class FrameDemo2 extends JFrame{
^
FrameDemo2.java:11: cannot find symbol
symbol : method setSize(int,int)
location: class FrameDemo2
myFrame.setSize(300,400);
^
FrameDemo2.java:12: cannot find symbol
symbol : method setLocationRelativeTo(<nulltype>)
location: class FrameDemo2
myFrame.setLocationRelativeTo(null);
^
FrameDemo2.java:13: cannot find symbol
symbol : variable EXIT_ON_CLOSE
location: class FrameDemo2
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
^
FrameDemo2.java:14: cannot find symbol
symbol : method setVisible(boolean)
location: class FrameDemo2
myFrame.setVisible(true);
^
5 errors

C:\SchooleJava>


Why is that?

Yours Truly
Winky ^^
Was This Post Helpful? 0
  • +
  • -

#10 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Posted 22 April 2011 - 11:09 AM

WinkyCode I believe you are missing your import:
 import javax.swing.*; 

This will make available all classes of the Swing package including JFrame and WindowConstants.
Was This Post Helpful? 0
  • +
  • -

#11 WinkyCode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 155
  • Joined: 23-February 11

Posted 23 April 2011 - 02:16 AM

I got a error in the Command Prompt. Got me thinking so I went through the code and found out that it should be as the OP said, like this:

public static void main(String args[]){



Someone else had suggested this solution:

public static void main(String[] args[]){



which I used and got me an error. For me, the working code is as follow:

import javax.swing.*; 

public class FrameDemo2 extends JFrame{
/**
* Constructor - This method automatically run whenever an object of FrameDemo2 is created.
**/
public FrameDemo2(){
super("Behold, our new frame");
}

public static void main(String args[]){
FrameDemo2 myFrame = new FrameDemo2();
myFrame.setSize(300,400);
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}



Thank you jeremejazz for this thread! :rockon:

This post has been edited by WinkyCode: 23 April 2011 - 02:20 AM

Was This Post Helpful? 0
  • +
  • -

#12 muffinman8641  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 121
  • Joined: 17-March 11

Posted 25 August 2011 - 12:22 PM

Hi, sorry if I'm a little late here but my code is as follows:
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class FrameDemo {
	public static void main (String[] args) {
	JFrame myFrame = new JFrame("This is my frame");
	myFrame.setSize(300,400);
	myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	myFrame.setVisible(true);
	}
}


...and it compiles fine in command prompt. However, upon attempting to run the program, command prompt gives me the following crap:
Posted Image
(The first few lines there are just me forgetting to use cd to change the directory.)
Was This Post Helpful? 0
  • +
  • -

#13 Jexpos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 02-September 11

Posted 02 September 2011 - 08:21 PM

Here is the code if you want text to appear in the frame:

Full Code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;


public class Frame extends JFrame{
	
	private JLabel text1;

public Frame() {
super("Hello.");
setLayout(new FlowLayout());

text1 = new JLabel("Sentence");
text1.setToolTipText("Magic");
add(text1);

}
public static void main(String[]  args){
Frame Frame = new Frame();
Frame.setSize(300,400);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);


}
}



"Sentence" appears on the screen and "Magic" appears when you hover your mouse over the text.
Was This Post Helpful? 1
  • +
  • -

#14 lindahart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 11

Posted 14 October 2011 - 02:45 PM

I understand mostly about the code i am having an issue compiling how do I do this I have asked my instructor but he will not answer. How do I see the frame? What do I do after i type in the code to get the end result?
Was This Post Helpful? 0
  • +
  • -

#15 Jexpos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 02-September 11

Posted 19 October 2011 - 06:24 PM

View Postlindahart, on 14 October 2011 - 02:45 PM, said:

I understand mostly about the code i am having an issue compiling how do I do this I have asked my instructor but he will not answer. How do I see the frame? What do I do after i type in the code to get the end result?


Well I somewhat understand what you are trying to say. Do you mean you don't know how to run the frame? Just run the code it looks like a triangle in a green circle (i think its green, I'm color blind)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2