package demo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JSeparator; import javax.swing.border.BevelBorder; import javax.swing.border.Border; import javax.swing.plaf.basic.BasicBorders; @SuppressWarnings("serial") public class AdvancedGUIApp extends JFrame { // JPanels used in the UI JPanel jpSouth; JPanel jpEast; JPanel jpCenter; /** * @param args not used */ public static void main(String[] args) { new AdvancedGUIApp(); } // ctor - inits the UI, etc private AdvancedGUIApp() { initUI(); // init the main JFrame createMenu(); // put menus on it //TODO: add radio buttons and mouse listeners setVisible(true); // make it visible } // main UI JFrame initialization - size, title, layout, etc private void initUI() { // create the containing window and set its size etc setTitle("SE1021 Advanced GUI Demo app"); setSize(400, 300); setLocation(400, 400); setResizable(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Configure the content pane for a border layout Container canvas = getContentPane(); canvas.setLayout(new BorderLayout(10,10)); // create some panels to put in the various sections of the layout jpSouth = new JPanel( new FlowLayout(FlowLayout.LEFT)); jpSouth.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); jpSouth.setPreferredSize(new Dimension(100, 50)); jpEast = new JPanel( new FlowLayout(FlowLayout.LEFT)); jpEast.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); jpEast.setPreferredSize(new Dimension(100, 50)); jpCenter = new JPanel(); jpCenter.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); // add the panels to the layout canvas.add(jpSouth, BorderLayout.SOUTH); canvas.add(jpEast, BorderLayout.EAST); canvas.add(jpCenter, BorderLayout.CENTER); } /** * creates the menu and places it in the JFrame. */ private void createMenu() { // create the nested event handler that will respond to menu events MenuHandler mh = new MenuHandler(); // JMenuBar holds menus JMenuBar menuBar = new JMenuBar(); //File JMenu; holds File menu items JMenu fileMenu = new JMenu("File"); JMenuItem jmiOpen = new JMenuItem("Open..."); jmiOpen.setActionCommand("Open"); // redefine to remove "..." jmiOpen.addActionListener(mh); // add the JMenuItem to the JMenu fileMenu.add(jmiOpen); // now add the JMenu to the JMenuBar menuBar.add(fileMenu); //TODO: add more Options and Tools menus // finally, add the JMenuBar to the JFrame setJMenuBar(menuBar); } /** * Adds radio buttons to the window * */ public void createRadioButtons() { // create the event handler that will respond to button events RadioButtonHandler rbh = new RadioButtonHandler(); JRadioButton rb1 = new JRadioButton("One"); rb1.addActionListener(rbh); JRadioButton rb2 = new JRadioButton("Two"); rb2.addActionListener(rbh); JRadioButton rb3 = new JRadioButton("Three"); rb3.addActionListener(rbh); //TODO: Add a ButtonGroup to manage the automatic // selection/deselection of radio buttons. Note: can be abused // to do the same for checkboxes // place the buttons in the East panel, with an ugly border Border border = new BasicBorders.RadioButtonBorder(Color.black, Color.blue, Color.yellow, Color.green); jpEast.setBorder(border); jpEast.add(rb1); jpEast.add(rb2); jpEast.add(rb3); } /** * sets up event handling for the mouse */ private void createMouseListeners() { MouseHandler mh = new MouseHandler(); jpCenter.addMouseListener( mh ); // listen for mouse presses and clicks //TODO: add a mouse motion listener } /** * implementation of the ActionListener interface * Handles action events from menus only */ private class MenuHandler implements ActionListener { /** * This is the method called by the JVM when action events occur */ public void actionPerformed(ActionEvent event) { System.out.println( "The "+ event.getActionCommand() + " menu item was selected."); } } /** * implementation of the ActionListener interface * Handles action events from radio buttons only */ private class RadioButtonHandler implements ActionListener { /** * This is the method called by the JVM when action events occur */ public void actionPerformed(ActionEvent event) { System.out.println( "The "+ event.getActionCommand() + " radio button was selected."); } } /** * implementation of the MouseListener and MouseMotionListener interfaces * Handles Mouse Events from the mouse */ private class MouseHandler implements MouseListener { //TODO: add MouseMotionListener /** * These are methods called by the JVM when mouse events occur */ //MouseListener methods @Override public void mouseClicked(MouseEvent e) { System.out.println("mouseClicked at " + e.getX() + "," + e.getY() ); } @Override public void mouseEntered(MouseEvent e) { System.out.println("mouseEntered at " + e.getX() + "," + e.getY() ); } @Override public void mouseExited(MouseEvent e) { System.out.println("mouseExited at " + e.getX() + "," + e.getY() ); } @Override public void mousePressed(MouseEvent e) { System.out.println("mousePressed at " + e.getX() + "," + e.getY() ); } @Override public void mouseReleased(MouseEvent e) { System.out.println("mouseReleased at " + e.getX() + "," + e.getY() ); } } }