I have used Java for some time, but I have never created a GUI - always CLI. How does one create a GUI in Java? Can you suggest a good tutorial/reference?
I'm looking to create a simple GUI that has two long text areas and some buttons.
I have used Java for some time, but I have never created a GUI - always CLI. How does one create a GUI in Java? Can you suggest a good tutorial/reference?
I'm looking to create a simple GUI that has two long text areas and some buttons.
Here's a simple example
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Foo{
public static void main(String[] args) {
JFrame f = new JFrame("A JFrame");
f.setSize(250, 250);
f.setLocation(300,200);
final JTextArea textArea = new JTextArea(10, 40);
f.getContentPane().add(BorderLayout.CENTER, textArea);
final JButton button = new JButton("Click Me");
f.getContentPane().add(BorderLayout.SOUTH, button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.append("Button was clicked\n");
}
});
f.setVisible(true);
}
}
Well what you have to do is create your text/code thin (Note Pad++ or Notepad) and remember the name.
Then open it an type
import java.awt.*;
import javax.swing.*;
This basically tells java to get java.awt
and javax.swing
from its various code libraries that came with java when you downloaded it (Understandable as this is language made to help developers).
You then need to make your function that will have everything from the size, text inside it, color ect. REMEBER, we are not coding the gui here as that was already done when we imported import java.awt.*;
and javax.swing.*
;.
When I put public class work
,Work in the name of my file (if it was called code it would be public class code
.
public class work {
private static void createWindow() {
//Create and set up the window
JFrame frame = new JFrame ("simple GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel = new JLabel("Im cool" ,SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
//Display the window
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
Remember you have not called the function anywhere, its basically waiting there until it is called.
public static void main(String[] args) {
This tells the computer that what ever comes next, you do when i run the program.
So inside that you need to put
createWindow();
As this is what you called your function above and it is calling the function, you do not need to call this function as it does it when you run the program.
Creating a JFrame is not all as hard as people think, all you need is a definition class and a GUI Class. It's one of the simplest things in Java
Definition Class:
public class GetMyJavaWindow {
public static void main (String args[]) {
new JavaWindow();
}
}
JFrame class:
import javax.swing.JFrame;
public class JavaWindow { // Class name must match what it says in the def. class
public static final long serialVersionUID = 1L; // Needed for the JFrame to work.
public JavaWindow() { // Must match class name
this.setVisible(true); // Required
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // Required
this.setSize(800,600); // this.setSize (x,y);
}
}
If you need any more GUI help, come to me.
You have different possibilities here, but I'd recommend using Swing with an IDE such as Netbeans, which provides a very good WYSIWYG editor (called Matisse).
Netbeans also has project templates that you can use to quickly get started.
Finally, as others pointed out, make sure to do your homework and read some beginner Swing tutorials.
http://download.oracle.com/javase/tutorial/uiswing/ (Tutorials from Oracle) http://www.javabeginner.com/java-swing/java-swing-tutorial
Search for "Java Swing tutorials" :)