2013-10-04 51 views
0

我正在嘗試製作GUI,但無法正確獲取佈局設置無法讓Java佈局正常查看

+0

我會建議你設置每個組件的界限。它會給你的應用程序一個期望的外觀。 – Prasad

+0

我還沒有在我的課上學到界限。 –

+0

我不建議超出必要範圍使用界限 – RamonBoza

回答

1

首先,您需要爲主框架定義佈局。

試着在你的V @ genere構造

public Vigenere() { 
     setLayout(new GridLayout(2, 1)); 

     JPanel topPanel = new JPanel(new GridLayout(1, 2)); 
     JPanel p1 = new JPanel(new GridLayout(5, 9)); 
     p1.add(new JLabel("Source File")); 
     p1.add(jtfSourceFile); 
     p1.add(new JLabel("Results File")); 
     p1.add(jtfResultsFile); 
     p1.add(new JLabel("Key Code")); 
     p1.add(jtfKeyCode); 
     p1.add(new JLabel("Compare")); 
     p1.add(jtfCompare); 

     JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
     p2.add(jbtOpen); 
     p2.add(jbtSave); 
     p2.add(jbtKey); 
     p2.add(jbtCompare); 
     p2.add(jbtEncrypt); 
     p2.add(jbtDecrypt); 
     p2.add(jbtClear); 
     p2.add(jbtQuit); 

     topPanel.add(p1); 
     topPanel.add(p2); 


     JPanel p3 = new JPanel(new GridLayout(2, 1)); 
     jtfSource.setBorder(BorderFactory.createLineBorder(Color.black)); 
     jtfResults.setBorder(BorderFactory.createLineBorder(Color.black)); 
     p3.add(jtfSource); 
     p3.add(jtfResults); 

     add(topPanel); 
     //add(p2); 
     add(p3); 

     pack(); 

    } 

的頂部加入這一行,並從那裏繼續;)

+0

我剛剛更新了代碼。我沒有在框架中獲得所有面板,但他們沒有展示他們應該如何。 –

+0

你可以用gimp或油漆做一個線框來展示你想要的嗎? – RamonBoza

+0

http://i.imgur.com/tT300Xb.png這裏是我想去的佈局。 –

2

這可以通過使用由BorderLayout的化合物或嵌套的佈局來完成,具有FlowLayoutPAGE_START約束中,以及約束中的兩個文本區域的GridLayout

事情是這樣的:

ToolBarAnd2AreasLayout

OTOH,你可能會掉出FlowLayoutJToolBar(看起來更好),以及GridLayoutJSplitPane(更實用,因爲窗格可以設置爲無論用戶在那一刻需要什麼尺寸)。


給出的示例圖片(..paints一萬字)現在的問題,似乎很明顯由4行標籤,文本域,按鈕的頂部區域,按鈕會得到更好的GroupLayout完成。無論是那個還是另一個BorderLayoutGridLayout實例(標籤各一個,字段&按鈕),LINE_START,CENTERLINE_END

這裏是後者的一個例子:

enter image description here

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.TitledBorder; 

public class ToolBarAnd2AreasLayout { 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       // the GUI as seen by the user (without frame) 
       JPanel gui = new JPanel(new BorderLayout()); 
       gui.setBorder(new TitledBorder("BorderLayout()")); 

       JPanel controls = new JPanel(new BorderLayout(4,4)); 
       controls.setBorder(new TitledBorder("BorderLayout(4,4)")); 

       JPanel labels = new JPanel(new GridLayout(0,1)); 
       labels.setBorder(new TitledBorder("GridLayout(0,1)")); 
       controls.add(labels, BorderLayout.LINE_START); 
       JPanel fields = new JPanel(new GridLayout(0,1)); 
       fields.setBorder(new TitledBorder("GridLayout(0,1)")); 
       controls.add(fields, BorderLayout.CENTER); 
       JPanel buttons = new JPanel(new GridLayout(0,2,2,2)); 
       buttons.setBorder(new TitledBorder("GridLayout(0,2,2,2)")); 
       controls.add(buttons, BorderLayout.LINE_END); 

       for (int ii=0; ii<4; ii++) { 
        labels.add(new JLabel("Label " + (ii+1))); 
        fields.add(new JTextField(5)); 
        buttons.add(new JButton("Button " + ((ii*2) + 1))); 
        buttons.add(new JButton("Button " + ((ii*2) + 2))); 
       } 
       gui.add(controls, BorderLayout.PAGE_START); 

       JPanel input = new JPanel(new GridLayout(0,1,2,2)); 
       input.setBorder(new TitledBorder(
         "GridLayout(0,1,2,2)")); 
       for (int ii=0; ii<2; ii++) { 
        input.add(new JScrollPane(new JTextArea(5,35))); 
       } 
       gui.add(input, BorderLayout.CENTER); 

       JFrame f = new JFrame("Demo"); 
       f.add(gui); 
       // Ensures JVM closes after frame(s) closed and 
       // all non-daemon threads are finished 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       // See http://stackoverflow.com/a/7143398/418556 for demo. 
       f.setLocationByPlatform(true); 

       // ensures the frame is the minimum size it needs to be 
       // in order display the components within it 
       f.pack(); 
       // should be done last, to avoid flickering, moving, 
       // resizing artifacts. 
       f.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

我嘗試過這樣做,但它不適合我。我已經添加了我現在擁有的最新代碼。我基本上只需要JLabels和JTextField就可以更好地匹配你的。 –

+0

快速瀏覽一下你的代碼,看起來你'一個'GridLayout'太遠了'。我正要考慮修理它,但是懶惰。相反,我添加了用於第二個屏幕截圖的代碼作爲對答案的編輯。看看你如何去做。 –

+0

已更新。我沒有卡在按鈕上。我無法將f.code添加到主要方法中,因爲它無法識別f.add(gui)。我對此感到困惑。我試圖把它放入主體和一切。它工作的唯一方式就是現在的樣子。除非我錯過了一些東西。 –