1

First off, this is my first post to Stack Overflow so please forgive me if I am not following some of the proper etiquitte. Im trying to make a GUI program that will display different food genres and then when the user presses the genre they want it will show a randomly generated restraunt of that genre. I am fairly new to programming and I am having a hard time figuing out how to make my buttons actually work. I have assigned the Restraunts objects each a value. Mexican will be values 1 and 2. Italian will be values 3 and 4. What Im wanting is when the user selects "Mexican" for the program to generate a random number (the restraunts value attribute) between 1 and 2 and then display that object with all its attributes in the same window. I've been stuck on this part for a bit and any help would be greatly appreciated. Thank you all in advance for your time. The code I have is as follows:

import TrySource.TryWindow;
import TrySource.Restraunts;
import java.awt.FlowLayout;
import javax.swing.JFrame;


public class TrySomethingNew 
{

   public static void main (String[] args)
    {
      TryWindow frame = new TryWindow();
        frame.setTitle("Try Something New");
        frame.setSize(1000,900);
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
   }
}

package TrySource;

import TrySource.TryWindow.ButtonClicked;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class TryWindow extends JFrame
{
    JButton jbtMexican = new JButton("Mexican");
    JButton jbtItalian = new JButton("Italian");
    
    
    public TryWindow()
    {
        super("Try Something New");
        
        add(jbtMexican);
        add(jbtItalian);
    
    
    }//end TryWindow constructor
    
    class ButtonClicked implements MouseListener
    {
        

        @Override
        public void mouseClicked(MouseEvent e) 
        {
        }

        @Override
        public void mousePressed(MouseEvent e) 
        {
             
        }

        @Override
        public void mouseReleased(MouseEvent e) 
        {
        }

        @Override
        public void mouseEntered(MouseEvent e) 
        {
        }

        @Override
        public void mouseExited(MouseEvent e) 
        {
        }
        
    }
    
    
}//end class

package TrySource;

public class Restraunts 
{
    String restrauntName;
    String restrauntAddress;
    String restrauntPhone;
    
    public Restraunts(String name, String address, String phone, int value)
    {
        restrauntName = name;
        restrauntAddress = address;
        restrauntPhone = phone;

    }//end constructor
    
    Restraunts joseLocos = new Restraunts("Jose Locos", "853 N Glenstone Ave, Springfield, MO 65802",
            "(417) 831-1300", 1);
    Restraunts amigos = new Restraunts ("Amigos Mexican Restaurant","2118 S Campbell Ave, Springfield, MO 65807",
            "(417) 887-1401", 2);
    Restraunts zios = new Restraunts("Zios Italian Kitchen", "1249 E Kingsley St, Springfield, MO 65804",
            "(417) 889-1919", 3);
    Restraunts bambinos = new Restraunts("Bambinos Cafe", "1141 E Delmar St, Springfield, MO 65807",
            "(417) 862-9999", 4);
            
}//end Restraunts
Butters573
  • 37
  • 1
  • 5
  • Take a look at the [tutorial on JButtons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html), you need to use an ActionListener, not a MouseListener. (or at least it's a lot simpler if you do.) – markspace Nov 13 '14 at 03:19

1 Answers1

0
  • Add the Restraunts to a List
  • Group them together (by their type) using a Map
  • Use Collections.shuffle to randomise the List and select the first one

Take a look at

for more details

Oh, and you might like to gave a look at How to Use CardLayout

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366