Presentation is loading. Please wait.

Presentation is loading. Please wait.

SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes.

Similar presentations


Presentation on theme: "SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes."— Presentation transcript:

1 SD2054 Software Development

2

3 By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes create option dialogues create message dialogues create input dialogues create confirm dialogues.

4 import javax.swing.*;

5 Making choices pull-down menus combo boxes

6 Pull-down menus

7

8

9

10

11

12 JFrame

13 public class Flag extends JFrame implements ActionListener { }

14 Declare menu components as attributes of the Flag class

15

16 JMenuBar

17 private JMenuBar bar = new JMenuBar();

18

19 JMenu

20 private JMenu topStripeMenu = new JMenu("Top Colour");

21

22 private JMenu middleStripeMenu = new JMenu(“Middle Colour");

23

24 private JMenu bottomStripeMenu = new JMenu(“Bottom Colour");

25

26 JMenuItem

27 private JMenuItem blueStripe = new JMenuItem("Blue");

28

29 private JMenuItem redStripe = new JMenuItem(“Red");

30 Assemble menu components onto the screen in the Constructor

31 public Flag( ) { setVisible(true); }

32 First, add menu-items …..

33 … to menu

34 blueStripe

35 topStripeMenu blueStripe

36 topStripeMenu.add(blueStripe); topStripeMenu blueStripe

37 topStripeMenu redStripe

38 topStripeMenu redStripe topStripeMenu.add(redStripe);

39 Then, add menu …..

40 … to menu-bar

41 bar

42 topStripeMenu bar

43 topStripeMenu bar bar.add(topStripeMenu);

44 Finally, add menu-bar …..

45 …. to frame

46 bar

47 setJMenuBar(bar);

48 Processing menu choices…..

49 ..first, give each menu item a listener in the constructor.

50 blueStripe

51 blueStripe.addActionListener(this);

52 ..then write the code to process a menu item in the actionPerformed method.

53 blueStripe public void actionPerformed(ActionEvent e) { }

54 public void actionPerformed(ActionEvent e) { } if (e.getSource() == blueStripe) { // code to process menu choice here }

55 JPanel public void actionPerformed(ActionEvent e) { } if (e.getSource() == blueStripe) { // code to process menu choice here }

56 topPanel public void actionPerformed(ActionEvent e) { } if (e.getSource() == blueStripe) { // code to process menu choice here }

57 topPanel public void actionPerformed(ActionEvent e) { } if (e.getSource() == blueStripe) { topPanel.setBackground(Color.blue); }

58 public void actionPerformed(ActionEvent e) { } if (e.getSource() == blueStripe) { topPanel.setBackground(Color.blue); }

59 The JComboBox class

60

61 private String[ ] colours = {"Select colour","Red", "Blue", "Green"}; private JComboBox box = new JComboBox(colours);

62 box.addActionListener(this);

63 String item = box.getSelectedItem();

64 String item = box.getSelectedItem();

65 String item = (String) box.getSelectedItem();

66

67 if (item.equals("Red")) { getContentPane().setBackground(Color.red); }

68 if (item.equals("Red")) { getContentPane().setBackground(Color.red); }

69 box.setSelectedIndex(0);

70 box.setSelectedIndex(0);

71 The JOption Class creating option dialogues creating message dialogues creating input dialogues creating confirm dialogues.

72 The JOptionPane dialogue class

73 Option Dialogue

74

75

76

77

78 String[ ] choice = {"Enter a word", "Find the secret", "Quit"};

79 String[ ] choice = {"Enter a word", "Find the secret", "Quit"};

80 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

81 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

82 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word"); indicates no parent frame

83 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word"); Could use the this keyword if there is a parent frame.

84 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

85 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

86 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

87 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

88 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

89 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word"); or an icon of your choice

90 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

91 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

92 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");

93 result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word"); 0 = button 1 1 = button 2 2 = button 3

94 The JOptionPane dialogue class

95 Input Dialogue

96

97 word = JOptionPane.showInputDialog (null, "Enter a word", null, JOptionPane.PLAIN_MESSAGE);

98 word = JOptionPane.showInputDialog (null, "Enter a word", null, JOptionPane.PLAIN_MESSAGE);

99 word = JOptionPane.showInputDialog (null, "Enter a word", null, JOptionPane.PLAIN_MESSAGE);

100 word = JOptionPane.showInputDialog (null, "Enter a word", null, JOptionPane.PLAIN_MESSAGE);

101 word = JOptionPane.showInputDialog (null, "Enter a word", null, JOptionPane.PLAIN_MESSAGE);

102 word = JOptionPane.showInputDialog (null, "Enter a word", null, JOptionPane.PLAIN_MESSAGE);

103 The JOptionPane dialogue class

104 Message Dialogue

105

106 JOptionPane.showMessageDialog (null, “You did not enter a magic word”, null, JOptionPane.INFORMATION_MESSAGE);

107 JOptionPane.showMessageDialog (null, “You did not enter a magic word”, null, JOptionPane.INFORMATION_MESSAGE);

108 JOptionPane.showMessageDialog (null, “You did not enter a magic word”, null, JOptionPane.INFORMATION_MESSAGE);

109 JOptionPane.showMessageDialog (null, “You did not enter a word”, null, JOptionPane.INFORMATION_MESSAGE);

110 JOptionPane.showMessageDialog (null, “You did not enter a word”, null, JOptionPane.INFORMATION_MESSAGE);

111 JOptionPane.showMessageDialog (null, “You did not enter a word”, null, JOptionPane.ERROR_MESSAGE);

112 The JOptionPane dialogue class

113 Confirm Dialogue

114 answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

115 answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

116 answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

117 answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

118 answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

119 if(answer == JOptionPane.YES_OPTION) { // process choice here }

120


Download ppt "SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes."

Similar presentations


Ads by Google