1

即使在調整窗口大小(如果可能)的情況下,我想保留一個使用絕對佈局創建的面板。我遇到了幾個建議here和[這裏] [2]但沒有骰子!以下是我的示例代碼,任何想法或建議?我沒有像JLable那樣集中單個組件的問題,但是我想要將一個面板與許多組件集中在一起!調整窗口大小時的中心面板

import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.BorderFactory; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 
import javax.swing.border.Border; 
import javax.swing.JLabel; 


public class TestPanel extends JFrame { 

    private JLabel lblSetupTitle; 
    private Border compoundBorder, outlineColorBorder, outlineBorder; 
    private JTextArea txtrManageData; 
    private JPanel childPanel; 

    public TestPanel() 
    { 
     setBackground(Color.white); 
     outlineColorBorder = BorderFactory.createLineBorder(Color.gray); 
     outlineBorder = BorderFactory.createEmptyBorder(20, 20, 20, 20); 
     compoundBorder = BorderFactory.createCompoundBorder(outlineColorBorder, outlineBorder); 

     lblSetupTitle = new JLabel("Setup"); 
     lblSetupTitle.setBounds(443, 288, 44, 23); 
     txtrManageData = new JTextArea("Text Area Text"); 
     txtrManageData.setBounds(393, 322, 142, 61); 

     childPanel = new JPanel(); 
     childPanel.setLocation(89, 38); 
     childPanel.setSize(921, 452); 
     childPanel.setBorder(compoundBorder); 

     setupGUIElements(); 
     setupPanel(); 
    } 

    private void setupGUIElements() 
    { 
     txtrManageData.setBackground(null); 
     txtrManageData.setLineWrap(true); 
     txtrManageData.setWrapStyleWord(true); 
    } 

    private void setupPanel() 
    { 
     getContentPane().setLayout(new GridBagLayout()); // set layout of parent panel to GridBagLayout 
     childPanel.setLayout(null); // set layout of child panel to AbsoluteLayout 
     childPanel.add(lblSetupTitle); 
     childPanel.add(txtrManageData); 

     getContentPane().add(childPanel, new GridBagConstraints()); 

     this.setSize(1020, 500); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       TestPanel ex = new TestPanel(); 
       ex.setVisible(true); 
      } 
     }); 
    } 
} 

編輯:在這樣

example gui i would like to create

+2

不要使用空'LayoutManager'。 – alex2410 2014-11-06 09:22:48

+4

使用絕對佈局我們你的爆發錯誤,BorderLayout或GrudBagLayout將免費做到這一點。這正是我們擁有佈局管理器的許多原因之一,來處理這些冗長乏味的重複性任務...... – MadProgrammer 2014-11-06 09:22:50

+0

哦..它會幫助佈局,如果'txtrManageData = new JTextArea(「Text Area Text 「);'而不是'txtrManageData = new JTextArea(」Text Area Text「,3,20);' – 2014-11-06 09:29:55

回答

2

我會嵌套佈局。

enter image description here

enter image description here

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

public class ThreeButtonTextFieldCombo { 

    private JPanel ui = null; 

    ThreeButtonTextFieldCombo() { 
     initUI(); 
    } 

    public final void initUI() { 
     if (ui!=null) return; 
     ui = new JPanel(new GridBagLayout()); 
     ui.setBorder(new TitledBorder("Parent Panel")); 

     JPanel controls = new JPanel(new GridLayout(1,0,10,10)); 
     ui.add(controls); 
     controls.setBackground(Color.RED); 
     controls.setBorder(new TitledBorder("Child Panel")); 

     for (int ii=1; ii<4; ii++) { 
      addLabelAndField(controls, "String " + ii); 
     } 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    private void addLabelAndField(JPanel panel, String text) { 
     JPanel controls = new JPanel(new BorderLayout(3, 3)); 
     controls.setBorder(new EmptyBorder(20,20,20,20)); 
     JLabel l = new JLabel(text); 
     controls.add(l, BorderLayout.PAGE_START); 

     JTextArea ta = new JTextArea(text, 2, 8); 
     controls.add(new JScrollPane(ta)); 

     panel.add(controls); 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       JFrame f = new JFrame("Three Button/Text Field Combo"); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 
       ThreeButtonTextFieldCombo tbtfc = 
         new ThreeButtonTextFieldCombo(); 
       f.setContentPane(tbtfc.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 
       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

確定..按鈕/標籤,標籤/按鈕有點混淆了,但交換標籤的按鈕,它將工作原理相同。 – 2014-11-06 09:56:19

0

與您的代碼的第一個問題創造一些任何提示,鏈接,指導您使用的GridBagConstraints的空實例添加你的孩子面板。我從來沒有見過它像以前那樣使用。

getContentPane().add(childPanel, new GridBagConstraints()); 

不要任何佈局設置內容窗格,只是添加它是這樣的:

getContentPane().add(childPanel); 

現在,如果你運行它,你會得到兩個組件在中間,在那裏你使用它們定義setBounds(..)方法。

與幾乎所有人對您的問題發表評論一樣,您不應該使用null佈局,而應使用其他佈局。我將使用GridBagLayout來組織圖中的三個按鈕和三個文本框。然後,您可以在您的子面板上設置邊界(..)。

如果你真的必須使用絕對佈局,那麼你將不得不做一些數學。

如果你的第一個標籤是這樣的:

labell1.setBounds(443, 288, 44, 23); 

那麼你的第二個標籤應該是這樣的:

labell2.setBounds(443 + someXDisplacement, 288, 44, 23); 

..和第三:

labell3.setBounds(443 + (someXDisplacement x 2), 288, 44, 23); 

你得到圖片。

+2

*「如果你真的必須使用絕對佈局..」* *當地獄凍結時,我真的需要絕對佈局。* – 2014-11-06 09:54:01

+0

很高興得到downvoted回答正是OP想要的。明確建議他們不要使用AbsoluteLayout,但如果他們確實需要使用它,則給他們一個建議。有時候,這是一個笑話。 – 2014-11-06 10:25:29

+0

*「很高興得到downvoted ..」*不要看着我的倒票。但是.. *「用於回答OP所要的內容。」* ..我覺得在這種情況下是一個錯誤。 – 2014-11-06 10:45:57