+ All Categories
Home > Documents > GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I...

GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I...

Date post: 04-Aug-2020
Category:
Upload: others
View: 9 times
Download: 0 times
Share this document with a friend
38
GUI – Part I Overall Steps for Completing a GUI Application (if you get confused) 1. Design a Layout on Paper a. use “tick” marks for dimensions (pixel dimensions) b. use Layouts (and Layouts within Layouts) 2. Setup classes (files) a. Driver -> I’m going to give you this!!! Don’t touch!! b. GUI class/file -> most construction of GUI 3. set up constructor within GUI class a. where MOST of the code really goes b. Driver (copy, paste and change what I have) 4. Add Actionlistener Designing the Layout Should be your FIRST STEP!!! Without regard to Java, draw up what you would want to a GUI to look like o Share it with someone else, they may have good ideas or questions about the interface Draw a design for a Simple Calculator using the UPPER section of the paper. The Java Components Next, we have to determine WHAT GUI components we want 1
Transcript
Page 1: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

GUI – Part IOverall Steps for Completing a GUI Application(if you get confused)

1. Design a Layout on Papera. use “tick” marks for dimensions (pixel dimensions)b. use Layouts (and Layouts within Layouts)

2. Setup classes (files)a. Driver -> I’m going to give you this!!! Don’t touch!!b. GUI class/file -> most construction of GUI

3. set up constructor within GUI classa. where MOST of the code really goesb. Driver (copy, paste and change what I have)

4. Add Actionlistener

Designing the Layout Should be your FIRST STEP!!! Without regard to Java, draw up what you would want to a GUI to look like

o Share it with someone else, they may have good ideas or questions about the interface

Draw a design for a Simple Calculator using the UPPER section of the paper.

The Java Components Next, we have to determine WHAT GUI components we want http://docs.oracle.com/javase/tutorial/ui/features/components.html Tips and tricks

o Give each GUI components a name use the Component type as part name

JButton => JBexito Create all components in one area of code (specified later)

Easier to find if you need to change something since all in one area

How many JButtons, JTextFields, JRadioButtons, etc… do you need for your design? Write it down on your sheet of paper.

1

Page 2: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Where do we put the code?Three File System

source GUI (JPanel) driver/main()methods involving calculationDATA STRUCTURES!!

all GUI aspectsGUI methods will CALL source methods

start of the entire project(much like C++)

ALWAYS COMPILE FROM HERE!!

Not everyone will use this systemo easier for now until the student gets more experienceo use this today for your examples

Creating the Driver for the First Application before getting into the details, getting the first GUI application running can

be challengingOpen whatever IDE you are using, and download the “Files for Calc” at the website.1. In Eclipse create a new Project2. Copy the code below into your code3. Please note, the code is NOT complete!!

GuiDriverForCalc.java

import javax.swing.*;

public class GuiDriverForCalc{

public static void main(String[] args){

JFrame JFwindow = new JFrame("Lupoli’s Simple Calculator");JFwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// close window when someone hit’s "x"

JFwindow.getContentPane().add(new Calc()); // get’s Calc, and places it into THIS window/frame

JFwindow.pack();JFwindow.setVisible(true);

}}

2

Page 3: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Explaining the JFrame created in the Driver within the main() highest window in the hierarchy items that need to be completed

JFrame explanation in Main()public static void main(String[] args){

JFrame window = new JFrame(“Lupoli’s Simple Calculator”);// creating an instance of the JFrame Window along with a title

JFwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// setting how the window will close this code below closes the window// when someone hit’s “x”

JFwindow.getContentPane().add(new Calc());// add other COMPLETED JPanel CLASSES to the JFrame,// all JPanels will be separate classes

JFwindow.pack();// pack the JPanels together inside the JFrame window

JFwindow.setVisible(true);// enable the JFrame window

}

Organizing Panels always have a game plan remember, the GUI class itself is a Panel!!

o I will call it a “base” panel can add more panels to the base panel but can also add panels to panels!! then the buttons/images, etc.. are added to those panels

3

Page 4: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Scenario #1 – Just the Base Panel

import java.awt.*;import java.awt.event.*;import javax.swing.*;

public class Calc extends JPanel{

private JTextField JTFanswer = new JTextField("0", 40);private JButton JBnum1 = new JButton("1");private JButton JBnum2 = new JButton("2");private JButton JBnum3 = new JButton("3");

Calc(){

// adding directly to the base panelthis.add(JTFanswer);this.add(JBnum1);this.add(JBnum2);this.add(JBnum3);

}}

// create the rest of the buttons (0-9) and add them to the base panel

4

Page 5: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Scenario #2 – Adding sub panels to the Base Panel

public class Calc extends JPanel{

private JTextField JTFanswer = new JTextField("0", 40);private JButton JBnum1 = new JButton("1");private JButton JBnum2 = new JButton("2");private JButton JBnum3 = new JButton("3");

Calc(){

// creating sub panels Left and Right!!JPanel JPLeft = new JPanel();JPLeft.setBackground(Color.RED); // must add importJPLeft.setPreferredSize(new Dimension(200, 50));JPLeft.add(JBnum1);

JPanel JPRight = new JPanel();JPRight.setBackground(Color.BLUE);JPRight.setPreferredSize(new Dimension(150, 90));JPRight.add(JBnum2);

// now adding the sub panels BACK to the BASE panelthis.add(JPLeft);

this.add(JPRight);}

}

5

Page 6: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Scenario #3 – Forgot to add a panel to the basepublic class Calc extends JPanel{

private JTextField JTFanswer = new JTextField("0", 40);private JButton JBnum1 = new JButton("1");private JButton JBnum2 = new JButton("2");private JButton JBnum3 = new JButton("3");

Calc(){

// creating sub panels Left and Right!!JPanel JPLeft = new JPanel();JPLeft.setBackground(Color.RED); // must add importJPLeft.setPreferredSize(new Dimension(200, 50));JPLeft.add(JBnum1);

JPanel JPRight = new JPanel();JPRight.setBackground(Color.BLUE);JPRight.setPreferredSize(new Dimension(150, 90));JPRight.add(JBnum2);

JPanel JPRight2 = new JPanel();JPRight2.setBackground(Color.YELLOW);JPRight2.setPreferredSize(new Dimension(150, 90));JPRight2.add(JBnum3);

// now adding the sub panels BACK to the BASE panelthis.add(JPLeft);

this.add(JPRight); //this.add(JPRight2);}

}

6

Page 7: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Scenario #4 – Correctly Adding 3 subpanels to the Base panel

public class Calc extends JPanel{

private JTextField JTFanswer = new JTextField("0", 40);private JButton JBnum1 = new JButton("1");private JButton JBnum2 = new JButton("2");private JButton JBnum3 = new JButton("3");

Calc(){

// creating sub panels Left and Right!!JPanel JPLeft = new JPanel();JPLeft.setBackground(Color.RED); // must add importJPLeft.setPreferredSize(new Dimension(200, 50));JPLeft.add(JBnum1);

JPanel JPRight = new JPanel();JPRight.setBackground(Color.BLUE);JPRight.setPreferredSize(new Dimension(150, 90));JPRight.add(JBnum2);

JPanel JPRight2 = new JPanel();JPRight2.setBackground(Color.YELLOW);JPRight2.setPreferredSize(new Dimension(150, 90));JPRight2.add(JBnum3);

// now adding the sub panels BACK to the BASE panelthis.add(JPLeft);

this.add(JPRight); this.add(JPRight2);}

}

7

Page 8: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Creating the GUI File for the First Application right now, we can create the GUI components we need but we need to do much more before a window will work notice a NEW CLASS!! (Look at which file pictured)

o the class is an extended JPanel!!

Calc.java

import java.awt.*;import java.awt.event.*;import javax.swing.*;

public class Calc extends JPanel{

private JTextField JTFanswer = new JTextField("0", 40); // 0 is placed into the text, 40 chars long

private JButton JBnum1 = new JButton("1"); //text on Button is ‘1’

1. Inside your “Calc” constructor, set the base panel’s background color to GREEN and the size (400x400), etc… Have fun!

a. You are NOT touching the driver file!!!b. Figure out the code for setting the base panel’s color and dimension.

Yes. I have not exactly given it to you, but use previous code!!c. No subpanels are needed!!

2. Below the creation of all of the JButtons (0-9), finish creating the code for the operation buttons (+ - / * =) for your Java Components for the Simple Calculator.

3. Also, on the back on your paper design, mark the name given on each Java Component

8

Page 9: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

The Base JPanel file setup each Master JPanel needs to contain

o private “section” where ALL GUI components for that JPanel are created

o constructor **** where background and preferred size are set where a LAYOUT is determined remember this for later!!

will cover in a minute where GUI items are added to the JPanel many examples to follow

o methods many examples to follow

o an internal class for an ActionListener will cover that in a minute

JPanel File with GUI interfaceimports…

class somethingPanel extends JPanel{// all GUI Componentsprivate JButton JButton1 = new JButton(); // instance of original class…

Constructor/GUI Setup

Methods

ActionListener CLASS

}

9

Page 10: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Types of Layouts JPanel must be created first, class extension, Import java.awt.*; download code for these, LayoutExamples.zip all coded examples are for the BASE panel

o easy adapted for a sub panelo subpanel.setLayout(…..)

Borderlayouto BROKEN UP INTO SECTIONS

all code (for base or sub panels) are placed inside the constructor

BorderLayout JPanelNorth

West Center (Greedy!!) EastSouth

Calc() // Calc constructor{

this.setLayout(new BorderLayout()); // this line goes INSIDE the constructor (in Calc)…

FlowLayouto default setting for ALL panels!! o Single Panelo Flows GUI components left to righto Great for laying buttons togethero Left justified

FlowLayout JPanel

Calc() // Calc constructor

GUI Component1 GUI Component2 GUI Component3

10

Page 11: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

{this.setLayout(new FlowLayout());// this line goes INSIDE the constructor (in Calc)

Grid Layouto sets GUI component into a matrix formato great for laying buttons togethero to set inside the container

GridLayout 2x3 Example

Calc() // Calc constructor{

this.setLayout(new GridLayout(2, 3)); // ROW, then COLUMN…

GUI Component1 GUI Component2 GUI Component3GUI Component4 GUI Component5 GUI Component6

11

Page 12: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

GridLayout Exampleimport java.awt.Color;import java.awt.Dimension;import java.awt.GridLayout;import javax.swing.JPanel;

public class Master extends JPanel{

public Master (){

//setLayout(new GridLayout(2, 4));this.setLayout(new GridLayout(4, 2));this.setSize(new Dimension(300,300));

// internal JpanelsJPanel LeftWindow = new JPanel();LeftJFwindow.setBackground(Color.RED); // must add importLeftJFwindow.setPreferredSize(new Dimension(150, 90));

JPanel RightWindow = new JPanel();RightJFwindow.setBackground(Color.BLUE);RightJFwindow.setPreferredSize(new Dimension(150, 90));

JPanel TopWindow = new JPanel();TopJFwindow.setBackground(Color.YELLOW);TopJFwindow.setPreferredSize(new Dimension(150, 90));

JPanel BottomWindow = new JPanel();BottomJFwindow.setBackground(Color.GREEN);BottomJFwindow.setPreferredSize(new Dimension(150, 90));

JPanel WestWindow = new JPanel();WestJFwindow.setBackground(Color.CYAN);WestJFwindow.setPreferredSize(new Dimension(150, 90));

JPanel EastWindow = new JPanel();EastJFwindow.setBackground(Color.PINK);EastJFwindow.setPreferredSize(new Dimension(150, 90));

// Add sub JPanels to Master JPanel this.add(LeftWindow); this.add(RightWindow); this.add(TopWindow); this.add(BottomWindow); this.add(WestWindow); this.add(EastWindow);

}}

4 x 2, why white space?

2 x 4

Notice the shape difference!!!

12

Page 13: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

BoxLayouto a set of GUI Components in a row, or in a column

this.setLayout( new BoxLayout(this, BoxLayout.X_AXIS) ); // or X_AXISnorth.setLayout( new BoxLayout(north, BoxLayout.Y_AXIS) ); // or X_AXIS

Horizontal Box Layout Example (X_AXIS)

Vertical Box Layout Example (Y_AXIS)

GUI GUI GUI GUIGUI GUIGUIGUI

13

Page 14: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Layouts in action showing the more popular ones with GUI components also showing two versions

o just using the baseo creating a simple subpanel, then adding that to the base

both o will show the SAME GUI o are using the same JButtons

BorderLayoutJust the base Using SubPanelsimport java.awt.*;import java.awt.event.*;import javax.swing.*;

public class Calc extends JPanel{

private JButton JBnum1 = new JButton("1");

private JButton JBnum2 = new JButton("2");

private JButton JBnum3 = new JButton("3");

private JButton JBnum4 = new JButton("4");

private JButton JBnum5 = new JButton("5");

private JButton JBnum6 = new JButton("6");

Calc(){

this.setLayout(new BorderLayout());

this.add(JBnum1, BorderLayout.NORTH);

this.add(JBnum2, BorderLayout.EAST);

this.add(JBnum3, BorderLayout.WEST);

this.add(JBnum4, BorderLayout.SOUTH);

this.add(JBnum5, BorderLayout.CENTER);

}}

import java.awt.*;import java.awt.event.*;import javax.swing.*;

public class Calc extends JPanel{

private JButton JBnum1 = new JButton("1");private JButton JBnum2 = new JButton("2");private JButton JBnum3 = new JButton("3");private JButton JBnum4 = new JButton("4");private JButton JBnum5 = new JButton("5");private JButton JBnum6 = new JButton("6");

Calc(){

JPanel subPanel = new JPanel();subPanel.setLayout(new

BorderLayout());subPanel.add(JBnum1,

BorderLayout.NORTH);subPanel.add(JBnum2,

BorderLayout.EAST);subPanel.add(JBnum3,

BorderLayout.WEST);subPanel.add(JBnum4,

BorderLayout.SOUTH);subPanel.add(JBnum5,

BorderLayout.CENTER);

// add everything to basethis.add(subPanel);

}}

14

Page 15: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

GridLayout 2 x 3Just the base Using SubPanelsimport java.awt.*;import java.awt.event.*;import javax.swing.*;

public class Calc extends JPanel{

private JButton JBnum1 = new JButton("1");

private JButton JBnum2 = new JButton("2");

private JButton JBnum3 = new JButton("3");

private JButton JBnum4 = new JButton("4");

private JButton JBnum5 = new JButton("5");

private JButton JBnum6 = new JButton("6");

Calc(){

this.setLayout(new GridLayout(2,3));

// rows then columnsthis.add(JBnum1);this.add(JBnum2);this.add(JBnum3);this.add(JBnum4);this.add(JBnum5);this.add(JBnum6);

}}

import java.awt.*;import java.awt.event.*;import javax.swing.*;

public class Calc extends JPanel{

private JButton JBnum1 = new JButton("1");private JButton JBnum2 = new JButton("2");private JButton JBnum3 = new JButton("3");private JButton JBnum4 = new JButton("4");private JButton JBnum5 = new JButton("5");private JButton JBnum6 = new JButton("6");private JLabel JLabel1 = new JLabel("label");

Calc(){

JPanel subPanel = new JPanel();subPanel.setLayout(new

GridLayout(2,3)); // rows then columnssubPanel.add(JBnum1);subPanel.add(JBnum2);subPanel.add(JBnum3);subPanel.add(JBnum4);subPanel.add(JLabel1);subPanel.add(JBnum6);

// add subpanel to basethis.add(subPanel);

}}

1. set your base panel for Calc to GridLayout2. add all your components in a meaningful order to the grid layout3. display it

15

Page 16: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Showing the Different Layouts in Action

“None”o You can specify that a JPanel NOT to have a layouto In only ONE component in a certain area

16

Page 17: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Mixing Layouts OTHER Layouts can be added inside of other Layouts OTHER JPanels can be added to the ORIGINAL JPanel/JFrame

o must watch how you declareo inside THAT new panel can have smaller panels

here are some other possibilities

Mixed Layout ExamplesNorth

Center East

JPanel with a JPanelTitle

JPanel(s)

BorderLayout JPanelNorth

West Center EastSouth

17

Page 18: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Mixed Layout Examplemixing layouts image hereimport java.awt.*;

public class Calc extends JPanel{

private JButton JBnum1 = new JButton("1");private JButton JBnum2 = new JButton("2");private JButton JBnum3 = new JButton("3");private JButton JBnum4 = new JButton("4");private JButton JBnum5 = new JButton("5");private JButton JBnum6 = new JButton("6");private JLabel JLabel1 = new JLabel("l1");private JLabel JLabel2 = new JLabel("l2");private JLabel JLabel3 = new JLabel("l3");private JLabel JLabel4 = new JLabel("l4");

Calc(){

this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

JPanel subPanel = new JPanel();subPanel.setLayout(new GridLayout(2,3)); // rows then columnssubPanel.add(JBnum1);subPanel.add(JBnum2);subPanel.add(JBnum3);subPanel.add(JBnum4);subPanel.add(JBnum5);subPanel.add(JBnum6);

// add subpanel to basethis.add(JLabel1); // leftthis.add(subPanel);this.add(JLabel2);this.add(JLabel3); // rightmost

}}

18

Page 19: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

1. For Calc, Create two JLabels, with “-“ values (or blank)2. Remake your base Layout “BorderLayout”3. CENTER BORDERLAYOUT, use a gridlayout for button 0-9

a. Label, 0 label for the bottom row4. EAST - > = button5. NORTH -> Textbox

19

Page 20: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Overall Placement for GUI Success There is an order to everything! There is also a specific place for everything!!

Overall placement of everything

// all imports go here

class Calc extends JPanel{

Data membersprivate JButton JBnum1 = new JButton(“1”);

// create ALL GUI components here

20

Page 21: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

ConstructorCalc(){

// set base panel defaults// set GUI component attributes (size, color, etc…)// create sub panels (if necessary)// add buttons to panels// add sub panels to base panel// add GUI components to ActionListener

}

ActionListenerprivate class ButtonListener implements ActionListener{// create actionListener class}

}

21

Page 22: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Exercise Set #1 (not longer needed??)Complete this in order in Calc.java

1. Create Calc constructor // do not re-create the buttons!!!2. inside the constructor

a. set layout to BorderLayoutb. create 2 secondary (smaller) JPanels (INSIDE THE CONSTRUCTOR!!)

i. name one NORTH use FlowLayoutii. name other CENTER use GridLayout

c. add JTextField to NORTHd. add all buttons to CENTER JPanel

3. add north and center to Calc’s JPanel4. Compile and run (hopefully)

a. notice that if you hit a button, nothing happens

Exercise Set #2Create a new Project “TicTacToe”9 JButtons, no labelsNotice Color and numbering in PictureGridLayout (3)Driver.java and TicTacToe.java- No internal panels, add buttons directly to Master Panel

22

Page 23: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Introduction to the ActionListener used to set code into effect when an event happens

o like a button being pressed import a java file

o java.awt.event.*; // for Action Listeners is a PRIVATE CLASS INSIDE the GUI Class USES A RESERVED FUNCTION

o used inside the JPANEL classo function header must be:

public void actionPerformed(ActionEvent e)o conditions on where ActionListener function should be placedo each GUI Component that we want to have an event happen:

we must ADD IT TO THE ACTIONLISTENER button1.addActionListener(new ButtonListener());

this line above belo ngs in the constructorAction Listener Class Example

// REMEMBER, this code is WITHIN a GUI CLASS!!private class ButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e){

String actionCommand = e.getActionCommand();// gets what was written on GUI Component

if(actionCommand.equals("1")){ JOptionPane.showMessageDialog(null, "You pressed BUTTON 1"); return; }if(actionCommand.equals("2")){ JOptionPane.showMessageDialog(null, "You pressed BUTTON 2"); return; }if(actionCommand.equals("3")){ JOptionPane.showMessageDialog(null, "You pressed BUTTON 3"); return; }

// THIS IS WHERE YOUR GAME MECHANICS is done!!!}

}// ADD THIS to your Calc, below the constructor

Don’t forget to add them in the Constructor!!

// add actionlistener to GUI ComponentsJBnum1.addActionListener(new ButtonListener());JBnum2.addActionListener(new ButtonListener());

23

Page 24: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

JBnum3.addActionListener(new ButtonListener());

24

Page 25: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

Determining which GUI component was selected by “text” on the component

o good for label, button, etc…o matches what was written on the button

ActionListener that matches text on Componentpublic void actionPerformed(ActionEvent e){String actionCommand = e.getActionCommand(); // gets what was written on GUI Component

if(actionCommand.equals("Button 1")){ … }

by component o named in private section of the JPanel class

ActionListener that matches Component’s namepublic void actionPerformed(ActionEvent event){

if(event.getSource() == JBnum1) // Button 1 was hit{ …}

ActionListener Setup ComparisonText Match Variable name matchCalc(){this.add(JTFanswer);this.add(JBnum1);this.add(JBnum2);this.add(JBnum3);

// add actionlistener to GUI ComponentsJBnum1.addActionListener(new ButtonListener());JBnum2.addActionListener(new ButtonListener());JBnum3.addActionListener(new ButtonListener());}

SAME!!

private class ButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand();

if(actionCommand.equals("1")){ JOptionPane….1"); return; }if(actionCommand.equals("2")){ JOptionPane….2"); return; }if(actionCommand.equals("3")){ JOptionPane….3"); return; }

}} // code has been shortened!!

private class ButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e){

if(e.getSource() == JBnum1){ JOptionPane…. 1"); return; }if(e.getSource() == JBnum2){ JOptionPane…. 2"); return; }if(e.getSource() == JBnum3){ JOptionPane….3"); return; }

}} // code has been shortened!!

25

Page 26: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

First Example with an ActionListener – (Text Match) it also has a few GUI Components (JButtons) matches the NAME written on the Component

LeftWindow2.javaimport java.awt.Color;import java.awt.Dimension;import javax.swing.*;import java.awt.event.*;

import javax.swing.JPanel;

public class LeftWindow2 extends JPanel{ private JButton JButton1 = new JButton("Button 1"); private JButton JButton2 = new JButton("Button 2"); private JButton JButton3 = new JButton("Button 3");

public LeftWindow2() { setBackground(Color.RED); setPreferredSize(new Dimension(200, 70)); // add all GUI components to action listener button1.addActionListener(new ButtonListener()); button2.addActionListener(new ButtonListener()); button3.addActionListener(new ButtonListener());

// add Components to THIS JPanel add(button1); add(button2); add(button3); }

private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); // gets what was written on GUI Component

if(actionCommand.equals("Button 1")) { JOptionPane.showMessageDialog(null, "You pressed BUTTON 1"); return; } if(actionCommand.equals("Button 2")) { JOptionPane.showMessageDialog(null, "You pressed BUTTON 2"); return; } if(actionCommand.equals("Button 3")) { JOptionPane.showMessageDialog(null, "You pressed BUTTON 3"); return; } } }}

26

Page 27: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

27

Page 28: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

First Example with an ActionListener – (Source Match) uses the SAME “RightJFwindow.java”, and “GuiDriver.java” new LeftWindow3.java

o download “ActionListener2.zip”LeftWindow3.java

import java.awt.Color;import java.awt.Dimension;import javax.swing.*;import java.awt.event.*;

import javax.swing.JPanel;

public class LeftWindow3 extends JPanel{ private JButton button1 = new JButton("Button 1"); private JButton button2 = new JButton("Button 2"); private JButton button3 = new JButton("Button 3");

public LeftWindow3() { setBackground(Color.RED); setPreferredSize(new Dimension(200, 70)); // add all GUI components to action listener button1.addActionListener(new ButtonListener()); button2.addActionListener(new ButtonListener()); button3.addActionListener(new ButtonListener());

// add Components to THIS JPanel this.add(button1); this.add(button2); this.add(button3); }

private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == button1) { JOptionPane.showMessageDialog(null, "You pressed BUTTON 1"); return; } if(e.getSource() == button2) { JOptionPane.showMessageDialog(null, "You pressed BUTTON 2"); return; } if(e.getSource() == button3) { JOptionPane.showMessageDialog(null, "You pressed BUTTON 3"); return; } } }}

28

Page 29: GUI – Part Ifaculty.cse.tamu.edu/.../notes/Java/SWING_GUI_I_Notes.docx · Web viewGUI – Part I Overall Steps for Completing a GUI Application (if you get confused) Design a Layout

1. Create an ActionListener class and function for the Calc GUI. a. Remember where do we physically place the code?

2. Using the “source match” method, for each NUMERICAL button place the code:

JOptionPane.showMessageDialog(null, "You pressed "+ button1.getText() ); return;

3. Compile and Run

Java Component Reference Attached documentation Shows how to create and manipulate each

Problems that have occurred Java isn’t perfect with GUI Images would not change (transition of images only)

Images in transition fix

this.paintImmediately(this.getVisibleRect()); // this is the Extended JButton// place inside extended JButton class

29


Recommended