public class SwingAppletDemo
class Paper
1: // SwingAppletDemo.java
3: import java.awt.*;
4: import java.awt.event.*;
5: import javax.swing.*;
6: import javax.swing.event.*;
9: public class SwingAppletDemo
10: extends JApplet
11: implements ActionListener,
12: ItemListener,
13: ListSelectionListener,
14: ChangeListener
15: {
16: private JButton button1, button2, button3, button4;
17: private JRadioButton hotRadio, warmRadio, coldRadio;
18: private ButtonGroup radioButtons;
19: private JLabel label1;
20: private JCheckBox checkBox1;
21: private JComboBox combo1;
22: private JList list1;
23: private DefaultListModel listModel1;
24: private JTextField textField1;
25: private JTextArea textArea1;
26: private JSlider slider1;
27:
28: Paper paper;
30: public void init()
31: {
32: Container c = getContentPane();
33: c.setLayout(new FlowLayout() );
35: label1 = new JLabel("Some Basic Swing:");
36: c.add(label1);
38: checkBox1 = new JCheckBox("A JCheckBox");
39: c.add(checkBox1);
40: checkBox1.addItemListener(this);
42: hotRadio = new JRadioButton("Hot",false);
43: c.add(hotRadio);
44: hotRadio.addItemListener(this);
46: warmRadio = new JRadioButton("Warm",false);
47: c.add(warmRadio);
48: warmRadio.addItemListener(this);
50: coldRadio = new JRadioButton("Cold",false);
51: c.add(coldRadio);
52: coldRadio.addItemListener(this);
54: radioButtons = new ButtonGroup();
55: radioButtons.add(hotRadio);
56: radioButtons.add(warmRadio);
57: radioButtons.add(coldRadio);
59: combo1= new JComboBox();
60: combo1.addItem("Red");
61: combo1.addItem("Yellow");
62: combo1.addItem("Blue");
63: combo1.addActionListener(this);
64: c.add(combo1);
65:
66: button1 = new JButton("Message Dialog");
67: c.add(button1);
68: button1.setToolTipText("Click to see a message dialog");
69: button1.addActionListener(this);
71: button2 = new JButton("Confirm dialog");
72: c.add(button2);
73: button2.addActionListener(this);
75: button3 = new JButton("Input dialog");
76: c.add(button3);
77: button3.addActionListener(this);
79: button4 = new JButton("Draw circle");
80: c.add(button4);
81: button4.addActionListener(this);
83: listModel1 = new DefaultListModel();
84: listModel1.addElement("Mike");
85: listModel1.addElement("Maggie");
86: listModel1.addElement("Matthew");
87: listModel1.addElement("Eleanor");
89: list1 = new JList(listModel1);
90: list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
91: c.add(list1);
92: list1.addListSelectionListener(this);
94: textField1 = new JTextField(15);
95: textField1.setText("A JTextField: Type here ...");
96: c.add(textField1);
97: textField1.addActionListener(this);
99: // A text area in a scroll pane
100: textArea1 = new JTextArea("A JTextArea");
101: textArea1.setLineWrap(true);
102: JScrollPane areaScrollPane = new JScrollPane(textArea1);
103: areaScrollPane.setVerticalScrollBarPolicy(
104: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
105: areaScrollPane.setHorizontalScrollBarPolicy(
106: JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
107: areaScrollPane.setPreferredSize(new Dimension(100, 200));
108: c.add(areaScrollPane);
110: // A panel in a scroll pane
111: paper = new Paper();
112: JScrollPane paperScrollPane = new JScrollPane(paper);
113: paperScrollPane.setPreferredSize(new Dimension(100, 200));
114: c.add(paperScrollPane);
116: slider1 = new JSlider(JSlider.HORIZONTAL, 0, 10, 5);
117: slider1.setMajorTickSpacing(10);
118: slider1.addChangeListener(this);
119: slider1.setMinorTickSpacing(1);
120: slider1.setPaintTicks(true);
121: slider1.setPaintLabels(true);
122: c.add(slider1);
123: }
125:
126: public void actionPerformed(ActionEvent e)
127: {
128: if (e.getSource() == button1)
129: {
130: JOptionPane.showMessageDialog(null, "A message:\n" +
131: "The Combo state is: " + combo1.getSelectedIndex());
132: }
134: if (e.getSource() == button2)
135: {
136: int userResponse = JOptionPane.showConfirmDialog(null,
137: "Delete files?");
138: if (userResponse == JOptionPane.NO_OPTION)
139: JOptionPane.showMessageDialog(null,
140: "No - Files not deleted!");
142: else if (userResponse == JOptionPane.YES_OPTION)
143: JOptionPane.showMessageDialog(null,
144: "Yes - Files deleted (just pretending!)");
146: else if (userResponse == JOptionPane.CANCEL_OPTION)
147: JOptionPane.showMessageDialog(null, "You cancelled.");
149: else if (userResponse == JOptionPane.CLOSED_OPTION)
150: // User closed the dialog - could treat as 'Cancel'
151: JOptionPane.showMessageDialog(null, "You closed.");
152: }
154: if (e.getSource() == button3)
155: {
156: String bookTitle = JOptionPane.showInputDialog(null,
157: "Enter book title");
158: if (bookTitle == null)
159: JOptionPane.showMessageDialog(null, "You cancelled.");
160: else
161: JOptionPane.showMessageDialog(null, "Title is: "
162: + bookTitle);
163: }
165: if(e.getSource() == button4)
166: {
167: paper.setDiameter(55);
168: paper.display();
169: }
171: if(e.getSource() == combo1)
172: {
173: int n=combo1.getSelectedIndex();
174: JOptionPane.showMessageDialog(null, "Combo item number " +
175: n + ": the string is: " + combo1.getSelectedItem());
176: }
177:
178: if (e.getSource() == textField1)
179: JOptionPane.showMessageDialog(null, "You hit enter " +
180: "on textfield. Text is:\n" + textField1.getText());
181: }
183:
184: public void itemStateChanged(ItemEvent e)
185: {
186: if (e.getSource() == checkBox1)
187: JOptionPane.showMessageDialog(null,
188: "checkBox changed to: " + checkBox1.isSelected());
190: if (e.getSource() == hotRadio)
191: JOptionPane.showMessageDialog(null, "Hot clicked " +
192: hotRadio.isSelected());
194: if (e.getSource() == warmRadio)
195: JOptionPane.showMessageDialog(null, "Warm clicked " +
196: warmRadio.isSelected());
198: if (e.getSource() == coldRadio)
199: JOptionPane.showMessageDialog(null, "Cold clicked " +
200: coldRadio.isSelected());
201: }
204: public void valueChanged(ListSelectionEvent e)
205: {
206: if (e.getSource() == list1)
207: JOptionPane.showMessageDialog(null,
208: "Chose list item number: " + list1.getSelectedIndex());
209: }
212: public void stateChanged(ChangeEvent e)
213: {
214: if (e.getSource() == slider1)
215: textArea1.append("\nSlider value is: " + slider1.getValue());
216: }
217: }
221: // An object of this class is used to "draw on"
222: class Paper
223: extends JPanel
224: {
225: int diameter = 400;
227: public void setDiameter(int d)
228: {
229: diameter = d;
230: }
232: public void paintComponent(Graphics g)
233: {
234: super.paintComponent(g);
235: g.drawOval(0, 0, diameter,diameter);
236: g.drawString("Jpanel drawing", 0, 20);
237: }
239: public void display()
240: {
241: repaint();
242: }
243: }