gui in java code example
Example 1: java gui
import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button = new JButton("Click me");
frame.getContentPane().add(button);
frame.setVisible(true);
}
}
Example 2: java how to make a gui
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);
}
}
Example 3: how to build a gui in java
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI implements ActionListener{
int count = 0;
private JLabel label;
private JPanel panel;
private JFrame frame;
public GUI(){
frame = new JFrame();
JButton button = new JButton("Howdy");
button.addActionListener(this);
label = new JLabel("Number of clicks: 0");
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout(0,1));
panel.add(button);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Our GOOEY");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number of clicks: " + count);
}
}
Example 4: java gui
import javax.swing.*;
public class Main {
public static void main(String []args) {
JFrame framing = new JFrame();
framing.setPSize
framing.setExtendedState()
Example 5: java gui
import javax.swing.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button = new JButton("Click me");
frame.getContentPane().add(button);
frame.setVisible(true);
}
}