@KT:Listing 1: SwingGui.java @LI: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingGui { private final int WINDOW_WIDTH=300, WINDOW_HEIGHT=200; private JFrame iMainWindow; private JPanel iInputPanel; private JLabel iLabel; private JTextArea iTextArea; private JTextField iTextField; private JButton iButton; public SwingGui() { iMainWindow = new JFrame("Swing-Example"); iMainWindow.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { exit(); } }); } private void createGUI() { iMainWindow.getContentPane().setLayout (new GridLayout(2,1,0,5)); iTextArea = new JTextArea(); createPanel(); iMainWindow.getContentPane().add(iTextArea); iMainWindow.getContentPane().add(iInputPanel); iMainWindow.setJMenuBar(createMenuBar()); } private void createPanel() { iInputPanel = new JPanel(); iInputPanel.setLayout(new FlowLayout()); iLabel = new JLabel("Text: "); iTextField = new JTextField(10); iButton = new JButton("Add"); iInputPanel.add(iLabel); iInputPanel.add(iTextField); iInputPanel.add(iButton); iButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iTextArea.append(iTextField.getText()); iTextArea.append("\n"); } } ); } private JMenuBar createMenuBar() { JMenuBar mb = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenu helpMenu = new JMenu("Help"); JMenuItem exitItem = new JMenuItem("Exit"); JMenuItem aboutItem = new JMenuItem("About..."); mb.add(fileMenu); mb.add(helpMenu); fileMenu.add(exitItem); helpMenu.add(aboutItem); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { exit(); } } ); aboutItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { about(); } } ); return mb; } private void show() { iMainWindow.setSize(WINDOW_WIDTH,WINDOW_HEIGHT); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); iMainWindow.setLocation ((screenSize.width - WINDOW_WIDTH)/2, (screenSize.height - WINDOW_HEIGHT)/2); iMainWindow.setVisible(true); iMainWindow.show(); iTextField.requestFocus(); } void exit() { iMainWindow.setVisible(false); iMainWindow.dispose(); System.exit(0); } void about() { JOptionPane.showMessageDialog(iMainWindow, "Swing-Example class: " + SwingGui.class.getName(), "About Swing-Example", JOptionPane.INFORMATION_MESSAGE); } public static void main(String argv[]) { SwingGui main = new SwingGui(); main.createGUI(); main.show(); } }