Download presentation
Presentation is loading. Please wait.
Published byBrooke Patrick Modified over 8 years ago
1
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004
2
Celsius Converter import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CelsiusConverter implements ActionListener { JFrame converterFrame; JPanel converterPanel; JTextField textfieldCelsius; JLabel celsiusLabel, fahrenheitLabel; JButton convertButton; public static void main(String[] args) { CelsiusConverter converter = new CelsiusConverter(); } … }
3
Celsius Converter public CelsiusConverter() { converterFrame = new JFrame("Convert Celsius to Fahrenheit"); converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the panel. converterPanel = new JPanel(new GridLayout(2, 2)); //Add the components. addComponents(); //Set the default button. converterFrame.getRootPane().setDefaultButton(convertButton); //Add the panel to the window. converterFrame.getContentPane().add(converterPanel, BorderLayout.CENTER); //Display the window. converterFrame.pack(); converterFrame.setVisible(true); }
4
Celsius Converter private void addComponents() { //Create components. textfieldCelsius = new JTextField(2); celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT); convertButton = new JButton("Convert"); fahrenheitLabel = new JLabel("Fahrenheit ", SwingConstants.LEFT); //Listen to events from the Convert button. convertButton.addActionListener(this); //Add the components to the container. converterPanel.add(textfieldCelsius); converterPanel.add(celsiusLabel); converterPanel.add(convertButton); converterPanel.add(fahrenheitLabel); celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); }
5
Painting import java.awt.*; import javax.swing.*; public class paintingExample1 extends JFrame { public static void main(String args[]) { paintingExample frame = new paintingExample(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,400); frame.setVisible(true); } public void paint(Graphics g) { int framewidth = this.getWidth(); int frameheight = this.getHeight(); Graphics2D g2d = (Graphics2D) g; g2d.setBackground(Color.WHITE); g2d.setColor(Color.BLUE); g2d.clearRect(0,0, framewidth, frameheight); g2d.drawLine(10, 40, 150, 300); g2d.drawRect(200, 200, 50, 100); }
6
Adding Interactivity We ’ ll make the following changes: public class paintingExample extends JFrame implements MouseMotionListener { int x = 150; int y = 300; –In main() add: frame.addMouseMotionListener(frame); g2d.drawLine(90, 140, x, y);
7
Event Handlers public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); this.repaint(); } public void mouseMoved(MouseEvent e) { }
8
Multiple Buttons // create the buttons JButton buttonInc = new JButton("Increase"); buttonInc.setActionCommand("increase"); buttonInc.addActionListener(mainObj); JButton buttonDec = new JButton("Decrease"); buttonDec.addActionListener(mainObj); buttonDec.setActionCommand("decrease");
9
Multiple Buttons public void actionPerformed(ActionEvent e) { if(e.getActionCommand() == "increase") numClicks++; else if (e.getActionCommand() == "decrease") numClicks--; label.setText(labelPrefix + numClicks); }
10
Check Boxes
11
Initialization Code: chinButton = new JCheckBox("Chin"); chinButton.setSelected(true); chinButton.addItemListener(this); If you want to do something in the event the user clicks a check box –The class should implement ItemListener –Create an event handler method as follows: public void itemStateChanged(ItemEvent e) { } To find out if the box is checked call: –chinButton.isSelected();
12
Radio Buttons
13
Initialization Code: JRadioButton birdButton = new JRadioButton(birdString); birdButton.setActionCommand(birdString); birdButton.setSelected(true); JRadioButton catButton = new JRadioButton(catString); catButton.setActionCommand(catString); ButtonGroup group = new ButtonGroup(); group.add(birdButton); group.add(catButton); birdButton.addActionListener(this); catButton.addActionListener(this);
14
Radio Buttons If you want to do something in the event the user clicks a radio button –Same as a regular JButton (implement ActionListener and the method actionPerformed() ) To find out if the radio button is selected –birdButton.isSelected() ;
15
Combo Boxes
16
Creating String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; //Create the combo box, select item at index 4. //Indices start at 0, so 4 specifies the pig. JComboBox petList = new JComboBox(petStrings); petList.setSelectedIndex(4); petList.addActionListener(this);
17
Combo Boxes Handling Events public class ComboBoxDemo... implements ActionListener {... petList.addActionListener(this) {... public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String petName = (String)cb.getSelectedItem(); // code to handle the event }... }
18
Further Reading http://java.sun.com/docs/books/tutorial/uiswing/TOC.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.