|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
This is the fifth of six 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 Now if you have read my previous tutorials and were experimental, I’m sure you have tried to add another button to your program with no luck. I said that using the add() method “adds” a object to your window. Well it does, but you have to specify where to place the object. If you don’t specify, they are placed on top of each other causing you not to see the other objects. There are a few ways to specify the locations and ill discuss two layouts here. Here is the code we created in a previous tutorial. 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.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();
}
}
Code:
interfaceFrame.setLayout(new java.awt.FlowLayout()); Code:
package cctuts;
import java.awt.event.*;
import javax.swing.*;
public class InterfaceFive implements ActionListener {
JFrame interfaceFrame;
JButton startButton, stopButton;
public InterfaceFive() {
JFrame.setDefaultLookAndFeelDecorated(true);
interfaceFrame = new JFrame("First GUI");
interfaceFrame.setSize(200,70);
interfaceFrame.setVisible(true);
interfaceFrame.setLayout(new java.awt.FlowLayout());
startButton = new JButton("Start");
startButton.addActionListener(this);
interfaceFrame.add(startButton);
stopButton = new JButton("Stop");
stopButton.addActionListener(this);
interfaceFrame.add(stopButton);
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 InterfaceFive();
}
}
![]() The second way, and my preference, to do this is to use a grid layout. It works very similar to a HTML table. To create a grid we are again going to use the addLayout() method and give it a parameter, but this time rather than using java.awt.FlowLayout() we are going to use java.awt.GridLayout() which itself requires a x and y integer dimension. So it would look like this: Code:
interfaceFrame.setLayout(new java.awt.GridLayout(1,2)); ![]() Last edited by John; 01-11-2007 at 03:21 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 - Tic-Tac-Toe | John | Java Tutorials | 25 | 02-27-2008 06:41 PM |
| ! Need urgent help ! Drawing program using cygwin | siren | C and C++ | 0 | 05-26-2007 10:51 PM |
| Java:Tutorial - Making multiple objects work differently | John | Java Tutorials | 0 | 01-11-2007 02:10 PM |
| Java:Tutorial - Adding Buttons to your Interface | John | Java Tutorials | 0 | 01-11-2007 02:03 PM |
| Java:Tutorial - Making A Window | John | Java Tutorials | 0 | 01-11-2007 02:02 PM |
| Xav | ........ | 1024.41 |
| MeTh0Dz|Reb0rn | ........ | 974.08 |
| morefood2001 | ........ | 850.04 |
| John | ........ | 841.93 |
| WingedPanther | ........ | 661.52 |
| marwex89 | ........ | 575.59 |
| Brandon W | ........ | 456.18 |
| chili5 | ........ | 292.12 |
| orjan | ........ | 187.41 |
| Steve.L | ........ | 181.88 |
Goal: 100,000 Posts
Complete: 79%