Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.

Similar presentations


Presentation on theme: "GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be."— Presentation transcript:

1 GUI Components

2 The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be added to a window. There are three that we are focusing on today: There are three that we are focusing on today: Label  We worked with this yesterday. Label  We worked with this yesterday. Text Field Text Field Button Button

3 Labels: Labels: The JLabel class is used to create a label. The JLabel class is used to create a label. To create a label: To create a label: Define the label in the class fields. Define the label in the class fields. Ex: Ex: private JLabel lblMessage2; Create the label object and send it’s constructor the text of the label. Create the label object and send it’s constructor the text of the label. Ex: Ex: lblMessage2 = new JLabel("Moby's music is majestic!"); Add the label to the panel. Add the label to the panel. Ex: Ex:panel.add(lblMessage2);

4 Textboxes: Textboxes: The JTextField class is used to create a textbox. The JTextField class is used to create a textbox. To create a textbox: To create a textbox: Define the textbox in the class fields. Define the textbox in the class fields. Ex: Ex: private JTextField txtBox2; private JTextField txtBox2; Create the textbox object and send it’s constructor the width of the textbox (in characters). Create the textbox object and send it’s constructor the width of the textbox (in characters). Ex: Ex: txtBox2 = new JTextField(30); Add the textbox to the panel. Add the textbox to the panel. Ex: Ex:panel.add(txtBox2);

5 Buttons: Buttons: The JButton class is used to create a button. The JButton class is used to create a button. To create a button: To create a button: Define the button in the class fields. Define the button in the class fields. Ex: Ex: private JButton btnMeaning2; private JButton btnMeaning2; Create the button object and send it’s constructor the text for the button Create the button object and send it’s constructor the text for the button Ex: Ex: btnMeaning2 = new JButton("Brian Eno is a genius."); Add the button to the panel. Add the button to the panel. Ex: Ex:panel.add(btnMeaning2);

6 To alter the color of a JFrame Component: To alter the color of a JFrame Component: Many of the Swing component classes have the following methods: Many of the Swing component classes have the following methods: setBackground( )  This sets the background color. setBackground( )  This sets the background color. setForeground( )  This sets the text color. setForeground( )  This sets the text color. To use these methods, you have to import the AWT package: To use these methods, you have to import the AWT package: import java.awt.* import java.awt.* For example, to change the text color of a label: For example, to change the text color of a label: lblMessage.setForeground(Color.RED); lblMessage.setForeground(Color.RED);

7 Here are a few of the color constants you may use: Here are a few of the color constants you may use: Color.BLACK Color.BLACK Color.CYAN Color.CYAN Color.GRAY Color.GRAY Color.LIGHT_GREY Color.LIGHT_GREY Color.ORANGE Color.ORANGE Color.RED Color.RED Color.YELLOW Color.YELLOW Color.BLUE Color.BLUE Color.DARK_GREY Color.DARK_GREY Color.GREEN Color.GREEN Color.MAGENTA Color.MAGENTA Color.PINK Color.PINK Color.WHITE Color.WHITE

8 The Color Class: The Color Class: What if I need a different color? Easy! You can create a color object. What if I need a different color? Easy! You can create a color object. Just create a color object and pass its constructor the color value you want: Just create a color object and pass its constructor the color value you want: Ex: Ex: Color c4 = new Color(230, 204,255); Color c4 = new Color(230, 204,255); Then you pass the color object to the setBackground( ) or setForeground( ) method. Then you pass the color object to the setBackground( ) or setForeground( ) method. Ex: Ex: btnMeaning.setBackground(c4); btnMeaning.setBackground(c4);

9 RGB Settings: RGB Settings: The Color class uses the RGB (Red Green Blue) standard for setting its color. The Color class uses the RGB (Red Green Blue) standard for setting its color. Each letter is given a value from 1 to 255. For example, the following setttings create the color “yellow.” Each letter is given a value from 1 to 255. For example, the following setttings create the color “yellow.” R – 255 R – 255 G – 255 G – 255 B – 0 B – 0 You can use the following website to find different colors, and the RGB code that goes along with those colors:\ You can use the following website to find different colors, and the RGB code that goes along with those colors:\ http://www.colorschemer.com/online.html http://www.colorschemer.com/online.html http://www.colorschemer.com/online.html

10 Just copy the RGB value and replace the periods with commas. Just copy the RGB value and replace the periods with commas.

11 Using a main( ) method with GUI classes: Using a main( ) method with GUI classes: We have often used a Main class with a main( ) method to create instances of other classes. We have often used a Main class with a main( ) method to create instances of other classes. We can simply our programs by equipping the GUI class with a main method. We can simply our programs by equipping the GUI class with a main method. Just have the main method call the No-Arg constructor: Just have the main method call the No-Arg constructor: public static void main(String[] args) { Nihilism window = new Nihilism( ); Nihilism window = new Nihilism( ); }


Download ppt "GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be."

Similar presentations


Ads by Google