this program should be written in JAVA. Write a GUl-based program that displays
ID: 3739574 • Letter: T
Question
this program should be written in JAVA.
Write a GUl-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
JAVA CODE:
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[] country = {"USA","India","Mexico","Canada","Brazil","China"};
private static final long serialVersionUID = 1L;
JLabel l1, l2, l3,l4;
JButton b1,b2,b3,b4;
JTextField t1, t2, t3,t4;
JavaGui()
{
l1 = new JLabel("Station1");
l2 = new JLabel("Station2");
l3 = new JLabel("Station3");
l4 = new JLabel("Station4");
b1 = new JButton("S1");
b2 = new JButton("S2");
b3 = new JButton("S3");
b4 = new JButton("S4");
t1 = new JTextField(" ?");
t2 = new JTextField(" ?");
t3 = new JTextField(" ?");
t4 = new JTextField(" ?");
add(l1);
add(t1);
add(b1);
add(l2);
add(t2);
add(b2);
add(l3);
add(t3);
add(b3);
add(l4);
add(t4);
add(b4);
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)
{
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.