Swing GUIs

Work on this exercise by yourself, but ask questions of your instructor, student assistants and classmates as desired.

Goals

The goals of this exercise are to:

The Demo and Exercise: Work with your instructor to create a basic GUI.

Start by reading the main steps of the IEP. Then create a UML design for the project. Then start implementing according to the following 6-step Iterative Enhancement Plan (IEP):
1. The 400x300 frame appears
  1. Create a new project called SwingDemo.
  2. Create a new class called SwingDemo with a main method.
  3. In main(), create a new SwingDemoFrame
  4. Create the SwingDemoFrame class. It should extend JFrame.
  5. Create a default constructor that contains the following code:
    	public SwingDemoFrame() {
    		this.setSize(new Dimension(400, 300));
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setTitle("Swing Demo");
    		this.setLayout(new FlowLayout());
    
    
    Back in main, call the frame's setVisible() method with the value "true" to get the frame to show up.
2. A label that says "Hello" appears, along with a button that says "Press me".
  1. To get something to appear, you need to create it, then add it.
  2. Create a new JLabel called myLabel, and set its text to "Hello".
  3. Create a new button called colorChangeButton with the text, "Press me"
3. A red panel appears vertically the between the button and the label.
  1. Create and add a new panel. You should create a new ColoredPanel class that extends JPanel.
  2. Set the size of the panel to 200 x 200. This is just like it is for frames, but instead, it uses a setPreferredSize() method.
  3. Set the background color to red (hint: type this. and look at your options. One will look very appropriate!)
  4. To get the vertical layout, change the frame's LayoutManager to BorderLayout, and add things to NORTH, CENTER, and SOUTH, respectively.
4. A circle appears inside the panel.
  1. You can't paint within a frame, you need to paint inside a panel. This is done using the paintComponent() method.
  2. Create a public void method with the signature: paintComponent(Graphics g)
  3. The first line of the method should be a call to JPanel's paintComponent(g) method. (How?)
  4. Now, call g's drawOval() method using an appropriate size and location.
  5. (BONUS: how could you get it to show up in the middle of the screen?)
5. Another shape appears in the panel.
Please draw or fill at least one other type of shape on the panel.
6. Something else novel appears on the screen
If you finish early, please experiment with adding another type of component to the screen. (The tutorial on Swing in the Java Tutorial is very good.)
7. Clicking the button causes the color of the panel to change to green.
  1. For this, you will add an ActionListener.  The component that will respond to the event (call it r) must implement ActionListener, which means it must have an actionPerformed() method. You then need to call a.addActionListener(r) from somewhere in r, replacing a with the component that is listening for events, and r is the one that's responding.
  2. Two examples: a panel responds to a button's clicks:
    this.button.addActionListener(this)
    
    A button responds to its own mouse clicks:
    this.addActionListener(this)
    
8. Instead of a label, there is a text field at the top of the screen that changes the panel back to red when someone presses "enter".
  1. Handle this the same way that you did the button, but now the panel will need to know what component generated the event. Use conditions like this:
    if (event.getSource() == this.button) // then it was the button that was clicked