简体   繁体   English

Java Swing 文本字段和按钮的边框

[英]Java Swing Borders to textfields and buttons

I have a Swing UI which contains 6 text fields and labels for the input and 1 button and texfield to show the output.我有一个 Swing UI,其中包含 6 个文本字段和输入标签以及 1 个按钮和 tex 字段以显示 output。 now I want to make a border around these two.现在我想在这两个周围做一个边界。

I have read some materials regarding Titled borders but I think its only for single elements.我已经阅读了一些有关标题边框的材料,但我认为它仅适用于单个元素。 Please suggest.请建议。

You can add that last 2 components to a JPanel and then add that panel to main frame.您可以将最后 2 个组件添加到 JPanel,然后将该面板添加到主框架。 Now you can give border to JPanel and it will around 2 components inside it.现在您可以为 JPanel 提供边框,它将在其中包含 2 个组件。

To give border to jPanel you can use following:要为 jPanel 提供边框,您可以使用以下命令:

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参考: http://download.oracle.com/javase/tutorial/uiswing/components/border.html

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.您可以制作一个带有标题边框的 JPanel,然后使用您选择的内容管理器在 JPanel 中放置您想要的任何组件。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM