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);
}
}