2010-01-15 19 views
1

我想將單個標題邊框設置爲textfields組,我如何在java/swing中執行此操作。java中的邊框

我曾嘗試下面的代碼,但文本字段內板壓

// Create panel and add some components to it. 
JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT)); 

pnl.add(new JLabel("Name")); 
pnl.add(new JTextField()); 

// Add titled border to panel, which will therefore surround 
// all child components placed on the panel. 
pnl.setBorder(BorderFactory.createTitledBorder("It's Friday!")); 
+3

你能提供更多的上下文嗎?這是一個擺動應用程序? AWT? HTML? – Asaph 2010-01-15 07:10:13

+0

請解釋這個「文本字段在面板內壓縮」的說法!你看到什麼,你會看到什麼發生? – 2010-01-15 07:58:13

回答

4

下面是一個使用Swing的一個例子:

// Create panel and add some components to it. 
JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT)); 

pnl.add(new JLabel("Name")); 
pnl.add(new JTextField()); 

// Add titled border to panel, which will therefore surround 
// all child components placed on the panel. 
pnl.setBorder(BorderFactory.createTitledBorder("It's Friday!")); 
+0

我已經試過這個,但文本字段正在壓縮面板 – ganga 2010-01-15 07:48:34

+5

文本字段「壓縮」不是應用邊框的結果,而是因爲您沒有設置首選大小。對於'JTextField',您可以使用'setPreferredSize'來完成此操作,或者創建具有指定列數的文本字段。 – Adamski 2010-01-15 09:31:16

1

這是因爲文本字段沒有大小設置呢。 通過使用setColumns(int)方法來設置尺寸是最快的。你也可以使用setPreferredSize(Dimension)。