sd2054 software development
DESCRIPTION
SD2054 Software Development. Advanced Graphics Programming. By the end of this lecture you should be able to:. create pull down menus create combo boxes create option dialogues create message dialogues create input dialogues create confirm dialogues. import javax.swing .*;. - PowerPoint PPT PresentationTRANSCRIPT
SD2054
Software Development
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.
import javax.swing.*;
Making choices
pull-down menus
combo boxes
Pull-down menus
Pull-down menus
Pull-down menus
Pull-down menus
Pull-down menus
Pull-down menus
Pull-down menus
JFrame
JFrame
public class Flag extends JFrame implements ActionListener{
}
Declare menu components as attributes of the Flag class
JMenuBar
private JMenuBar bar = new JMenuBar();
JMenuBar
JMenu
JMenu
private JMenu topStripeMenu = new JMenu("Top Colour");
JMenu
JMenu
private JMenu middleStripeMenu = new JMenu(“Middle Colour");
JMenu
JMenu
private JMenu bottomStripeMenu = new JMenu(“Bottom Colour");
JMenuItem
JMenuItem
private JMenuItem blueStripe = new JMenuItem("Blue");
JMenuItem
JMenuItem
private JMenuItem redStripe = new JMenuItem(“Red");
Assemble menu components onto the screen in the Constructor
public Flag( ){
setVisible(true);}
First, add menu-items …..
… to menu
blueStripe
topStripeMenu
blueStripe
topStripeMenu.add(blueStripe);
topStripeMenu
blueStripe
topStripeMenu
redStripe
topStripeMenu
redStripe
topStripeMenu.add(redStripe);
Then, add menu …..
… to menu-bar
bar
topStripeMenu
bar
topStripeMenu
bar
bar.add(topStripeMenu);
Finally, add menu-bar …..
…. to frame
bar
bar
setJMenuBar(bar);
Processing menu choices…..
..first, give each menu item a listener in the constructor.
blueStripe
blueStripe
blueStripe.addActionListener(this);
..then write the code to process a menu item in the actionPerformed method.
blueStripe
public void actionPerformed(ActionEvent e){
}
blueStripe
public void actionPerformed(ActionEvent e){
}
if (e.getSource() == blueStripe){ // code to process menu choice here }
JPanel
public void actionPerformed(ActionEvent e){
}
if (e.getSource() == blueStripe){ // code to process menu choice here }
topPanel
public void actionPerformed(ActionEvent e){
}
if (e.getSource() == blueStripe){ // code to process menu choice here }
topPanel
public void actionPerformed(ActionEvent e){
}
if (e.getSource() == blueStripe){ topPanel.setBackground(Color.blue); }
public void actionPerformed(ActionEvent e){
}
if (e.getSource() == blueStripe){ topPanel.setBackground(Color.blue); }
The JComboBox class
The JComboBox class
private String[ ] colours = {"Select colour","Red", "Blue", "Green"};
private JComboBox box = new JComboBox(colours);
box.addActionListener(this);
String item = box.getSelectedItem();
String item = box.getSelectedItem();
String item = (String) box.getSelectedItem();
String item = (String) box.getSelectedItem();
if (item.equals("Red")) { getContentPane().setBackground(Color.red); }
if (item.equals("Red")) { getContentPane().setBackground(Color.red); }
box.setSelectedIndex(0);
box.setSelectedIndex(0);
The JOption Class
• creating option dialogues
• creating message dialogues
• creating input dialogues
• creating confirm dialogues.
The JOptionPane dialogue class
The JOptionPane dialogue class
Option Dialogue
String[ ] choice = {"Enter a word", "Find the secret", "Quit"};
String[ ] choice = {"Enter a word", "Find the secret", "Quit"};
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
indicates no parent
frame
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.
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
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
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
result = JOptionPane.showOptionDialog (null, "Choose an option", "Magic Words",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, choice, "Enter a word");
0 = button 11 = button 22 = button 3
The JOptionPane dialogue class
The JOptionPane dialogue class
Input Dialogue
word = JOptionPane.showInputDialog (null,
"Enter a word", null, JOptionPane.PLAIN_MESSAGE);
word = JOptionPane.showInputDialog (null,
"Enter a word", null, JOptionPane.PLAIN_MESSAGE);
word = JOptionPane.showInputDialog (null,
"Enter a word", null, JOptionPane.PLAIN_MESSAGE);
word = JOptionPane.showInputDialog (null,
"Enter a word", null, JOptionPane.PLAIN_MESSAGE);
word = JOptionPane.showInputDialog (null,
"Enter a word", null, JOptionPane.PLAIN_MESSAGE);
word = JOptionPane.showInputDialog (null,
"Enter a word", null, JOptionPane.PLAIN_MESSAGE);
The JOptionPane dialogue class
The JOptionPane dialogue class
Message Dialogue
JOptionPane.showMessageDialog (null, “You did not enter a magic word”,
null, JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog (null, “You did not enter a magic word”,
null, JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog (null, “You did not enter a magic word”,
null, JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog (null, “You did not enter a word”, null,
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog (null, “You did not enter a word”, null,
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog (null, “You did not enter a word”, null,
JOptionPane.ERROR_MESSAGE);
The JOptionPane dialogue class
The JOptionPane dialogue class
Confirm Dialogue
answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
answer = JOptionPane.showConfirmDialog (null, "Are you sure you want to know the secret?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(answer == JOptionPane.YES_OPTION){
// process choice here
}