Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 7 ■ WRITING A USER INTERFACE

menuBar.add(file);
frame.setJMenuBar(menuBar);
}


private void createAndShowGUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(200, 200));
addMenu(frame);
frame.pack();
frame.setVisible(true);
}


public static void main(String[] args) {
SwingDemo swingDemo = new SwingDemo();
swingDemo.createAndShowGUI();
}


@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Exit")) {
System.exit(0);
}
}
}


Again, before we discuss how and why it works, let's see what it looks like. Figure 7-2 shows our
additions.


Figure 7-2. SwingDemo with a menu

Free download pdf