bsc Logo

Let's get started with a GUI application!

  1. Open Eclipse
  2. Right-Click on your edu.buffalostate.cis425.fa10.exercises..your-username
  3. Choose: File -> new -> other
  4. Choose: WindowBuilder -> Swing Designer -> JFrame
  5. In the Name: textbox type: HelloGUI
  6. Check the public static void main(String[] args) checkbox
  7. Click: Finish
  8. Click the Design tab
  9. Click on the JPanel icon
  10. In the Frame -> Properties window, change the Title to: Hello World App
  11. Move your cursor into the Frame and click the mouse button to place the panel inside the frame
    wb window
  12. In the Properties window, change the Layout drop-down to: Absolute
  13. Click on the JLabel icon
  14. Move your cursor into the Frame and click the mouse button to place the label inside the panel
  15. Change the new Label to: Name:
  16. Click on the JTextField icon
  17. Move your cursor into the Frame and click the mouse button to place the textbox next to the Name: label
  18. In the Properties window, change the Variable to: nameStr
  19. Click on the JTextField icon
  20. Move your cursor into the Frame and click the mouse button to place the textbox below the Name: label and textbox
  21. In the Properties window, change the Variable to: outStr
  22. In the Properties window, change the Columns to: 30
  23. Click on the JButton icon
  24. Move your cursor into the Frame and click the mouse button to place the button next to the Name: label and textbox
  25. In the Properties window, change the variable to: clickBtn
  26. In the Properties window, change the text to: Click Me!
  27. Right-click on the JButton next to the Name: textfield
  28. Choose: Add Event Handler -> Action -> Action Performed
  29. When the Source window opens, after this line:
    public void actionPerformed(ActionEvent e) {
    add this line:
    outStr.setText("Hello " + nameStr.getText() + "!");
  30. Click the Run icon
  31. Enter your name
  32. Click the button
    Hello window

HelloGUI.java Template (Java)

package edu.buffalostate.cis425.fa10.exercises..put-your-username-here;

/**
 * File: HelloGUI.java
 *
 * Write a program that creates a GUI window to display a message.
 *
 * Fill in the blanks to make the program work 
 *
 */
/**
 *
 * @author put-your-name-here 
 *
 */

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class HelloGUI extends JFrame {
  
  private JPanel contentPane;
  private JTextField nameStr;
  private JTextField outStr;
  
  /**
   *
   * Launch the application.
   *
   */
  public static void main( String args[] ) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          HelloGUI frame = new HelloGUI();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }  
  
  /**
   *
   * Create the frame.
   *
   */
  public HelloGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    
    JPanel panel = new JPanel();
    contentPane.add(panel , BorderLayout.CENTER);
    panel.setLayout(null);
    
    JLabel lblName  = new JLabel("Name:");
    lblName .setBounds(34, 25, 46, 14);
    panel.add(lblName );
    
    nameStr = new JTextField();
    nameStr.setBounds(76, 22, 86, 20);
    panel.add(nameStr);
    nameStr.setColumns(10);
    
    outStr = new JTextField();
    outStr.setBounds(34, 65, 86, 20);
    panel.add(outStr);
    outStr.setColumns(30);
    
    JButton clickBtn = new JButton("Click Me!");
    clickBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        outStr.setText("Hello " + nameStr.getText() + "!");
      }
    });
    clickBtn.setBounds(172, 21, 89, 23);
    panel.add(clickBtn);
  }
}

HelloGui Class Output

Hello Gui Screenshot