This is a discussion on Java:Tutorial - Making multiple objects work differently within the Java Programming forums, part of the Software Development category; Prerequisites You should know how to create a window in Java. LINK GOES HERE You should know how to add ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| Prerequisites You should know how to create a window in Java. LINK GOES HERE You should know how to add a button to the window in Java. LINK GOES HERE 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. In pervious tutorials, our two buttons did the same thing, but real applications have different buttons that perform different functions. Solution Lets start with a shell we’ve formulated in previous tutorials: Code: package cctuts; import java.awt.event.*; import javax.swing.*; public class InterfaceSix implements ActionListener{ JFrame interfaceFrame; JButton startButton, stopButton; public InterfaceSix(){ JFrame.setDefaultLookAndFeelDecorated(true); interfaceFrame = new JFrame("First GUI"); interfaceFrame.setSize(200,70); interfaceFrame.setLayout(new java.awt.GridLayout(1,2)); startButton = new JButton("Start"); startButton.addActionListener(this); interfaceFrame.add(startButton); stopButton = new JButton("Stop"); stopButton.addActionListener(this); interfaceFrame.add(stopButton); interfaceFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE); interfaceFrame.setVisible(true); } public void actionPerformed(ActionEvent a) { } public static void main(String[] args){ new InterfaceSix(); } } Now just to recap what we have here: a class that makes a visible window 200px by 70px. The window looks pretty because we set the default look and feel. The window contains two button objects which are positioned using a grid layout. Each button as an action listener, so when the button is pressed the actionPerformed method is invoked. If anything I just said doesn’t make sense, you should read my previous tutorials. In the actionPerformed method we need to determine which button is pressed so we can have our program act accordingly. We do this by using the a.getSource() method, which tells us which button is being pressed. We can then perform simple logical operations to tell our program what to do. For example if the startButton is pressed, a.getSource() == startButton. We then can say something like: Code: if(a.getSource() == startButton){ System.out.println(“The start button was pushed…”); Incorporated into a full class it would look something like this: Code: package cctuts; import java.awt.event.*; import javax.swing.*; public class InterfaceSix implements ActionListener{ JFrame interfaceFrame; JButton startButton, stopButton; public InterfaceSix(){ JFrame.setDefaultLookAndFeelDecorated(true); interfaceFrame = new JFrame("First GUI"); interfaceFrame.setSize(200,70); interfaceFrame.setLayout(new java.awt.GridLayout(1,2)); startButton = new JButton("Start"); startButton.addActionListener(this); interfaceFrame.add(startButton); stopButton = new JButton("Stop"); stopButton.addActionListener(this); interfaceFrame.add(stopButton); interfaceFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE); interfaceFrame.setVisible(true); } public void actionPerformed(ActionEvent a) { if(a.getSource() == startButton){ System.out.println("Start button was pressed..."); } if(a.getSource() == stopButton){ System.out.println("Stop button was pressed..."); } } public static void main(String[] args){ new InterfaceSix(); } } |
|
#2
| |||
| |||
| Java is a programming language originally developed by Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. _______________________ Keyword Research Georgia Health Insurance |
|
#3
| |||
| |||
| Hello friends Thanks for sharing such knowledgeable things. But please tell me what is the difference between java programming and core java.. Please reply me if anyone knows Thanks |
|
#4
| |||
| |||
| Get a huge variety of Ed Hardy clothing, NIKE, Adidas, Christian Audigier and a lot more brands at a discounted price and with worldwide free shipping. So visit Ed Hardy Clothing|Ed Hardy Hats|Hip Hop|Christian Audigier|Timberland Boots - Raining Hollywood and start ordering today. |
![]() |
| 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 11:34 AM |
| Java:Tutorial - The Variable | pranky | Java Programming | 0 | 02-23-2007 11:59 PM |
| Java:Tutorial - Arrays | pranky | Java Programming | 0 | 02-23-2007 11:54 PM |
| Java:Tutorial - Make Your Button Work | pranky | Java Programming | 0 | 02-23-2007 09:06 AM |
| Java:Tutorial - Tic-Tac-Toe | pranky | Java Programming | 6 | 02-23-2007 08:48 AM |
Our Partners |