GUI for java power point, Lecture notes of Computer Science

this documents are very important for java developers

Typology: Lecture notes

2019/2020

Uploaded on 02/19/2023

muluken-kindachew
muluken-kindachew šŸ‡ŖšŸ‡¹

6 documents

1 / 93

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction To Java GUI
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d

Partial preview of the text

Download GUI for java power point and more Lecture notes Computer Science in PDF only on Docsity!

Introduction To Java GUI

Simplest GUI programming:

JOptionPane

An option pane is a simple dialog box for graphical input/output

  • advantages: - (^) simple - (^) flexible (in some ways) - looks better than the black box of doom
  • (^) disadvantages:

     created with static methods; 

not very object-oriented

  • (^) not very powerful (just simple dialog boxes)

JOptionPane examples 1

  • showMessageDialog analogous to

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

  • showConfirmDialog analogous to a

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

  • (^) Swing component inheritance hierarchy

     Component defines methods that can be used in its subclasses 

(for example, paint and repaint)

  • Container - collection of related components - (^) When using JFrames, attach components to the content

pane (a Container)

  • Method add to add components to content pane

JComponent - superclass to most Swing components

  • (^) Much of a component's functionality inherited from these

classes

java.awt.Component

java.awt.Container

java.lang.Object

javax.swing.JComponent

JLabel

  • Labels - Provide text instructions on a GUI - Read-only text - (^) Programs rarely change a label's contents - (^) Class JLabel (subclass of JComponent)
  • (^) Methods
    • (^) Can declare label text in constructor
    • myLabel.setToolTipText( "Text" ) - (^) Displays "Text"in a tool tip when mouse over label
    • myLabel.setText( "Text" )
    • myLabel.getText()
  1. import

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

  • Area for manipulating multiple lines of text - Like JTextField, inherits from JTextComponent - Many of the same methods
  • JScrollPane - Provides scrolling - Initialize with component - new JScrollPane( myComponent ) - Can set scrolling policies (always, as needed, never) - (^) See book for details

JTextArea

  • Box container - Uses BoxLayout layout manager - (^) Arrange GUI components horizontally or vertically - Box b = Box.createHorizontalbox(); - Arranges components attached to it from left to right, in order

attached

  1. main

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

  • (^) Methods (continued)

     setEditable( boolean ) - (^) If true, user can edit text 
    • getPassword - (^) Class JPasswordField - (^) Returns password as an array of type char
  • Example - Create JTextFields and a JPasswordField - Create and register an event handler - (^) Displays a dialog box when Enter pressed

JButton

  • Button - Component user clicks to trigger an action - (^) Several types of buttons - (^) Command buttons, toggle buttons, check boxes, radio buttons
  • Command button - Generates ActionEvent when clicked - Created with class JButton - (^) Inherits from class AbstractButton
  • Jbutton - (^) Text on face called button label - Each button should have a different label - Support display of Icons