2014-10-03 97 views
0

我在嘗試使用Swing創建簡單UI時遇到了一些問題。這是我想要的窗口:Desired result無法爲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); 

我得到以下結果: Result

我不知道如何將兩個按鈕放置在窗口中央。而且我也不知道如何爲JTextArea設置一個固定大小。而已。 在此先感謝!

回答

4

考慮:

  • 使用的BoxLayout的整體GUI
  • 創建使用GridBagLayout的持有兩個按鈕中間的JPanel。
  • 然後添加到外部BoxLayout - 使用JPanel一個JScrollPane,中間JPanel和另一個JScrollPane。

所有這些的關鍵是嘗試想象佈局管理器應該做什麼,然後嵌套您的JPanel,每個JPanel都有自己的佈局管理器,以實現您的目標。

例如,該GUI

enter image description here

可以通過該代碼來製備:

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.*; 

public class LayoutEg extends JPanel { 
    private static final long serialVersionUID = 1L; 
    private static final int ROWS = 30; 
    private static final int COLS = 30; 
    private static final int GBC_GAP = 10; 
    private JTextArea textArea1 = new JTextArea(ROWS, COLS); 
    private JTextArea textArea2 = new JTextArea(ROWS, COLS); 
    private JButton encryptButton = new JButton("Encrypt"); 
    private JButton decryptButton = new JButton("Decrypt"); 

    public LayoutEg() { 
     JPanel centerPanel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.CENTER; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridheight = 1; 
     gbc.gridwidth = 1; 
     gbc.insets = new Insets(GBC_GAP, GBC_GAP, GBC_GAP, GBC_GAP); 

     // setting the weightx and weighty of 0 is what forces the buttons 
     // in the center to bunch together 
     gbc.weightx = 0; 
     gbc.weighty = 0; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     centerPanel.add(encryptButton, gbc); 

     gbc.gridy = 1; 
     centerPanel.add(decryptButton, gbc); 

     setBorder(BorderFactory.createEmptyBorder(GBC_GAP, GBC_GAP, GBC_GAP, GBC_GAP)); 
     setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); 
     add(new JScrollPane(textArea1)); 
     add(centerPanel); 
     add(new JScrollPane(textArea2)); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Layout Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new LayoutEg()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

Thx很多人。很棒! – Zan 2014-10-03 21:15:47

2

您可能想要考慮使用GridBagLayout來實現此設計,因爲它具有更好的控件來鎖定您希望按鈕顯示的位置。你實際上可以在GridBagConstraints中做你的單元格填充,或者你可以考慮使用Border(s)

+0

THX。我將不得不通知我自己這些話題。我習慣使用用戶友好的工具佈置UI。沒有太多的自己編碼經驗。 – Zan 2014-10-03 21:16:55