Please send questions to
st10@humboldt.edu .
/**
* TryJFrames1
*
* this is a Swing version of (approximately) GuiApplic2.java.
* It uses the Swing versions of the AWT components used in
* GuiApplic2.java.
*
* modified by: Sharon M. Tuttle
* last modified: 2-12-01
**/
// notice what we now also need to import...!
import javax.swing.*;
// and, note that we STILL need these --- we are not
// using AWT components, but we *are* still using
// AWT layout managers, event model, colors, fonts, etc.
import java.awt.*;
import java.awt.event.*;
public class TryJFrames1
{
public static void main(String args[])
{
// using a subclass of JFrame defined below in this file;
TryJFrames1Frame mainFrame;
mainFrame = new TryJFrames1Frame();
// yes, you still have these methods for JFrames;
mainFrame.setTitle("TryJFrames1");
mainFrame.show();
// still setting up size and window-closing in class
// TryJFrames1Frame...
}
}
// note that it extends JFrame... it is a subclass of JFrame.
class TryJFrames1Frame extends JFrame
{
// notice we are now using JLabel, JButton here;
private int count;
private JLabel countLabel;
private JButton toggleButton;
private boolean nowGreen;
// still need constructor for main() use;
public TryJFrames1Frame()
{
// now JLabel, JButton;
JLabel welcome, author, version;
JButton countButton;
// NEW: the needed reference to this JFrame's
// content pane
Container myContentPane;
// NEW: get content pane for this JFrame
myContentPane = getContentPane();
// these will be instantiated as inner listener
// classes for the two buttons
ActionListener ctListener, toggListener;
// set up window-closing for the instance of this
// TryJFramesFrame, again with an anonymous inner class
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
// NEW: notice that you set the layout manager for the
// content pane;
myContentPane.setLayout(new FlowLayout());
// JLabels can be set up very much as AWT Labels are;
welcome = new JLabel("Welcome to a JFrame example");
welcome.setForeground(Color.blue);
// NEW --- note what JButton instance is add()'ed to,
// however! again, to the content pane;
myContentPane.add(welcome);
// same applies for these other basic components;
author = new JLabel("Sharon M. Tuttle");
author.setForeground(Color.black);
myContentPane.add(author);
version = new JLabel("Version 1.2");
version.setForeground(Color.red);
myContentPane.add(version);
countButton = new JButton("count clicks");
myContentPane.add(countButton);
// still using inner class CountListener to
// handle countButton's events;
count = 0;
ctListener = new CountListener();
countButton.addActionListener(ctListener);
countLabel = new JLabel("count clicks button pressed: 0 times");
myContentPane.add(countLabel);
nowGreen = true;
toggleButton = new JButton("toggle color");
toggleButton.setBackground(Color.green);
myContentPane.add(toggleButton);
// still using inner class ToggleListener to
// handle toggleButton's events;
toggListener = new ToggleListener();
toggleButton.addActionListener(toggListener);
// yup, JFrame does have a setSize() method, still;
setSize(250, 200);
}
// this special listener handles counting button
private class CountListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// only possible action is a counting button
// press --- increase press count accordingly
count++;
countLabel.setText("count clicks button pressed: " +
count + " times");
}
}
// this special listener handles toggling the toggle
// button's background color
private class ToggleListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// decide what next background-color-to-be should be
// for toggleButton
if (nowGreen)
{
toggleButton.setBackground(Color.orange);
nowGreen = false;
}
else
{
toggleButton.setBackground(Color.green);
nowGreen = true;
}
}
}
}