我在嘗試使用Swing創建簡單UI時遇到了一些問題。這是我想要的窗口:無法爲Swing元素設置固定位置
我想在兩邊有兩個可滾動的JTextAreas,並且在窗口中間還有兩個按鈕。
這裏是我的代碼有:
public class MainWindow extends JFrame implements ActionListener {
private JTextArea inputArea;
private JTextArea outputArea;
private JButton encriptButton, decriptButton;
public MainWindow() {
super();
}
public void initUI() {
this.setSize(900, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputArea = new JTextArea();
JScrollPane js = new JScrollPane(inputArea);
js.setPreferredSize(new Dimension(350,400));
outputArea = new JTextArea();
JScrollPane js2 = new JScrollPane(outputArea);
js2.setPreferredSize(new Dimension(350, 400));
JPanel panel = new JPanel();
encriptButton = new JButton("Encript");
decriptButton = new JButton("Decript");
panel.add(encriptButton);
panel.add(decriptButton);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(js, BorderLayout.WEST);
this.getContentPane().add(js2, BorderLayout.EAST);
this.getContentPane().add(panel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
現在,如果我做這樣的事情:
MainWindow window = new MainWindow();
window.initUI();
window.setVisible(true);
我得到以下結果:
我不知道如何將兩個按鈕放置在窗口中央。而且我也不知道如何爲JTextArea設置一個固定大小。而已。 在此先感謝!
Thx很多人。很棒! – Zan 2014-10-03 21:15:47