Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple JFrame example in Java
package fypsol.guiexamples.ex01;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MainClass {
public static void main(String[] args) {
JButton myButton = new JButton("Hello World");
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(myButton, "Alert", "Hello World Clicked", JOptionPane.INFORMATION_MESSAGE);
}
});
JFrame mJFrame = new JFrame("My First Jframe");
mJFrame.setSize(250, 150);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mJFrame.setLocationRelativeTo(null);
// mJFrame.setLocation(400, 200);
mJFrame.setResizable(false);
mJFrame.add(myButton);
mJFrame.setLayout(new FlowLayout());
mJFrame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment