import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SwingAppletDemo extends JApplet implements ActionListener, ItemListener, ListSelectionListener, ChangeListener { private JButton button1, button2, button3, button4; private JLabel label1; private JCheckBox checkBox1; private ButtonGroup radioButtons; private JRadioButton hotRadio, warmRadio, coldRadio; private JComboBox combo1; private JList list1; private DefaultListModel listModel; private JTextField textField1; private JSlider slider1; private JTextArea textArea1; Paper paper; public void init() { Container c=getContentPane(); c.setLayout(new FlowLayout() ); label1= new JLabel("Some basic Swing:"); c.add(label1); checkBox1= new JCheckBox("-A JCheckBox"); c.add(checkBox1); checkBox1.addItemListener(this); hotRadio = new JRadioButton("hot",false); c.add(hotRadio); hotRadio.addItemListener(this); warmRadio = new JRadioButton("warm",false); c.add(warmRadio); warmRadio.addItemListener(this); coldRadio = new JRadioButton("cold",false); c.add(coldRadio); coldRadio.addItemListener(this); radioButtons = new ButtonGroup(); radioButtons.add(hotRadio); radioButtons.add(warmRadio); radioButtons.add(coldRadio); combo1= new JComboBox(); combo1.addItem("Red"); combo1.addItem("Yellow"); combo1.addItem("Blue"); combo1.addActionListener(this); c.add(combo1); button1 = new JButton("Message dialog"); c.add(button1); button1.setToolTipText("click to see a message dialog"); button1.addActionListener(this); button2 = new JButton("Confirm dialog"); c.add(button2); button2.addActionListener(this); button3 = new JButton("Input dialog"); c.add(button3); button3.addActionListener(this); button4 = new JButton("Draw circle"); c.add(button4); button4.addActionListener(this); listModel = new DefaultListModel(); listModel.addElement("Mike"); listModel.addElement("Maggie"); listModel.addElement("Matthew"); listModel.addElement("Eleanor"); list1 = new JList(listModel); list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); c.add(list1); list1.addListSelectionListener(this); textField1 = new JTextField(15); textField1.setText("A JTextField: type here..."); c.add(textField1); textField1.addActionListener(this); // a text area in a scroll pane textArea1 = new JTextArea(" A JTextArea"); textArea1.setLineWrap(true); JScrollPane areaScrollPane = new JScrollPane(textArea1); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(100,200)); c.add(areaScrollPane); // a panel in a scroll pane paper = new Paper(); JScrollPane paperScrollPane = new JScrollPane(paper); paperScrollPane.setPreferredSize(new Dimension(100,200)); c.add(paperScrollPane); slider1 = new JSlider(JSlider.HORIZONTAL, 0, 10, 5); slider1.setMajorTickSpacing(10); slider1.addChangeListener(this); slider1.setMinorTickSpacing(1); slider1.setPaintTicks(true); slider1.setPaintLabels(true); c.add(slider1); } public void actionPerformed(ActionEvent e) { if (e.getSource()== button1) { JOptionPane.showMessageDialog(null, "A message:\n"+ "The Combo state is: " + combo1.getSelectedIndex()); } if (e.getSource()== button2) { int userResponse = JOptionPane.showConfirmDialog(null, "Delete files?"); if (userResponse == JOptionPane.NO_OPTION) JOptionPane.showMessageDialog(null, "No - Files not deleted!"); else if (userResponse == JOptionPane.YES_OPTION) JOptionPane.showMessageDialog(null, "Yes - Files deleted (just pretending!)"); else if (userResponse == JOptionPane.CANCEL_OPTION) JOptionPane.showMessageDialog(null, "You cancelled"); else if (userResponse == JOptionPane.CLOSED_OPTION) // user closed the dialog - could treat as 'Cancel' JOptionPane.showMessageDialog(null, "You closed"); } if (e.getSource()== button3) { String bookTitle = JOptionPane.showInputDialog(null, "Enter book title"); if (bookTitle == null) JOptionPane.showMessageDialog(null, "You cancelled"); else JOptionPane.showMessageDialog(null, "Title is: " + bookTitle); } if(e.getSource() == button4) { paper.setDiameter(55); paper.display(); } if(e.getSource()==combo1){ int n=combo1.getSelectedIndex(); JOptionPane.showMessageDialog(null, "combo item number "+ n + ": the string is: "+combo1.getSelectedItem()); } if (e.getSource()== textField1) JOptionPane.showMessageDialog(null,"You hit enter " + "on textfield. Text is:\n"+ textField1.getText()); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == checkBox1) JOptionPane.showMessageDialog(null, "checkBox changed to: "+checkBox1.isSelected()); if (e.getSource()== hotRadio) JOptionPane.showMessageDialog(null, " hot clicked"); if (e.getSource()== warmRadio) JOptionPane.showMessageDialog(null, "warm clicked"+ coldRadio.isSelected()); if (e.getSource()== coldRadio) JOptionPane.showMessageDialog(null, "cold clicked"+ coldRadio.isSelected()); } public void valueChanged(ListSelectionEvent e) { if(e.getSource()==list1) JOptionPane.showMessageDialog(null, "Chose list item number:"+list1.getSelectedIndex()); } public void stateChanged(ChangeEvent e) { if (e.getSource()== slider1) textArea1.append("\nslider1 is: " + slider1.getValue()); } } class Paper extends JPanel { // to draw on int diameter =400; public void setDiameter(int d) { diameter = d; } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawOval(0,0,diameter,diameter); g.drawString("Jpanel drawing", 0, 20); } public void display() { repaint(); } }