This is a discussion on Java:Tutorial - Adding Buttons to your Interface within the Java Programming forums, part of the Software Development category; Prerequisites You should have JDK installed and an editing environment you are comfortable with. Java:Tutorial - Getting Started - CodeCall Programming ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Prerequisites You should have JDK installed and an editing environment you are comfortable with. Java:Tutorial - Getting Started - CodeCall Programming Forum You should also have read my previous tutorials: 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 Since you already know how to create a window we are going to start off where we left off last time with the window code. Code: package cctuts; import javax.swing.*; public class InterfaceTwo extends JFrame{ public InterfaceTwo(){ setSize(400,400); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args){ new InterfaceTwo(); } } To add a button we add an “object” to the window. To do that we must define the object type, object name and instantiate the object (discussed in previous tutorials). In this case our button type is JButton, you can name the object what ever you want. I’ve decided to call it startButton. The JButton accepts a string paramenter and when you create the object in java the syntax of the line would look like this: Code: JButton startButton = new JButton("Start"); Which is a simple button that says “Start.” Althought we created the object, we need to add the object to the window, and the JFrame class allows us to use the add() method to do just that. add() accepts an object parameter and would look like this: Code: add(startButton); The final code looks like this: Code: package cctuts; import javax.swing.*; public class InterfaceTwo extends JFrame{ public InterfaceTwo(){ setSize(400,400); JButton startButton = new JButton("Start"); add(startButton); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args){ new InterfaceTwo(); } } |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java:Tutorial - A better looking GUI | pranky | Java Programming | 2 | 03-08-2007 12:34 PM |
| 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 |