



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A tutorial on creating java swing applications and applets using various components such as buttons, labels, textfields, textareas, checkboxes, radio buttons, comboboxes, lists, borders, flowlayout, borderlayout, gridlayout, boxlayout, dialog boxes, menus, and popup menus. It covers the basics of creating and controlling the layout of these components, as well as converting applications to applets.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Java -Swing
Creating Windows & Applets
(lectures programs)
Sun OnLine Documentations
Sun Swing Tutorial
Buttons: Show a variety of buttons.
To run, we may use one of the following methods:
¾ % appletviewer Buttons.java
¾ % appletviewer ButtonsFrame.html
¾ % java Buttons
¾ Use any internet browser and open: ButtonsFrame.html
¾ Buttons
JButton b1 = new JButton("Button 1"); b1.setBackground(Color.red); b2.setEnabled(false);
¾ Lables
JLabel labl1 = new JLabel("TextField t1"); labl1.setText("Hello");
¾ TextFields
JTextField t1 = new JTextField(30);
String s = new String(); s = t1.getText(); s = t1.getSelectedText(); t1.setText(" ");
¾ TextAreas
JTextArea t = new JTextArea(5, 20); t.setText(t.getText()+ "Old Dominion University \n");
¾ CheckBoxs
JCheckBox cb1 = new JCheckBox("Check Box 1"); if(cb.isSelected()) ..
¾ RadioButtons
ButtonGroup g = new ButtonGroup(); JRadioButton rb1 = new JRadioButton("one", false); g.add(rb1);
¾ ComboBoxes
JComboBox c = new JComboBox(); c.addItem(description[count++]); c.getSelectedIndex(); c.getSelectedItem());
¾ Lists
DefaultListModel lItems=new DefaultListModel(); JList lst = new JList(lItems); lItems.addElement(flavors[count++]); Object[] items=lst.getSelectedValues();
¾ Borders
JPanel jp = new JPanel(); Border b = new TitledBorder("Title");
JOptionPane.showMessageDialog(null, "There's a bug on you!", "Message(Alert)!", JOptionPane.ERROR_MESSAGE);
int answer = JOptionPane.showConfirmDialog(null, "Quit? ", "Confirm(Yes/NO”", JOptionPane.YES_NO_OPTION);
int sel = JOptionPane.showOptionDialog(null, "Choose a Color!", "Option(Color)", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
String val = JOptionPane.showInputDialog("How many fingers do you see?");
¾ Menus
JMenuBar mb1 = new JMenuBar(); JMenu m = new JMenu("Flavors"); JMenuItem mi = new JMenuItem(flavors[i]); m.add(mi); mb1.add(m); setJMenuBar(mb1);
¾ Popup
JPopupMenu popup = new JPopupMenu(); JMenuItem mi = new JMenuItem(flavors[i]); popup.add(mi);
... popup.show(.. .);
Converting Applications to Applets
Application:
class A {} class B {}
public class FileName {
public static void main(String[] args) {
Applete:
//
import com.bruceeckel.swing.;*
// make sure to modify CLASSPATH // e.g., in file .cshrc in UNIX, to include where this package is // e.g., in UNIX /home/cs476/java/ThinkingInJava.2ndEditionR12/code
other imports....;
public class FileName extends JApplet { JTextArea Xout = new JTextArea(10, 30); public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(new JLabel("FileName")); cp.add(new JScrollPane(Xout));
Anywhere inside the code of main or any other class: replace: System.out.println("string"); with: Xout.append("string"); }
class A { } class B { } public static void main(String[] args) { Console.run(new FileName(), 350, 250);