按下时如何制作JButton参考对象?

时间:2014-11-13 03:14:09

标签: java class object jbutton

首先,这是我向Stack Overflow发表的第一篇文章,所以请原谅我,如果我没有遵循一些正确的etiquitte。我试图制作一个显示不同食物类型的GUI程序,然后当用户按下他们想要的类型时,它将显示该类型的随机生成的限制。我对编程很新,我很难弄清楚如何使我的按钮真正起作用。我已经为每个Restraunts对象分配了一个值。墨西哥将是值1和2.意大利语将是值3和4.我想要的是当用户为程序选择“墨西哥”以生成1到2之间的随机数(约束值属性)然后显示该对象时将所有属性放在同一窗口中。我一直坚持这一部分,任何帮助将不胜感激。提前感谢大家的时间。我的代码如下:

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

1 个答案:

答案 0 :(得分:0)

  • Restraunts添加到List
  • 使用Map
  • 将它们组合在一起(按类型)
  • 使用Collections.shuffle随机化List并选择第一个

看看

了解更多详情

哦,你可能想看看How to Use CardLayout