





















































































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
this documents are very important for java developers
Typology: Lecture notes
1 / 93
This page cannot be seen from the preview
Don't miss anything!






















































































Simplest GUI programming:
JOptionPane
An option pane is a simple dialog box for graphical input/output
created with static methods; not very object-oriented
JOptionPane examples 1
System.out.println for displaying a simple
message
import javax.swing.*;
class MessageDialogExample {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,
"How's the weather?");
JOptionPane.showMessageDialog(null,
"Second message");
}
}
JOptionPane examples 2
System.out.print that prints a question, then reading
an input value from the user (can only be one of the provided
choices)
import javax.swing.*;
class ConfirmDialogExample {
public static void main(String[] args) {
int choice = JOptionPane.showConfirmDialog(null,
"Erase your hard disk?");
if (choice == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Disk erased!");
} else {
JOptionPane.showMessageDialog(null, "Cancelled.");
}
}
}
Swing Overview
Component defines methods that can be used in its subclasses (for example, paint and repaint)
pane (a Container)
JComponent - superclass to most Swing components
classes
JLabel
1.1 extend JFrame
1.2 Declarations
1.3 Create container to
hold labels
1.4 Initialize JLabels
1 // Fig. 29.4: LabelTest.java
2 // Demonstrating the JLabel class.
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
7 public class LabelTest extends JFrame {
8 private JLabel label1, label2, label3;
10 public LabelTest()
12 super( "Testing JLabel" );
14 Container c = getContentPane();
15 c.setLayout( new FlowLayout() );
17 // JLabel constructor with a string argument
18 label1 = new JLabel( "Label with text" );
19 label1.setToolTipText( "This is label1" );
20 c.add( label1 );
22 // JLabel constructor with string, Icon and
23 // alignment arguments
24 Icon bug = new ImageIcon( "bug1.gif" );
25 label2 = new JLabel( "Label with text and icon",
26 bug, SwingConstants.LEFT );
27 label2.setToolTipText( "This is label2" );
28 c.add( label2 );
30 // JLabel constructor no arguments
1.4 Initialize JLabels
31 label3 = new JLabel();
32 label3.setText( "Label with icon and text at bottom" );
33 label3.setIcon( bug );
34 label3.setHorizontalTextPosition(
35 SwingConstants.CENTER );
36 label3.setVerticalTextPosition(
37 SwingConstants.BOTTOM );
38 label3.setToolTipText( "This is label3" );
39 c.add( label3 );
41 setSize( 275, 170 );
42 show();
45 public static void main( String args[] )
47 LabelTest app = new LabelTest();
JTextArea
JTextArea
attached
31 t2.setText( t1.getSelectedText() );
35 b.add( copy );
37 t2 = new JTextArea( 10, 15 );
38 t2.setEditable( false );
39 b.add( new JScrollPane( t2 ) );
41 Container c = getContentPane();
42 c.add( b );
43 setSize( 425, 200 );
44 show();
47 public static void main( String args[] )
49 TextAreaDemo app = new TextAreaDemo();
Program Output
JTextField and JPasswordField
setEditable( boolean ) - (^) If true, user can edit text JButton