开发者

Java Swing Borders to textfields and buttons

开发者 https://www.devze.com 2023-03-03 06:16 出处:网络
I have a Swing UI which contains 6 text fields and labels for the input and 1 button and texfield to show the output. now I want to make a border around these two.

I have a Swing UI which contains 6 text fields and labels for the input and 1 button and texfield to show the output. now I want to make a border around these two.

I have read some mate开发者_如何学Pythonrials regarding Titled borders but I think its only for single elements. Please suggest.


You could make a JPanel with a titled border, then put however many components you wanted in the JPanel using the content manager of your choice.

An example:

JPanel myPanel = new JPanel();
myPanel.setBorder(new TitledBorder(null, "My Title", TitledBorder.LEADING, TitledBorder.TOP, null, null));

myPanel.setLayout(new GridLayout(1, 0, 0, 0));

JButton button = new JButton("New button");
myPanel.add(button);

JLabel label = new JLabel("New label");
myPanel.add(label);


You can add that last 2 components to a JPanel and then add that panel to main frame. Now you can give border to JPanel and it will around 2 components inside it.

To give border to jPanel you can use following:

JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createLineBorder(Color.black));

If you want titled border then you can use following:

pane.setBorder(BorderFactory.createTitledBorder(BorderFactory
            .createMatteBorder(5, 5, 5, 5, Color.blue), "Title",
            TitledBorder.LEFT, TitledBorder.TOP));

Reference: http://download.oracle.com/javase/tutorial/uiswing/components/border.html

0

精彩评论

暂无评论...
验证码 换一张
取 消