Right-Click on your edu.buffalostate.cis425.fa10.exercises..your-username
Choose: File -> new -> other
Choose: WindowBuilder -> Swing Designer -> JFrame
In the Name: textbox type: HelloGUI
Check the public static void main(String[] args) checkbox
Click: Finish
Click the Design tab
Click on the JPanel icon
In the Frame -> Properties window, change the Title to: Hello World App
Move your cursor into the Frame and click the mouse button to place the panel inside the frame
In the Properties window, change the Layout drop-down to: Absolute
Click on the JLabel icon
Move your cursor into the Frame and click the mouse button to place the label inside the panel
Change the new Label to: Name:
Click on the JTextField icon
Move your cursor into the Frame and click the mouse button to place the textbox next to the Name: label
In the Properties window, change the Variable to: nameStr
Click on the JTextField icon
Move your cursor into the Frame and click the mouse button to place the textbox below the Name: label and textbox
In the Properties window, change the Variable to: outStr
In the Properties window, change the Columns to: 30
Click on the JButton icon
Move your cursor into the Frame and click the mouse button to place the button next to the Name: label and textbox
In the Properties window, change the variable to: clickBtn
In the Properties window, change the text to: Click Me!
Right-click on the JButton next to the Name: textfield
Choose: Add Event Handler -> Action -> Action Performed
When the Source window opens, after this line: public void actionPerformed(ActionEvent e) {
add this line: outStr.setText("Hello " + nameStr.getText() + "!");
Click the Run icon
Enter your name
Click the button
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
*
*/
/**
*
* @authorput-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);
}
}