Could you please help me: 1. import java.awt.event.*; import javax.swing.*; publ
ID: 3555159 • Letter: C
Question
Could you please help me:
1.
import java.awt.event.*;
import javax.swing.*;
public class ColorChooserExample
{
public static void main(String [] args)
{
MyFrame frame = new MyFrame("My Window");
frame.pack();
frame.setLocation(100, 75);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyFrame extends JFrame
{
JButton choose = new JButton("Choose");
JTextField text = new JTextField("Text field with default text.");
JPanel panel = new JPanel();
public MyFrame(String s)
{
super(s);
panel.add(choose);
text.setEditable(false);
panel.add(text);
add(panel);
choose.addActionListener(new ChooseHandler());
}
class ChooseHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Write code to get a color from the user. Use that color
// for the background of the text field.
// If the user does not give you a valid color, then set the
// text field's background to gray.
}
}
}
2.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUILayouts extends JFrame
{
JCheckBox b1 = new JCheckBox("shiny", true);
JCheckBox b2 = new JCheckBox("fun", false);
JTextField txt1 = new JTextField("red");
JTextField txt2 = new JTextField("green");
JTextField txt3 = new JTextField("blue");
JTextField txt4 = new JTextField("yellow");
JTextField txt5 = new JTextField("orange");
JButton btn1 = new JButton("Btn 1");
JButton btn2 = new JButton("Btn 2");
JButton btn3 = new JButton("Btn 3");
JButton btn4 = new JButton("Btn 4");
JButton btn5 = new JButton("Btn 5");
public static void main(String [] args)
{
GUILayouts gq = new GUILayouts();
}
public GUILayouts()
{
super("GUI Question");
// Write the code that should go here!
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
3.
import java.awt.event.*;
public class GUILayouts2 extends JFrame
{
JCheckBox b1 = new JCheckBox("shiny", true);
JCheckBox b2 = new JCheckBox("fun", false);
JTextField txt1 = new JTextField("red");
JTextField txt2 = new JTextField("green");
JTextField txt3 = new JTextField("blue");
JTextField txt4 = new JTextField("yellow");
JTextField txt5 = new JTextField("orange");
JButton btn1 = new JButton("Btn 1");
JButton btn2 = new JButton("Btn 2");
JButton btn3 = new JButton("Btn 3");
JButton btn4 = new JButton("Btn 4");
JButton btn5 = new JButton("Btn 5");
Button group = new ButtonGroup();
public static void main(String [] args)
{
GUILayouts2 gq = new GUILayouts2(2, 2););
}
public GUILayouts2()
{
super("GUI Question");
// Write the code that should go here!
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Explanation / Answer
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class TimerGUI extends JPanel { JLabel promptLabel, timerLabel; final int count = 30; JButton start; JButton end; Timer timer; public TimerGUI() { setLayout(new GridLayout(0,2)); start = new JButton("Start"); add(start); Event e = new Event(); start.addActionListener(e); end = new JButton("End"); end.setEnabled(false); add(end); end.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { timer.stop(); start.setEnabled(true); end.setEnabled(false); timerLabel.setText("Time Remaining " + count + " seconds"); } }); timerLabel = new JLabel("Time Remaining 300 seconds"); add(timerLabel); } private class Event implements ActionListener { @Override public void actionPerformed(ActionEvent e) { start.setEnabled(false); end.setEnabled(true); timerLabel.setText("Time Remaining " + count + " seconds"); TimeClass tc = new TimeClass(count); timer = new Timer(1000, tc); timer.start(); } } private class TimeClass implements ActionListener { int counter; public TimeClass(int count) { this.counter = count; } @Override public void actionPerformed(ActionEvent e) { counter--; if (counter = 1) { timerLabel.setText("Time Remaining " + counter + " seconds"); } else { timer.stop(); timerLabel.setText("game over"); } } } public static void main(String[] args) { JFrame myframe = new JFrame(); TimerGUI timer = new TimerGUI(); // myframe.getContentPane().add(content,BorderLayout.CENTER); myframe.getContentPane().add(timer, BorderLayout.CENTER); myframe.setTitle("Hangman Game"); myframe.pack(); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myframe.setLocationRelativeTo(null); myframe.setVisible(true); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.