How can I make a basic Java window with a text field and a button using JFrame?

Viewed 27

I tried to find some code from the internet and copy-pasted it (i also tried making my own code from the java documentation, but it gave some errors i didnt understand, i have jdk installed), but when i typed "javac program.java" it did not output a file.

1 Answers
import javax.swing.*;

public class SimpleUI {
  public static void main(String ... args) {
    var frame = new JFrame("Title goes here");
    var text = new JTextField(20);
    var button = new JButton("Click me");
    var panel = new JPanel();
    panel.add(text);
    panel.add(button);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}

Creates:

More info: https://docs.oracle.com/javase/tutorial/uiswing/start/index.html

Related