如何在Java中创建GUI

Ami*_*hum 8 java user-interface

我已经使用Java一段时间了,但我从未创建过GUI - 总是CLI.如何在Java中创建GUI?你能建议一个好的教程/参考吗?

我正在寻找一个简单的GUI,它有两个长文本区域和一些按钮.

Bal*_*a R 9

这是一个简单的例子

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);

  }

}
Run Code Online (Sandbox Code Playgroud)


Cro*_*zin 6

在Oracle Tutorial的页面上阅读Swing.

  • 没有.2017年没有理由使用Swing而不是Javafx/others (3认同)