Results 1 to 3 of 3
  1. #1
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Exclamation JOptionPane Menu Issue(non static in static ) (prompt help needed due in 7 hours)

    I tried setting up setting up a menu with 5 options MENU with jOptionPane Headings: "Play", "Instructions", "Credits", "Jukebox", "Quit/self destruct"

    //for attempt 1 I have nonstatic stuff in main class -- is it a simple fix?

    Java Code:
    import javax.swing.*;import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    
    /**
     * JOptionPane showInputDialog example #3.
     * Using a combo box in an input dialog (showInputDialog).
     * 
     * @author alvin alexander, http://alvinalexander.com
     */
    public class Men
    {
      public static final String[] pizzas = { "Play", "Credits", "Instructions", "Quit" };
      int x;
     // Runner r= new Runner();
    
    
     public static void main(String[] args)
     {
        String[] options = new String[] {"PLAY", "INSTRUCTIONS", "CREDITS","JUKEBOX", "SELF DESTRUCT"};
        int response = JOptionPane.showOptionDialog(null, "Please Selection an Option", "Mr. Guerra's Garage Band!",
            JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, options, options[0]);
             if(response==0)
    		a();
    		else if(response==1)
    		b();
    		else if(response==2)
    		c();
    		else if(response==3)
    		d();
     }
    	public static void a()
    	{
    		JOptionPane.showMessageDialog(this, "You have 5 seconds to hit run, once the grid shows up ",
    					"Simple Dialog", JOptionPane.INFORMATION_MESSAGE);
    	}
    	public static void b()
    	{
    		JOptionPane.showMessageDialog(this, "Instructions",
    					"Simple Dialog", JOptionPane.INFORMATION_MESSAGE);
    	}
    	public static void c()
    	{
    	JOptionPane.showMessageDialog(this, "Credits",
    					"Simple Dialog", JOptionPane.INFORMATION_MESSAGE);	
    	}
    	public static void d()
    	{
    		JOptionPane.showConfirmDialog(this, "Are you sure you want to Quit?",
    					"Question",JOptionPane.OK_CANCEL_OPTION);
    				if(x==0)
    					System.exit(0);
    				//else
    					//setVisible(false);	
    	}		
        // Where response == 0 for Yes, 1 for No, 2 for Maybe and -1 or 3 for Escape/Cancel.
    }
    Attempt 2
    //No clue What's im doing
    Java Code:
    import java.awt.*;import java.awt.event.*;
    import javax.swing.*;
    
    
    public class JRadioButtonDemo extends JPanel {
    
    
    	static JFrame frame;
    	JLabel jlbPicture;
    	RadioListener myListener = null;
    	public JRadioButtonDemo() {
    		// Create the radio buttons and assign Keyboard shortcuts using
    		// Mnemonics
    		JRadioButton jrbPlay = new JRadioButton("Play");
    		jrbPlay.setMnemonic(KeyEvent.VK_N);
    		jrbPlay.setActionCommand("Play");
    		JOptionPane.showMessageDialog(this, "You have 5 seconds to hit run, once the grid shows up ",
    					"Simple Dialog", JOptionPane.INFORMATION_MESSAGE);
    		//jrbPlay.setSelected(true);
    		JRadioButton jrbInstructions = new JRadioButton("Instructions");
    		jrbInstructions.setMnemonic(KeyEvent.VK_A);
    		jrbInstructions.setActionCommand("Instructions");
    		JRadioButton jrbJukebox= new JRadioButton("Jukebox");
    		jrbJukebox.setMnemonic(KeyEvent.VK_A);
    		jrbJukebox.setActionCommand("Jukebox");
    		JRadioButton jrbCredits = new JRadioButton("Credits");
    		jrbCredits.setMnemonic(KeyEvent.VK_S);
    		jrbCredits.setActionCommand("Credits");
    		
    			JOptionPane.showMessageDialog(this, "Credits",
    					"Simple Dialog", JOptionPane.INFORMATION_MESSAGE);
    		JRadioButton jrbQuit = new JRadioButton("Quit");
    		jrbQuit.setMnemonic(KeyEvent.VK_S);
    		jrbQuit.setActionCommand("Quit");
    			/*int x=JOptionPane.showConfirmDialog(this, "Are you sure you want to Quit?",
    					"Question",JOptionPane.OK_CANCEL_OPTION);
    				if(x==0)
    					System.exit(0);*/
    		// Group the radio buttons.
    		ButtonGroup group = new ButtonGroup();
    		group.add(jrbPlay);
    		group.add(jrbInstructions);
    		group.add(jrbJukebox);
    		group.add(jrbCredits);
    		group.add(jrbQuit);
    		// Register an action listener for the radio buttons.
    		myListener = new RadioListener();
    		jrbPlay.addActionListener(myListener);
    		jrbInstructions.addActionListener(myListener);
    		jrbCredits.addActionListener(myListener);
    		jrbJukebox.addActionListener(myListener);
    		jrbQuit.addActionListener(myListener);
    		// Set up the picture label
    		jlbPicture = new JLabel(new ImageIcon("" + "Yolo" + ".gif"));
    		// Set the Default Image
    		jlbPicture.setPreferredSize(new Dimension(177, 122));
    		// Put the radio buttons in a column in a panel
    		JPanel jplRadio = new JPanel();
    		jplRadio.setLayout(new GridLayout(0, 1));
    		jplRadio.add(jrbPlay);
    		jplRadio.add(jrbInstructions);
    		jplRadio.add(jrbCredits);
    		jplRadio.add(jrbJukebox);
    		jplRadio.add(jrbQuit);
    		setLayout(new BorderLayout());
    		add(jplRadio, BorderLayout.WEST);
    		add(jlbPicture, BorderLayout.CENTER);
    		setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    	}
    	/** Listens to the radio buttons. */
    	class RadioListener implements ActionListener {
    
    
    		public void actionPerformed(ActionEvent e) {
    			jlbPicture.setIcon(new ImageIcon("" + e.getActionCommand()
    					+ ".jpg"));
    		}
    	}
    	public static void main(String s[]) {
    		frame = new JFrame("Mr. Guerra's Garage Band");
    		frame.addWindowListener(new WindowAdapter() {
    
    
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    		frame.getContentPane().add(new JRadioButtonDemo(),
    				BorderLayout.CENTER);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    Last edited by Tolls; 06-05-2014 at 10:33 AM. Reason: Fix code tag

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: JOptionPane Menu Issue(non static in static ) (prompt help needed due in 7 hours)

    Why are you declaring your methods static? Since they apparently need access to the class state information (i.e. instance fields) they should be declared as instance methods. You may want to read up on when and when not to use static methods.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: JOptionPane Menu Issue(non static in static ) (prompt help needed due in 7 hours)

    The OP already answers your question before you asked it:

    Attempt 2
    //No clue What's im doing <--- your answer
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 03-31-2013, 04:30 AM
  2. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  3. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •