Java Visual Hello World Popup
Posted Nov 15th, 2009 by Conor in in College,Java,LanguagesI spent yesterday going a bit deeper into Java with one of my lecturers. I decided to try and write a more complicated hello world app. It’s quite simple really once you get around the strict OOP and different syntax… This is how I did it:
First you need to import some of the java libraries that we will be using:
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*;
Then add the class, Popup. Make sure to name the file Popup.java
public class Popup{
private JFrame frame=new JFrame("Hello!");
private JPanel panel=new JPanel();
private JButton button=new JButton("Say Hello");
private JLabel label=new JLabel("Click the button to say hello...",SwingConstants.CENTER);
private ButtonListener buttonListener=new ButtonListener();
What that actually means is create a new instance of JFrame, JPanel, JButton and JLabel and store them in the frame,panel, button and label variables. As for the ButtonListener, we will deal with that later. Now we will deal with the launchFrame function which does what it says on the tin – launches the frame! Basically it just connects all of the pieces of information stored into the variables above and displays them together.
public void launchFrame(){
int height=400, width=600;
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Above we create two new integers which store the width and height of our frame. The next line sets the variables to the frame size. The final line tells the Java application to close when the window, or JFrame, is closed. Now that we have the configuration out of the way we can move on to adding the variables to the frame.
frame.getContentPane().add(label,BorderLayout.CENTER); panel.add(button); frame.getContentPane().add(panel,BorderLayout.WEST); button.addActionListener(buttonListener); frame.setVisible(true); }
The code above adds the label to the frame, then adds the button to the panel and then the panel to the frame. Once again disregard the button.addAction.. line for now. The final line simply sets the frame to visible so we can see it.
Now that we have the frame settled, we need to add our essential main function to start up the execution:
public static void main(String args[]){
Popup popup=new Popup();
popup.launchFrame();
}
}
This code basically creates an instance of our class, Popup and then executes the function launchFrame which we have created in that class.
That’s the guts of it, but now we need to talk about these lines:
private ButtonListener buttonListener=new ButtonListener();
public void launchFrame(){
....
button.addActionListener(buttonListener);
Basically we have previously created a button, but these lines define what happens when the button is pressed. The second line of code tells the machine to listen out for the clicking of the button, and when that happens to execute the variable buttonListener. We have created the variable buttonListener, which creates a new instance of the ButtonListener class which we have yet to create.
That is the next step, to create that class. Create a new file called ButtonListener.java and add this code:
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*;
public class ButtonListener implements ActionListener{
The first four lines import the libraries that we are using. The final line is important. It states that our class, ButtonListener is implementing the ActionListener class. Below is the content of our new class:
private JFrame pressed=new JFrame("Hello World!");
public void actionPerformed(ActionEvent actionEvent){
First, we create a new JFrame for our popup just like we did in the other file. The function actionPerformed is a function from the ActionListener class that we are implementing. The parameters for the function are predefined so we need them. The rest of the code is very similar to the previous code so I won’t explain it in depth. Basically it just configures the new frame, adds the label and then displays it:
int height=160, width=200;
pressed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel pressedtext=new JLabel("Hello World!",SwingConstants.CENTER);
pressed.getContentPane().add(pressedtext,BorderLayout.CENTER);
pressed.setSize(width,height);
pressed.setVisible(true);
}
Save that file and then compile both files by executing:
javac ButtonListener.java Popup.java
Now if you run the program you should see a nice visual dialogue:
java Popup
You can download a jar archive of the program here.
The full code is below. Popup.java:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Popup{
private JFrame frame=new JFrame("Hello!");
private JPanel panel=new JPanel();
private JButton button=new JButton("Say Hello");
private JLabel label=new JLabel("Click the button to say hello...",SwingConstants.CENTER);
private ButtonListener buttonListener=new ButtonListener();
public void launchFrame(){
int height=400, width=600;
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label,BorderLayout.CENTER);
panel.add(button);
frame.getContentPane().add(panel,BorderLayout.WEST);
button.addActionListener(buttonListener);
frame.setVisible(true);
}
public static void main(String args[]){
Popup popup=new Popup();
popup.launchFrame();
}
}
ButtonListener.java:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonListener implements ActionListener{
private JFrame pressed=new JFrame("Hello World!");
public void actionPerformed(ActionEvent actionEvent){
int height=160, width=200;
pressed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel pressedtext=new JLabel("Hello World!",SwingConstants.CENTER);
pressed.getContentPane().add(pressedtext,BorderLayout.CENTER);
pressed.setSize(width,height);
pressed.setVisible(true);
}
}