-
Notifications
You must be signed in to change notification settings - Fork 0
Advanced GUI
As you can see from the GUI section, GUI in Java can offer a lot of benefits. Using more advanced GUI concepts you can do things like add menus, change button layout and change color of content.
In Java when you use GUI elements you are using what is called the Swing component. This is why when you use GUI in a class you must import the javax.swing.* class. Swing components are usually put into a container. A container is a component that holds other components. A container is often in the form of a window that you can resize, and move. There is also a thing called a frame. A frame is a window that has a title bar and border. A frame is also the most common component you will use.
A JFrame is how most components in Java are displayed. The JFrame class has four constructors that you can set when creating the JFrame.
Constructor | Use |
---|---|
JFrame() | constructs a new frame |
JFrame(String title) | creates new frame with specified title |
JFrame(GraphicsConfiguration gc | creates JFrame in the specified configuration |
JFrame(String title, GraphicsConfiguration gc | creates JFrame in the specified configuration and title |
You can create JFrame just like you do other objects in Java.
JFrame newFrame = new JFrame("This is a title");
Now that you have the JFrame object you can use different methods to set the title of the frame, the size of the frame and many more attributes.
JFrame newFrame = new JFrame("This is a title");
newFrame.setSize(200 , 100); //sets he size of the frame to 200 pixels x 100 pixels
newFrame.setVisible(true); //sets the frame to be visible
Output
Put screenshot here
A JLabel is an uneditable component that is used to provide information to the user. Just like with the JFrame the JLabel component has a lot of constructors available.
Constructor | Use |
---|---|
JLabel() | creates label with no image and empty string for title |
JLabel(Icon Image) | create label with image |
JLabel(Icon Image, int horizontalAlignment) | creates label with image and specified horizontal alignment |
JLabel(String text) | create label with specified text |
JLabel(String text, Icon Image, int horizontalAlignment) | creates label with text and alignment and image |
JLabel(String text, int horizontalAlignment | creates label with text and alignment |
You can create a label to display text and then combine it with the JFrame to display the label.
JLabel greeting = new JLabel("Hello there");
JFrame newFram = new JFrame("Test Frame");
newFrame.setSize(200 , 100); //sets he size of the frame to 200 pixels x 100 pixels
newFrame.setVisible(true); //sets the frame to be visible
newFrame.add(greeting);
Output
add screenshot here
With JLabel you can also change the font with the setFont() method.
Font newFont = new Font("Arial", Font.BOLD, 36);
JLabel greeting = new JLabel("Hello there");
greeting.setFont(newFont);
Output
Add screenshot here
The layout manager manager in Java is an object that controls where the components are positioned. By default, the border layout manager is used which may result in multiple components to obscure each other. A better layout to use is the flow layout manager which places the components in rows instead. The layout can be set in three different positions which are LEFT, CENTER, and RIGHT. If you do not specify the layout then by default it is centered.
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
JLabel greeting = new JLabel("Hello there");
Jlabel nextGreeting - new JLabel("Bubba");
JFrame newFram = new JFrame("Test Frame");
newFrame.setSize(200 , 100); //sets he size of the frame to 200 pixels x 100 pixels
newFrame.setVisible(true); //sets the frame to be visible
newFrame.setLayout(flow);
newFrame.add(greeting);
newFrame.add(nextGreeting);
Output
insert screenshot here
In Java, you can also add components to the JFrame to en
In a JFrame, it may be necessary to accept userInput. To do this you will need to add a text field to the JFrame to accept this input. Let's say you wanted to set a field with a length that was long enough for the user to enter 12 characters.
FlowLayout flow = new FlowLayout();
JLabel greeting = new JLabel("Hello there");
JLabel nextGreeting = new JLabel("Bubba");
//creates the text field of size 12
JTextField userResponse = new JTextField(12);
JFrame newFrame = new JFrame("Test Frame");
newFrame.setSize(255 , 100); //sets he size of the frame to 200 pixels x 100 pixels
newFrame.setVisible(true); //sets the frame to be visible
newFrame.setLayout(flow);
newFrame.add(greeting);
newFrame.add(nextGreeting);
//adds the text field to the frame
newFrame.add(userResponse);
Output
Place screen shot here
With the JTextField you can also use it to get what the user entered using the getTesxtMethod.
String userInput = userResponse.getText();
Now that you have text fields you will also need a button in order to confirm the input. To do this you would use the JButtons component.
JButton ConfirmButton = new JButton("Confirm?");
newFrame.add(confirmButton);
Output
place button here
JButtons have a total of five constructors.
Constructor | Use |
---|---|
JButton() | creates a button with no text |
JButton(Icon icon) | creates button with an Icon |
JBUtton(String text) | creates button with specified test |
JButton(String text, Icon icon) | creates button with text and icon |
JButton(Action a) | creates a button whose action is supplied from action class |
A event occurs when a user takes action. This can be anything from clicking a button to exiting the program. If you have a GUI application that asks the user to enter their name and then click the enter button. The action of the user clicking the button is an event. Based on what your application is set up to do you will process the clicking of the button in a certain way. This is called event handling. In order to handle events, you need and object that looks for the event. This is known as a listener. In order for this listener to properly work, you must register, or sign up the object to be a listener. Before you can create an event handler you must prepare your class to expect and respond to events.
In order to properly prepare your class, you must import the proper packages that accept those events.
import java.awt.event;
In order to properly register a listener, you must use the addActionListener() method. Let's say you have a button you want to register.
JButton someButton = new JButton("Click Here");
someButton.addActionListener(this);
*Note: the this keyword means that the method is referring to "this current object"
Now that you are telling the class that it will receive an event you will need to make and event handler method. The method you will use for this will be the actionPerformed() method. This method will contain the code that you want to execute when the event is received. In order to properly do this you will need another class that will extend from JFrame to handle this.
Main Class
public static void main(String args[]){
JNameFrame frame;
frame = new JNameFrame();
frame.setVisible(true);
}
JNameFrame Class
public class JNameFrame extends JFrame implements ActionListener
{
JLabel question = new JLabel("What is your name?");
JTextField answer = new JTextField(15);
JButton clickMe = new JButton("Enter");
JLabel greet = new JLabel("");
public JNameFrame()
{
super("Hello");
setSize(300, 200);
setLayout(new FlowLayout());
add(question);
add(answer);
add(clickMe);
add(greet);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clickMe.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
String name = answer.getText();
String greeting = "Hello " + name;
greet.setText(greeting);
}
}
Output
add screenshots
As you can see using a GUI is very powerful. There are a lot more things you can do with a GUI from math, to showing multiple options and even having a graphic and menus.