Download Gui Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Further GUI Programming
• Learning Outcomes
o Extend examples presented to write more useful applications.
o Write non-trivial, event-driven GUI applications.
• Introduction
• Example 1: Menu Test
• Example 2: File Dialog Test
• Example 3: Popup Menu Test
• Self-check Exercise 1
• Exercises
Unit 14
1
Introduction
• This is our concluding session on GUIs and event-driven programming.
• In our discussion on GUI-based programs so far, we have covered:
– GUI design issues, GUI components and containers.
– Basic event handling.
– Basic layout management.
• We now cover typical GUI elements found in Windows environment
including:
– Menus and pop-up menus
– File dialogs
– Mnemonics and keyboard accelerator
• With this coverage the student's knowledge base of the basics would
•
be broadened.
Unit 14
2
Class Hierarchy for Menus
• Classes used to define menus are:
o JMenuBar
o JMenuItem
o JMenu
o JCheckButtonMenuItem
o JRadioButtonMenuItem
Unit 14
3
Introduction to Example 1
• In this example we demonstrate how menus, separator,
mnemonic and accelerators can be added into an
application.
Unit 14
4
Details of Program Output
Unit 14
5
Example 1: Menus and Mnemonics
1 import java.awt.event.*;
2 import javax.swing.*;
3 class MenuTest extends JFrame {
4
private JMenuBar menuBar = new JMenuBar();
5
protected JMenu fileMenu = new JMenu("File");
6
protected JMenuItem neW, open, quit, save, print;
7
private JMenuItem saveCurrent, saveAs, saveAll;
8
MenuTest(String title){
9
super(title);
10
setJMenuBar(menuBar);
11
menuBar.add(fileMenu);
12
fileMenu.setMnemonic('F');
13
fileMenu.add(neW = new JMenuItem ("New"));
14
fileMenu.add(open = new JMenuItem ("Open"));
15
open.setMnemonic('o');
16
fileMenu.add(save = new JMenu ("Save"));
17
save.add(saveCurrent = new JMenuItem ("Save Current"));
18
save.add(saveAs
= new JMenuItem ("Save As"));
19
save.add(saveAll = new JMenuItem ("Save All"));
20
fileMenu.add(save);
21
fileMenu.add(print = new JCheckBoxMenuItem ("Print"));
22
fileMenu.addSeparator();
Unit 14
6
Menus and Mnemonics (cont’d)
23
fileMenu.add(quit = new JMenuItem("Quit"));
24
quit.setMnemonic(KeyEvent.VK_Q);
25
quit.setAccelerator(
26
KeyStroke.getKeyStroke(KeyEvent.VK_Q,KeyEvent.CTRL_MASK));
27
quit.addActionListener(new ActionListener(){
28
public void actionPerformed(ActionEvent e){
29
int result=JOptionPane.showConfirmDialog(MenuTest.this,
30
"Quit/Close Menu Demos Window?");
31
if (result == JOptionPane.YES_OPTION)
32
System.exit(0);
33
}
34
});
35
setSize(300,300);
36 }
37 public static void main(String args[]){
38
MenuTest t = new MenuTest("Menus Test");
39
t.setVisible(true);
40 }
41 }
Unit 14
7
Introduction to Example 2
• This example extends Example 1 to launch a file dialog on selecting
the Open menu.
• The selected file name is simply printed on another display window.
• The output is as follows:
Unit 14
8
Example 2: File Dialog Test
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 class FileDialogTest extends MenuTest {
5
JEditorPane textPane= new JEditorPane();
6
FileDialogTest(String title){
7
super(title);
8
open.addActionListener( new ActionListener() {
9
public void actionPerformed(ActionEvent e ) {
10
FileDialog fd = new FileDialog(FileDialogTest.this);
11
fd.setVisible(true);
12
textPane.setText("Selected file: "+fd.getFile());
13
} });
14
getContentPane().add(textPane);
15 }
16 public static void main(String args[]){
17
FileDialogTest t = new FileDialogTest("File Dialog Test");
18
t.setVisible(true);
19
20 }
21 }
Unit 14
9
Related documents