Write a GUI-based program that displays a Monopoly ® game. Add labels for the fo
ID: 3715334 • Letter: W
Question
Write a GUI-based program that displays a Monopoly ® game. Add labels for the four train stations, the free space and each of the property spaces. Add buttons for all the Chance cells and set the text of these to a question mark. When the user clicks on one of the buttons, set its text to a message of your choice, chosen randomly from four messages. On the jail spot, create a drop-down list with the choices, "Just Visiting" or "In Jail". If just visiting, display a message, "Good to have a visitor." If in jail, display a message, "Crime doesn't pay!".
PROGRAM LANGUAGE: JAVA
Write a GUI-based program that displays a Monopoly game. Add labels for the four train stations, the free space and each of the property spaces. Add buttons for all the Chance cells and set the text of these to a question mark. When the user clicks on one of the buttons, set its text to a message of your choice, chosen randomly from four messages. On the jail spot, create a drop-down list with the choices, 'Just Visiting" or 'In Jail. If just visiting, display a message, "Good to have a visitor." If in jail, display a message, 'Crime doesn't pay!". If you add images to the labels, you can receive .5 points per image. MONOPOLYExplanation / Answer
//Required packages imported
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JavaGui extends JFrame implements ActionListener
{
//String array to choose random string
String[] country = {"USA","India","Mexico","Canada","Brazil","China"};
private static final long serialVersionUID = 1L;
// Four labels
JLabel l1, l2, l3,l4;
//Four Buttons
JButton b1,b2,b3,b4;
// 4 text fields
JTextField t1, t2, t3,t4;
JavaGui()
{
// Labels Defined
l1 = new JLabel("Station1");
l2 = new JLabel("Station2");
l3 = new JLabel("Station3");
l4 = new JLabel("Station4");
// Buttons defined
b1 = new JButton("S1");
b2 = new JButton("S2");
b3 = new JButton("S3");
b4 = new JButton("S4");
// Textfields defines
t1 = new JTextField(" ?");
t2 = new JTextField(" ?");
t3 = new JTextField(" ?");
t4 = new JTextField(" ?");
// labels,textfields and buttons added
add(l1);
add(t1);
add(b1);
add(l2);
add(t2);
add(b2);
add(l3);
add(t3);
add(b3);
add(l4);
add(t4);
add(b4);
// Added actionlistener to buttons
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setSize(500,300);
setLayout(new GridLayout(5,3));
setTitle("Java GUI");
}
public void actionPerformed(ActionEvent ae)
{
// Actions for buttons defined
if(ae.getSource() == b1)
{
String random = (country[new Random().nextInt(country.length)]);
t1.setText(random);
}
if(ae.getSource() == b2)
{
String random = (country[new Random().nextInt(country.length)]);
t2.setText(random);
}
if(ae.getSource() == b3)
{
String random = (country[new Random().nextInt(country.length)]);
t3.setText(random);
}
if(ae.getSource() == b4)
{
String random = (country[new Random().nextInt(country.length)]);
t4.setText(random);
}
}
public static void main(String args[])
{
JavaGui a = new JavaGui();
a.setVisible(true);
a.setLocation(200, 200);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.