java中的边框

时间:2010-01-15 07:07:35

标签: java swing

我想将单个标题边框设置为一组文本字段,如何在java / 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!"));

2 个答案:

答案 0 :(得分: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!"));

答案 1 :(得分:1)

这是因为文本字段尚未设置大小。 使用setColumns(int)方法设置大小是最快的。您还可以使用setPreferredSize(Dimension)。