2013-07-14 62 views
-2

我試圖讓JPane填滿給定的空間,但它不會。 這個代碼應該是explanitary ...使用GridLayout發生意外的版面問題

JPanel memWindow = new JPanel(); 
    memWindow.setLayout(new GridLayout(1, 4)); 
    memWindow.add(memField = new JTextField(10)); 
    memWindow.add(memView = new JButton("VM")); 
    memWindow.add(memAdd = new JButton("M+")); 
    memWindow.add(memSub = new JButton("M-")); 

    JPanel memWindow2 = new JPanel(); 
    memWindow.setLayout(new GridLayout(1, 4)); 
    memWindow.add(mem2Field = new JTextField(10)); 
    memWindow.add(mem2View = new JButton("VM")); 
    memWindow.add(mem2Add = new JButton("M+")); 
    memWindow.add(mem2Sub = new JButton("M-")); 

    JPanel memWindows = new JPanel(); 
    memWindows.setLayout(new GridLayout(2, 1)); 
    memWindows.add(memWindow, BorderLayout.SOUTH); 
    memWindows.add(memWindow2, BorderLayout.NORTH); 

    add(displayWindow, BorderLayout.NORTH); 
    add(memWindows); 
    add(inputWindow, BorderLayout.SOUTH); 

這是GUI我得到...

enter image description here

+0

我想如果我看到整個代碼,我可能會有更好的機會幫助你 – user2277872

+4

你沒有接受過你之前提出的任何答案,所以我想我會跳過回答這個問題。 – camickr

+1

'memWindows.add(memWindow,BorderLayout.SOUTH);''''''和'memWindows.add(memWindow2,BorderLayout.NORTH);'約束無意義('BorderLayout.XXX') – MadProgrammer

回答

2

你的代碼有什麼問題?

1.您可以先解釋下面的代碼嗎?

JPanel memWindow2 = new JPanel(); 
memWindow.setLayout(new GridLayout(1, 4)); 
memWindow.add(mem2Field = new JTextField(10)); 
memWindow.add(mem2View = new JButton("VM")); 
memWindow.add(mem2Add = new JButton("M+")); 
memWindow.add(mem2Sub = new JButton("M-")); 

我想應該是這樣的:

JPanel memWindow2 = new JPanel(); 
memWindow2.setLayout(new GridLayout(1, 4)); 
memWindow2.add(mem2Field = new JTextField(10)); 
memWindow2.add(mem2View = new JButton("VM")); 
memWindow2.add(mem2Add = new JButton("M+")); 
memWindow2.add(mem2Sub = new JButton("M-")); 

2.如果你想使用BorderLayout.XXX以指定部件的位置,你應該使用的BorderLayout如下:

JPanel memWindows = new JPanel(); 
memWindows.setLayout(new BorderLayout()); 
memWindows.add(memWindow, BorderLayout.SOUTH); 
memWindows.add(memWindow2, BorderLayout.NORTH);