如何用边框包围Java Swing组件? [英] How to surround Java Swing components with border?

查看:75
本文介绍了如何用边框包围Java Swing组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个由几个选项卡式面板组成的应用程序.在每个组件上,我都希望将各组组件之间用边框分开.看起来像:

I'm building an application which will consist of few tabbed panels. On each of them, I'd like to put sets of components separated from each other by borders. It would look like:

|- Titled Border 1 ---

[JTextField] [JComboBox] [JTextField] [JComboBox]

|--------

|- Titled Border 2 ---

[JTextField] [JComboBox] [JTextField] [JComboBox]

|--------

... and so forth.

当我尝试在面板上简单地添加新的标题为标题2"的边框时,它被添加并覆盖了第一个保留顶部的组件.在某些示例中,我看到在一帧内定义了许多JPanel,并且每个面板都有自己的边框.在我的情况下可能可行,但是如何添加这些面板以使其显示在一个选项卡中?

When I tried to simply add new border, Titled Border 2, to the panel, it was added and covered the first one leaving components on top though. In some examples I saw many JPanels defined within one frame and each panel had its own border. It might work in my case but how to add these panels to be displayed within one tab?

Oracle的其中一本教程正好显示了一个带选项卡的窗格,其中包含多种边框的演示.当我尝试对其进行编辑并在其中放置组件时,它出现在两个边框之间,而不是被包围.对于我来说,这是另一种选择.

One of Oracle's tutorials shows exactly a tabbed pane with demo of many kinds of borders. When I tried to edit it and put a component there, it appeared between two borders instead of being surrounded. And it was another option that was unsuccessful for me.

第二件事是,我不使用任何布局管理器,组件位置是固定的,说实话,我会保留此设置.或者也许您建议在这种情况下使用任何布局管理器?

Second thing is, I don't use any layout manager, components positions are fixed and honestly I would keep this setting. Or maybe you recommend using any layout manager in this specific case?

您对如何解决此问题有任何提示吗?

Would you have any hints on how to solve this problem?

似乎还不允许附加屏幕截图,但这是代码的一部分,用于显示边框:

it seems I'm not allowed yet to attach a screenshot, but here's the part of code taking care of displaying the borders:

    lenMicro = new JPanel();
    lenMicro.setLayout(null);

    bGreyLine = BorderFactory.createLineBorder(Color.GRAY, 1, true);
    bTitled1 = BorderFactory.createTitledBorder(bGreyLine, "Length (1/2)", TitledBorder.LEFT, TitledBorder.TOP);
    lenMicro.setBorder(bTitled1);
    bTitled2 = BorderFactory.createTitledBorder(bGreyLine, "Length (2/2)", TitledBorder.LEFT, TitledBorder.TOP);
    lenMicro.setBorder(bTitled2); 

在最后两行取消注释时,将显示标题为长度(2/2)"的边框.

The border titled "Length (2/2)" is displayed when the last two lines are uncommented.

推荐答案

使用绝对定位,您必须为每个组件提供位置,它将成为视图的一部分.因此,如果不仔细查看您在做什么,就很难预测出哪里出了问题.尽管使用布局管理器,将减轻您在放置不同组件方面的负担在视图上.

When using Absolute Positioning, you have to provide Location for each and every component, that will become a part of the view. Hence without looking at exactly what you doing it's hard to predict where exactly the things are going wrong. Though using Layout Managers, will remove this big burden off your shoulders regarding placement of different components on the view.

此外,您必须设置相应组件的边框.因此,我绝对不能假设您添加了一个组件,并且该组件出现在两个边界之间(尽管考虑到使用绝对定位这一事实,您可能在视图上为该组件指定了错误的坐标).请看下面的示例代码,它可能会在这个方向上有所帮助:

Moreover, you have to set the border for the respective component. So in no way I can assume that you added one component and it appeared between two borders (though considering the fact that you using Absolute Positioning, you might have given the wrong Co-ordinates for the said component on the view). Please have a look at this example code, that might can help you a bit in this direction :

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

public class BorderExample
{
    private JPanel topPanel;
    private JPanel centerPanel;
    private JPanel bottomPanel;
    private int hgap;
    private int vgap;
    private JTextField tfield1, tfield2;
    private JComboBox cbox1, cbox2;
    private String[] data = {"One", "Two"};

    public BorderExample()
    {
        hgap = 5;
        vgap = 5;
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("Border Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(hgap, hgap, hgap, hgap));
        contentPane.setLayout(new BorderLayout(hgap, vgap));

        topPanel = new JPanel();
        topPanel.setOpaque(true);
        topPanel.setBackground(Color.WHITE);
        topPanel.setBorder(
            BorderFactory.createTitledBorder("Top Panel"));
        tfield1 = new JTextField(10);   
        tfield1.setBorder(
            BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(
                    EtchedBorder.RAISED, Color.GRAY
                    , Color.DARK_GRAY), "JTextField"));
        JPanel comboPanel = new JPanel();           
        cbox1 = new JComboBox(data);
        cbox1.setBorder(
            BorderFactory.createTitledBorder("JComboBox")); 
        topPanel.add(tfield1);  
        topPanel.add(cbox1);

        centerPanel = new JPanel(); 
        centerPanel.setOpaque(true);
        centerPanel.setBackground(Color.WHITE);
        centerPanel.setBorder(
            BorderFactory.createTitledBorder("Center Panel"));
        tfield2 = new JTextField(10);   
        tfield2.setBorder(
            BorderFactory.createLoweredBevelBorder());
        cbox2 = new JComboBox(data);
        cbox2.setBorder(
            BorderFactory.createRaisedBevelBorder());   
        centerPanel.add(tfield2);   
        centerPanel.add(cbox2);

        bottomPanel = new JPanel(); 
        bottomPanel.setOpaque(true);
        bottomPanel.setBackground(Color.WHITE);
        bottomPanel.setBorder(
            BorderFactory.createTitledBorder("Center Panel"));

        contentPane.add(topPanel, BorderLayout.PAGE_START);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(bottomPanel, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new BorderExample().displayGUI();
            }
        });
    }
}

以下是相同的输出:

这篇关于如何用边框包围Java Swing组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆