IT Community - Software Programming, Web Development and Technical Support

Java:Tutorial - A better looking GUI

This is a discussion on Java:Tutorial - A better looking GUI within the Java Programming forums, part of the Software Development category; rerequisites This tutorial is moderately difficult. If you know the Java basics please refer to my previous tutorials. If you ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-23-2007, 10:05 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
Default Java:Tutorial - A better looking GUI

rerequisites
This tutorial is moderately difficult. If you know the Java basics please refer to my previous tutorials. If you do know the Java basics you should be familiar with my previous tutorials on GUI's. To read my tutorials please refer to my INDEX

The Idea
In order for your program to be attractive, the user must be able to easily navigate through your program. By creating a GUI the user is presented with all the features of the program in a clear and coherent manner. Adding buttons to your interface allow for user interaction which is the reason for a GUI.

Solution
Although the idea introduced in the tutorial isn’t very big and doesn’t deserve to be its own tutorial, I changed the basic code we’ve been using so I felt it was worth while to point out a different method to create a window. It uses basic ideas I’ve explained in previous tutorials, but since this is how I will be creating most of my windows from now on, you should observe the changes and try to understand them.

This code does the exact same thing on the outside as a pervious tutorial I made, but there are significant changes. Lets take a look at the code:
Code:

package cctuts;

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

public class InterfaceFour implements ActionListener {
JFrame interfaceFrame;
JButton startButton;

public InterfaceFour() {
interfaceFrame = new JFrame("First GUI");
interfaceFrame.setSize(200,70);
interfaceFrame.setVisible(true);

startButton = new JButton("Start");
startButton.addActionListener(this);
interfaceFrame.add(startButton);

interfaceFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
interfaceFrame.setVisible(true);
}

public void actionPerformed(ActionEvent a) {
for(int i = 0; true; i++){
System.out.println(i);
}
}

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

As you see, rather than extending JFrame I’ve made an object called interfaceFrame. Essentially the code is exactly the same, but all the JFrame methods you want to use have to be called on the specific object, on our case interfaceFrame, and you do that by using the period. If you noticed, JFrame() accepts a string parameter where you can add the title of the application; I named it “First GUI.” Moreover I defined the object types as instance variables so they can be referenced in other parts of the code.

The other tidbit I wanted to point out in this tutorial is you can make your program “look” better by setting the default look and feel by adding this code
Code:

JFrame.setDefaultLookAndFeelDecorated(true);

This final code is important to understand because my future tutorials will be more complex and feed off this idea.

Code:

package cctuts;

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

public class InterfaceFour implements ActionListener {
JFrame interfaceFrame;
JButton startButton;

public InterfaceFour() {
JFrame.setDefaultLookAndFeelDecorated(true);
interfaceFrame = new JFrame("First GUI");
interfaceFrame.setSize(200,70);
interfaceFrame.setVisible(true);

startButton = new JButton("Start");
startButton.addActionListener(this);
interfaceFrame.add(startButton);

interfaceFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
interfaceFrame.setVisible(true);
}

public void actionPerformed(ActionEvent a) {
for(int i = 0; true; i++){
System.out.println(i);
}
}

public static void main(String[] args) {
new InterfaceFour();
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-05-2007, 10:52 PM
connect2sathya connect2sathya is offline
D-Web Trainee
 
Join Date: Feb 2007
Posts: 14
connect2sathya is on a distinguished road
Thumbs down Re: Java:Tutorial - A better looking GUI

hi friend , u cant add straight away add components to JFrames ... you have to use getContentPane() method...
ie.., <frameobj>.getContentPane().add(<component>);






Quote:
Originally Posted by pranky View Post
rerequisites
This tutorial is moderately difficult. If you know the Java basics please refer to my previous tutorials. If you do know the Java basics you should be familiar with my previous tutorials on GUI's. To read my tutorials please refer to my INDEX

The Idea
In order for your program to be attractive, the user must be able to easily navigate through your program. By creating a GUI the user is presented with all the features of the program in a clear and coherent manner. Adding buttons to your interface allow for user interaction which is the reason for a GUI.

Solution
Although the idea introduced in the tutorial isn’t very big and doesn’t deserve to be its own tutorial, I changed the basic code we’ve been using so I felt it was worth while to point out a different method to create a window. It uses basic ideas I’ve explained in previous tutorials, but since this is how I will be creating most of my windows from now on, you should observe the changes and try to understand them.

This code does the exact same thing on the outside as a pervious tutorial I made, but there are significant changes. Lets take a look at the code:
Code:

package cctuts;

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

public class InterfaceFour implements ActionListener {
JFrame interfaceFrame;
JButton startButton;

public InterfaceFour() {
interfaceFrame = new JFrame("First GUI");
interfaceFrame.setSize(200,70);
interfaceFrame.setVisible(true);

startButton = new JButton("Start");
startButton.addActionListener(this);
interfaceFrame.add(startButton);

interfaceFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
interfaceFrame.setVisible(true);
}

public void actionPerformed(ActionEvent a) {
for(int i = 0; true; i++){
System.out.println(i);
}
}

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

As you see, rather than extending JFrame I’ve made an object called interfaceFrame. Essentially the code is exactly the same, but all the JFrame methods you want to use have to be called on the specific object, on our case interfaceFrame, and you do that by using the period. If you noticed, JFrame() accepts a string parameter where you can add the title of the application; I named it “First GUI.” Moreover I defined the object types as instance variables so they can be referenced in other parts of the code.

The other tidbit I wanted to point out in this tutorial is you can make your program “look” better by setting the default look and feel by adding this code
Code:

JFrame.setDefaultLookAndFeelDecorated(true);

This final code is important to understand because my future tutorials will be more complex and feed off this idea.

Code:

package cctuts;

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

public class InterfaceFour implements ActionListener {
JFrame interfaceFrame;
JButton startButton;

public InterfaceFour() {
JFrame.setDefaultLookAndFeelDecorated(true);
interfaceFrame = new JFrame("First GUI");
interfaceFrame.setSize(200,70);
interfaceFrame.setVisible(true);

startButton = new JButton("Start");
startButton.addActionListener(this);
interfaceFrame.add(startButton);

interfaceFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
interfaceFrame.setVisible(true);
}

public void actionPerformed(ActionEvent a) {
for(int i = 0; true; i++){
System.out.println(i);
}
}

public static void main(String[] args) {
new InterfaceFour();
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-08-2007, 12:34 PM
aicram aicram is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 11
aicram is on a distinguished road
Default Re: Java:Tutorial - A better looking GUI

This is just out of curiosity. Does anyone know why java is called java? Thanks.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java:Tutorial - Access Modifiers pranky Java Programming 1 05-08-2007 05:55 AM
Java:Tutorial - The Variable pranky Java Programming 0 02-24-2007 12:59 AM
Java:Tutorial - Arrays pranky Java Programming 0 02-24-2007 12:54 AM
Java:Tutorial - Getting Started pranky Java Programming 0 02-24-2007 12:48 AM
Java:Tutorial - Tic-Tac-Toe pranky Java Programming 6 02-23-2007 09:48 AM


All times are GMT -7. The time now is 10:58 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0