//************************************************************************************
// The 6 demo classes are lifts from the Magelang
Swing Tutorial. They are provided
// as examples in a collection you can inspect
on your computer. Lift the code to an
// editor compile and run
//************************************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
//Demo class 1 from Magelang tutorial
class RadioButtonPanel extends JPanel {
RadioButtonPanel() {
// Set the layout to a GridLayout
setLayout(new GridLayout(4,1));
// Declare a radio button
JRadioButton radioButton;
// Instantiate a ButtonGroup for functional
// association among radio buttons
ButtonGroup rbg = new ButtonGroup();
// Create a label for the group
JLabel label = new JLabel("Annual Salary: ");
label.setFont(new Font("SansSerif", Font.BOLD, 14));
add(label);
// Add a new radio button to the pane
radioButton = new JRadioButton("$45,000");
add (radioButton);
// set key accelerator
radioButton.setMnemonic (KeyEvent.VK_4);
// Add the button to the ButtonGroup
rbg.add (radioButton);
// Set this radio button to be the default
radioButton.setSelected(true);
// Set up two more radio buttons
radioButton = new JRadioButton("$60,000");
radioButton.setMnemonic (KeyEvent.VK_6);
add (radioButton);
rbg.add (radioButton);
radioButton = new JRadioButton("$75,000");
radioButton.setMnemonic (KeyEvent.VK_7);
add (radioButton);
rbg.add (radioButton);
}
}
//*****************************************
// Demo class 2 from swing tutorial
class ScrollPanel extends JPanel {
public ScrollPanel() {
// The .gif here may not be visible
setLayout(new BorderLayout());
// or available to your code so go
Icon b = new ImageIcon("Ball_ani.gif");
// get one from the OS or the net,
JLabel cloudLabel = new JLabel(b);
// place it in the directory with
JScrollPane scrollPane = new JScrollPane(cloudLabel);
// your source code and make it's
add(scrollPane, BorderLayout.CENTER);
// file name the argument to the
}
// new ImageIcon( ) constructor
}
//*****************************************
//Demo class 3
class TextPanel extends JPanel {
public TextPanel() {
// Set the layout to a BorderLayout
setLayout(new BorderLayout());
// Create the three basic text components
JTextField textField = new JTextField("The JTextField");
JTextArea textArea = new JTextArea("The JTextArea.\n
The JTextPane" +
"below requires you to add a StyledDocument");
JTextPane textPane = new JTextPane();
//Set the textpane's font properties
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontFamily(attr, "Serif");
StyleConstants.setFontSize(attr, 18);
StyleConstants.setBold(attr, true);
textPane.setCharacterAttributes(attr, false);
add(textField, BorderLayout.NORTH);
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(new JScrollPane(textPane), BorderLayout.SOUTH);
}
}
//***********************************************
//demo class 4
class SliderPanel extends JPanel {
public SliderPanel() {
setLayout(new BorderLayout());
JSlider slider1 = new JSlider (JSlider.VERTICAL, 0, 100,
50);
slider1.setPaintTicks(true);
slider1.setMajorTickSpacing(10);
slider1.setMinorTickSpacing(2);
add(slider1, BorderLayout.EAST);
JSlider slider2 =
new JSlider (JSlider.VERTICAL, 0, 100, 50);
slider2.setPaintTicks(true);
slider2.setMinorTickSpacing(5);
add(slider2, BorderLayout.WEST);
JSlider slider3 =new JSlider (JSlider.HORIZONTAL, 0, 100,
50);
slider3.setPaintTicks(true);
slider3.setMajorTickSpacing(10);
add(slider3, BorderLayout.SOUTH);
JSlider slider4 =new JSlider (JSlider.HORIZONTAL, 0, 100,
50);
slider4.setBorder(BorderFactory.createLineBorder(Color.blue));
add(slider4, BorderLayout.NORTH);
}
}
//******************************************************
//demo class 5
class ComboPanel extends JPanel {
String choices[] = {
"Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn",
"Uranus","Neptune", "Pluto"};
public ComboPanel() {
JComboBox combo1 = new JComboBox();
combo1.setBorder(new BevelBorder(BevelBorder.RAISED));
//added a border to demo
JComboBox combo2 = new JComboBox();
combo2.setBorder(new BevelBorder(BevelBorder.RAISED));
for (int i=0;i<choices.length;i++) {
combo1.addItem (choices[i]);
combo2.addItem (choices[i]);
}
combo2.setEditable(true);
combo2.setSelectedItem("X");
combo2.setMaximumRowCount(4);
add(combo1);
add(combo2);
}
}
public class JF extends JFrame{
public static void main(String args[]){
JF jf=new JF();
jf.getContentPane().setLayout(new GridLayout(3,2,5,5));
//**************************************** //
Note Just as an example,class PasswordPanel is
//Demo class 6 from swing tutorial
// placed inside JF becoming an inner class You'll
// note it compiles to a class file beginning with
// a dollar sign
class PasswordPanel extends JPanel {
PasswordPanel() {
JPasswordField pass1 = new JPasswordField(20);
JPasswordField pass2 = new JPasswordField(20);
pass2.setEchoChar ('?');
add(pass1);
add(pass2);
}
}
//*******************************************
RadioButtonPanel rbp=new RadioButtonPanel();
jf.getContentPane().add(rbp);
ScrollPanel sp=new ScrollPanel();
jf.getContentPane().add(sp);
TextPanel tp=new TextPanel();
jf.getContentPane().add(tp);
PasswordPanel pwp=new PasswordPanel();
jf.getContentPane().add(pwp);
SliderPanel sdp=new SliderPanel();
jf.getContentPane().add(sdp);
ComboPanel cp=new ComboPanel();
jf.getContentPane().add(cp);
jf.setSize(600,500);
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter(){
// an anonymous inner class at it's best
public void windowClosing(WindowEvent
we){
// eliminates closing dos with 'Ctrl-c'
System.exit(0);
}
});
}
}