|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
This is the fourth of a few tutorials that will show you how to create graphical user interfaces using java.
Prerequisites 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 cl*** 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.EXIT_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();
}
}
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); Code:
package cctuts;
import java.awt.event.*;
import javax.swing.*;
public cl*** 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.EXIT_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();
}
}
![]() Last edited by John; 01-11-2007 at 03:11 PM. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java:Tutorial - Making multiple objects work differently | John | Java Tutorials | 0 | 01-11-2007 02:10 PM |
| Java:Tutorial - Adding more objects to your window | John | Java Tutorials | 0 | 01-11-2007 02:09 PM |
| Java:Tutorial - Make Your Button Work | John | Java Tutorials | 0 | 01-11-2007 02:04 PM |
| Java:Tutorial - Adding Buttons to your Interface | John | Java Tutorials | 0 | 01-11-2007 02:03 PM |
| Xav | ........ | 1322.18 |
| MeTh0Dz|Reb0rn | ........ | 1053.7 |
| morefood2001 | ........ | 879.43 |
| John | ........ | 877.37 |
| marwex89 | ........ | 869.98 |
| WingedPanther | ........ | 830.24 |
| Brandon W | ........ | 735.07 |
| chili5 | ........ | 309.39 |
| Steve.L | ........ | 239.84 |
| dcs | ........ | 216.02 |
Goal: 100,000 Posts
Complete: 82%