import java.awt.*;
import javax.swing.*;
public class A_simple_caculator {
private static void addComponentsToPane(Container pane) {
JComboBox<String> box = new JComboBox();
box.addItem("+");
box.addItem("-");
box.addItem("*");
box.addItem("/");
pane.setLayout(new FlowLayout()); // content pane默认是BorderLayout,现在设置为Flowlayout
pane.add(new JTextField(8));
pane.add(box);
pane.add(new JTextField(8));
pane.add(new JButton("="));
pane.add(new JTextField(8));
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("A_simple_caculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
public static void main(String args[]) {
// Create the frame on the main thread
// which is not event dispatching thread.
createAndShowGUI();
}
//main函数这样写也可以成功运行,但是基于线程安全考虑,不能这样写。
参考资料 : java swing编程介绍