Wednesday, February 20, 2008

Making a GUI in JAVA


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)
Now just follow these steps:
  1. 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
  2. Import some important files. import java.awt.*; import java.awt.event.*; import javax.swing.*;
  3. Create a new class, extend it to ActionListener and create the main Method. public class HelloGui implements ActionListener { public static void main(String[] args) { } }
  4. Under the main method create an instance of the class itself. HelloGui myProgram = new HelloGui();
  5. 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);
  6. Create an instance of the JPanel JPanel contentPanel = new JPanel(new GridLayout(0, 1));
  7. Add the created buttons in the content panel and create a window/frame. contentPanel.add(ladiesButton); contentPanel.add(gentsButton);
  8. Create an instance of the JFrame to give your GUI Program a window. JFrame mainWindow = new JFrame("Hello Java GUI Program by Rooseveltrp.com");
  9. Add the content pane in the window. Rebuild the window and then display it. mainWindow.getContentPane().add(contentPanel); mainWindow.pack(); mainWindow.setVisible(true);
  10. Create a new method called public void actionPerformed(ActionEvent e).
  11. 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!"); } }
  12. 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!"); } } }
  13. Program should output the following results.

No comments:

Post a Comment