Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
java.lang.Object
|
java.awt.Component
|
java.awt.Container
|
java.awt.Panel
|
java.applet.Applet
|
javax.swing.JApplet
…so you can display things in an
Example: A "Life" applet
Container (Applet)
Containers (Panels)
Component (Canvas)
Components (Buttons)
Components (TextFields)
Components (Labels)
Some types of components
Button Checkbox
Label
Scrollbar
Choice
TextArea
TextField List
Button
Checkbox
CheckboxGroup
Swing Component Classes
CLASS DESCRIPTION
AbstractButton Abstract superclass for Swing
buttons.
ButtonGroup Encapsulates a mutually exclusive
set of buttons.
ImageIcon Encapsulates an icon
JApplet The Swing version of Applet.
JButton The Swing push button class
JCheckBox The Swing check box class.
JLabel The Swing version of a label.
JComboBox Encapsulates a combo box
JRadioButton The Swing version of a radio
button.
JScrollPane Encapsulates a scrollable window.
void createMyGUI() {
... add components ...
pack(); // compute the size and lay it out
setVisible(true); // make the JFrame visible
}
}
Components Tour
Icons
ImageIcon(Stringfilename)
ImageIcon(URL url)
Methods:
int getIconHeight( )
Returns the height of the icon in pixels.
int getIconWidth( )
Returns the width of the icon in pixels.
void paintIcon(Component comp,Graphics g,int x,
int y)
Paints the icon at position x, y on the graphics context g.
Additional information about the paint operation can be
provided in comp.
Labels
JLabel(Icon i) LEFT
RIGHT
JLabel(String s) CENTER
JLabel(String s, Icon i, int align).
Method associated with icon & label.
Icon getIcon( )
String getText( )
void setIcon(Icon i)
void setText(String s)
Example
public class JLabelDemo extends JApplet {
public void init() {
// Get content pane
Container contentPane = getContentPane();
// Create an icon
ImageIcon ii = new ImageIcon("france.gif");
// Create a label
JLabel jl = new JLabel("France", ii,
JLabel.CENTER);
// Add label to the content pane
contentPane.add(jl);
}
}
TextFields & TextAreas
JTextField( )
JTextField(int cols)
JTextField(String s, int cols)
JTextField(String s)
JRadioButton(Icon i)
JRadioButton(Icon i, boolean state)
JRadioButton(String s)
JRadioButton(String s, boolean state)
JRadioButton(String s, Icon i)
JRadioButton(String s, Icon i, boolean
state)
Combo Boxes
JComboBox( )
Method:-
addItem( itemname in)
Lists
JList()
JList(Object[])
Method:-
setListData()
setVisibleRowCount(int c)
getSelectedValues()
Example
public class Subscriptions extends JFrame {
String[] subs = { "aWT","java","advaced java","software engg.","multimedia
tech.","management","MIS","Operating sys.","LINUX","BYE HAPPY TO READ" };
JList subList = new JList(subs);
public Subscriptions() {
super("Subscriptions");
setSize(150, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel subLabel = new Jlabel(subs);
panel.add(subLabel);
subList.setVisibleRowCount(5);
JScrollPane scroller = new JScrollPane(subLabel);
panel.add(scroller);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
Subscriptions app = new Subscriptions(); } }
Output
Tabbed Panes
STEP 1:-
public class JTabbedPaneDemo extends
JApplet {
public void init() {
JTabbedPane jtp = new JTabbedPane();
jtp.addTab("Cities", new CitiesPanel());
jtp.addTab("Colors", new ColorsPanel());
jtp.addTab("Flavors", new FlavorsPanel());
getContentPane().add(jtp);
}
} STEP
4:-
STEP 2:-
class CitiesPanel extends JPanel {
public CitiesPanel() {
JButton b1 = new JButton("New York");
add(b1);
JButton b2 = new JButton("London");
add(b2);
JButton b3 = new JButton("Hong Kong");
add(b3);
JButton b4 = new JButton("Tokyo");
add(b4);
}
}
STEP 3:- i.e Repeating STEP 2
class ColorsPanel extends class FlavorsPanel
JPanel { extends JPanel {
public ColorsPanel() { public FlavorsPanel() {
JCheckBox cb1 = new JComboBox jcb = new
JCheckBox("Red"); JComboBox();
add(cb1); jcb.addItem("Vanilla");
JCheckBox cb2 = new jcb.addItem("Chocolate");
JCheckBox("Green");
jcb.addItem("Strawberry")
add(cb2); ;
JCheckBox cb3 = new add(jcb);
JCheckBox("Blue");
add(cb3); }
} }
}
Output
ScrollPanes
JScrollPane(Component comp)
JScrollPane(int vsb, int hsb)
JScrollPane(Component comp, int vsb, int
hsb)
VERTICAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_AS_NEEDE
D
VERTICAL_SCROLLBAR_NEVER
HORIZONTAL_SCROLLBAR_ALWAYS
HORIZONTALL_SCROLLBAR_AS_NEEDED
HORIZONTAL_SCROLLBAR_NEVER
ScrollPaneConstants
Example
public class JScrollPaneDemo // Add panel to a scroll pane
extends JApplet { int v =
public void init() {
ScrollPaneConstants.VERTICA
// Get content pane L_SCROLLBAR_AS_NEEDED;
Container contentPane =
getContentPane(); int h =
ScrollPaneConstants.HORIZ
contentPane.setLayout(new ONTAL_SCROLLBAR_AS_NEE
BorderLayout());
DED;
// Add 400 buttons to a panel
JPanel jp = new JPanel();
JScrollPane jsp = new
JScrollPane(jp, v, h);
jp.setLayout(new GridLayout(20,
20)); // Add scroll pane to the
int b = 0; content pane
for(int i = 0; i < 20; i++) { contentPane.add(jsp,
for(int j = 0; j < 20; j++) {
BorderLayout.CENTER);
jp.add(new JButton("Button " + }
b)); }
++b;
}}
Output
Table
JSlider:
JToolBar:
JProgressBar:
Different
types of JDialogBoxes:
Etc………..