Here in this tutorial you will be showed how to make a GUI in JAVA ...i.e.. how to make frames, panels, buttons and how to display dialog boxes.
The 2 most important things you will need :--
- Basic knowledge of JAVA.
- A JAVA compiler (like BlueJ or Eclipse)
- A JAVA GUI Program should be designed like the diagram below (at least it's what I think): - main() - Frame -- Content Panel --- Buttons --- Text Areas --- Menus --- and so on
- Import some important files. import java.awt.*; import java.awt.event.*; import javax.swing.*;
- Create a new class, extend it to ActionListener and create the main Method. public class HelloGui implements ActionListener { public static void main(String[] args) { } }
- Under the main method create an instance of the class itself. HelloGui myProgram = new HelloGui();
- Create buttons and attach action listeners. JButton ladiesButton = new JButton("Ladies"); ladiesButton.setActionCommand("clickfromLadies"); ladiesButton.addActionListener(myProgram); JButton gentsButton = new JButton("Gents"); gentsButton.setActionCommand("clickfromGents"); gentsButton.addActionListener(myProgram);
- Create an instance of the JPanel JPanel contentPanel = new JPanel(new GridLayout(0, 1));
- Add the created buttons in the content panel and create a window/frame. contentPanel.add(ladiesButton); contentPanel.add(gentsButton);
- Create an instance of the JFrame to give your GUI Program a window. JFrame mainWindow = new JFrame("Hello Java GUI Program by Rooseveltrp.com");
- Add the content pane in the window. Rebuild the window and then display it. mainWindow.getContentPane().add(contentPanel); mainWindow.pack(); mainWindow.setVisible(true);
- Create a new method called public void actionPerformed(ActionEvent e).
- Inside the method actionPerformed enter the following codes to control the clicks from your button. public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("clickfromLadies")) { JOptionPane.showMessageDialog(null, "Roosevelt Loves Ladies!"); } else if(e.getActionCommand().equals("clickfromGents")) { JOptionPane.showMessageDialog(null, "Honestly, Roosevelt doesn't care about men!"); } }
- If you've done everything well your codes will look just like below: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HelloGui implements ActionListener { public static void main(String[] args) { HelloGui myProgram = new HelloGui(); JButton ladiesButton = new JButton("Ladies"); ladiesButton.setActionCommand("clickfromLadies"); ladiesButton.addActionListener(myProgram); JButton gentsButton = new JButton("Gents"); gentsButton.setActionCommand("clickfromGents"); gentsButton.addActionListener(myProgram); JPanel contentPanel = new JPanel(new GridLayout(0, 1)); contentPanel.add(ladiesButton); contentPanel.add(gentsButton); JFrame mainWindow = new JFrame("Hello Java GUI Program by Rooseveltrp.com"); mainWindow.getContentPane().add(contentPanel); mainWindow.pack(); mainWindow.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("clickfromLadies")) { JOptionPane.showMessageDialog(null, "Roosevelt Loves Ladies!"); } else if(e.getActionCommand().equals("clickfromGents")) { JOptionPane.showMessageDialog(null, "Honestly, Roosevelt doesn't care about men!"); } } }
- Program should output the following results.
No comments:
Post a Comment