import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JRadioButtonMenuItem; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JRadioButton; import javax.swing.JTextArea; import javax.swing.ButtonGroup; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public final class MenuExample extends JPanel { private static final int WINDOW_WIDTH = 640; private static final int WINDOW_HEIGHT = 480; private static final int PENDING_NOTHING = 0; private static final int PENDING_SQUARE = 1; private static final int PENDING_CIRCLE = 2; private static final int SHAPE_WIDTH = 50; private static final int SHAPE_HEIGHT = 50; private Color backColor = Color.white; private Color foreColor = Color.black; private int pendingShape = PENDING_NOTHING; private int pendingX, pendingY; public static void main(final String[] args) { JFrame frame = new JFrame("Menu Example"); MenuExample panel = new MenuExample(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); frame.setLocationRelativeTo(null); frame.setContentPane(panel); frame.setJMenuBar(panel.getMenuBar()); panel.initWidgets(); frame.setVisible(true); } public JMenuBar getMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); JMenuItem clearItem = new JMenuItem("Clear", KeyEvent.VK_L); fileMenu.add(clearItem); clearItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backColor = Color.black; repaint(); } }); fileMenu.addSeparator(); JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(exitItem); menuBar.add(fileMenu); JMenu colorMenu = new JMenu("Color"); colorMenu.setMnemonic(KeyEvent.VK_C); JMenuItem greenItem = new JMenuItem("Green", KeyEvent.VK_G); greenItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backColor = Color.green; repaint(); } }); colorMenu.add(greenItem); JMenuItem blueItem = new JMenuItem("Blue", KeyEvent.VK_B); blueItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backColor = Color.blue; repaint(); } }); colorMenu.add(blueItem); JMenuItem redItem = new JMenuItem("Red", KeyEvent.VK_R); redItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backColor = Color.red; repaint(); } }); colorMenu.add(redItem); menuBar.add(colorMenu); return menuBar; } public void initWidgets() { JLabel userNameLabel = new JLabel("Enter your username"); this.add(userNameLabel); JTextField userNameText = new JTextField(); userNameText.setColumns(8); this.add(userNameText); JLabel passwordLabel = new JLabel("Enter password"); this.add(passwordLabel); final JPasswordField passwordText = new JPasswordField(); passwordText.setColumns(8); final JButton submitButton = new JButton("Submit"); submitButton.setEnabled(false); passwordText.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { int length = passwordText.getPassword().length; if (length < 8) { passwordText.setBackground(Color.red); submitButton.setEnabled(false); } else { passwordText.setBackground(Color.white); submitButton.setEnabled(true); } } }); this.add(passwordText); submitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(submitButton, "You are now logged in."); } }); this.add(submitButton); final JPopupMenu pop = new JPopupMenu(); JMenu drawMenu = new JMenu("Draw"); JMenuItem drawCircle = new JMenuItem("Circle"); drawCircle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pendingShape = PENDING_CIRCLE; repaint(); } }); drawMenu.add(drawCircle); JMenuItem drawSquare = new JMenuItem("Square"); drawSquare.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pendingShape = PENDING_SQUARE; repaint(); } }); drawMenu.add(drawSquare); pop.add(drawMenu); JMenu colorMenu = new JMenu("Pen Color"); ButtonGroup colors = new ButtonGroup(); final JRadioButtonMenuItem blackCheck = new JRadioButtonMenuItem("Black", true); colorMenu.add(blackCheck); colors.add(blackCheck); final JRadioButtonMenuItem blueCheck = new JRadioButtonMenuItem("Blue", false); blackCheck.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { foreColor = Color.black; } }); blueCheck.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { foreColor = Color.blue; } }); colorMenu.add(blueCheck); colors.add(blueCheck); pop.add(colorMenu); this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { pendingX = e.getX(); pendingY = e.getY(); pop.show(MenuExample.this, pendingX, pendingY); } } }); ButtonGroup radios = new ButtonGroup(); JRadioButton option1 = new JRadioButton("Option 1"); radios.add(option1); this.add(option1); JRadioButton option2 = new JRadioButton("Option 2"); radios.add(option2); this.add(option2); JRadioButton option3 = new JRadioButton("Option 3"); radios.add(option3); this.add(option3); final JTextArea area = new JTextArea(5, 5); this.add(area); option1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.setText("Radio 1 selected!"); } }); } public void paint(Graphics g) { super.paint(g); g.setColor(backColor); //g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(foreColor); if (pendingShape == PENDING_CIRCLE) { g.drawOval(pendingX, pendingY, SHAPE_WIDTH, SHAPE_HEIGHT); } else if (pendingShape == PENDING_SQUARE) { g.drawRect(pendingX, pendingY, SHAPE_WIDTH, SHAPE_HEIGHT); } pendingShape = PENDING_NOTHING; } }