You want to write an application with a graphical user interface: The application may need to access local resources such as files on the local machine (applets are generally restricted from doing this). Or, it may need a menubar or call the file open/save dialog box.
![]() Image of Running Application |
The example simply places text entered in the bottom field into the top field.
This example consists of two classes. They would normally be placed in two files.
public class Main { public static void main(String[] args) { ListFrame list = new ListFrame(); list.show(); } }
import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.TextArea; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowListener; import java.awt.event.WindowEvent; import java.awt.FlowLayout; /** Display a textfield and a textarea. When "enter" is hit when in the textfield, transfer the text to a new line in the textarea. @author Byron Weber Becker @version 1.0 10-June-1999 */ public class ListFrame extends JFrame { private JTextField item = new JTextField(20); private JTextArea list = new JTextArea(10, 20); public ListFrame() { this.getContentFrame().setLayout(new FlowLayout()); this.getContentFrame().add(list); this.getContentFrame().add(item); item.addActionListener(new InputListener()); this.addWindowListener(new WindowCloser()); this.setSize(200, 300); item.requestFocus(); } private class InputListener implements ActionListener { public void actionPerformed(ActionEvent evt) { list.append(item.getText() + "\n"); item.selectAll(); } } private class WindowCloser implements WindowListener { //We must implement all methods in the WindowListenter interface // even if we provide no code in their implementation. public void windowActivated(WindowEvent evt) {} public void windowDeactivated(WindowEvent evt) {} public void windowOpened(WindowEvent evt) {} public void windowClosed(WindowEvent evt) {} public void windowDeiconified(WindowEvent evt) {} public void windowIconified(WindowEvent evt) {} public void windowClosing(WindowEvent evt) { System.exit(0); } } }
public class <MainClassName> { public static void main(String[] args) { <FrameClassName> <VarName> = new <FrameClassName>(<OptionalArguments>); <VarName>.show(); } }
import javax.swing.JFrame; import java.awt.event.WindowListener; import java.awt.event.WindowEvent; import java.awt.FlowLayout; public class <FrameClassName> extends JFrame { <InstanceVariables> public <FrameClassName>() { this.getContentPane().setLayout(new FlowLayout()); // defaults to BorderLayout <AddWidgetsToFrame> this.addWindowListener(new WindowCloser()); this.setSize(<width>, <height>); } private class WindowCloser extends WindowListener { public void windowActivated(WindowEvent evt) { <WhatToDoWhenWindowIsActivated> } public void windowDeactivated(WindowEvent evt) { ... } public void windowOpened(WindowEvent evt) { ... } public void windowClosed(WindowEvent evt) { ... } public void windowDiconified(WindowEvent evt) { ... } public void windowIconified(WindowEvent evt) { ... } public void windowClosing(WindowEvent evt) { <WhatToDoWhenCloseBoxClicked> } } }