package Lect39GUIs; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class SwingExampleManualLayout { private final static int WIDTH = 500, HEIGHT = 400; public SwingExampleManualLayout() { JFrame frame = buildFrame(); buildGUI(frame); displayFrame(frame); } public JFrame buildFrame() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) {} // Create a top-level window to put the picture in JFrame frame; frame = new JFrame("Swing Example"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); return frame; } public void buildGUI(JFrame frame) { JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(WIDTH, HEIGHT)); panel.setBackground(Color.WHITE); panel.setLayout(null); JButton button = new JButton("Do it"); button.setBounds(50, 50, 100, 20); panel.add(button); JLabel label = new JLabel(); label.setBounds(50, 75, 100, 20); panel.add(label); JTextArea textArea = new JTextArea(); textArea.setLineWrap(true); textArea.setBorder(new EtchedBorder()); textArea.setBounds(50, 100, 100, 200); panel.add(textArea); button.addActionListener(new DoIt(label)); frame.setContentPane(panel); } public void displayFrame(JFrame frame) { frame.setLocation(100, 100); frame.pack(); frame.show(); } static public void main(String[] args) { new SwingExampleManualLayout(); // Note that Swing starts a new thread, so the program continues // even though this thread exits System.out.println("This line executes and main exits, but program keeps running"); } } class DoIt implements ActionListener { private JLabel label; private int actionCount = 0; public DoIt(JLabel label) { this.label = label; } public void actionPerformed(ActionEvent e) { label.setText("It: " + actionCount++); } }