Abr*_*Voy 7 java user-interface swing border
我正在构建一个由几个标签面板组成的应用程序.在每一个上,我都希望通过边框将各组件彼此分开.它看起来像:
|- Titled Border 1 ---
[JTextField] [JComboBox] [JTextField] [JComboBox]
|--------
|- Titled Border 2 ---
[JTextField] [JComboBox] [JTextField] [JComboBox]
|--------
... and so forth.
Run Code Online (Sandbox Code Playgroud)
当我试图简单地向面板添加新的边框"标题边框2"时,它被添加并覆盖了第一个将组件留在顶部.在一些示例中,我看到在一个帧内定义了许多JPanel,并且每个面板都有自己的边框.它可能适用于我的情况,但如何添加这些面板以显示在一个选项卡中?
Oracle的一个教程显示了一个带有多种边框演示的选项卡式窗格.当我尝试编辑它并在其中放置一个组件时,它出现在两个边框之间而不是被包围.而另一种选择对我来说是不成功的.
第二件事是,我没有使用任何布局管理器,组件位置是固定的,老实说我会保留这个设置.或者您可能建议在这种特定情况下使用任何布局管理器?
您对如何解决这个问题有任何暗示吗?
编辑:似乎我还不允许附加截图,但这是代码的一部分,用于显示边框:
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);
Run Code Online (Sandbox Code Playgroud)
取消注释最后两行时,将显示标题为"长度(2/2)"的边框.
nIc*_*cOw 18
使用绝对定位时,您必须为每个组件提供位置,这将成为视图的一部分.因此,如果不确切地看到你在做什么,很难预测事情的确切位置.虽然使用布局管理器,但是在视图上放置不同组件会消除这一巨大负担.
此外,您必须为相应组件设置边框.所以我决不会假设您添加了一个组件并且它出现在两个边框之间(尽管考虑到您使用绝对定位的事实,您可能在视图上为所述组件提供了错误的坐标).请看一下这个示例代码,它可以帮助您朝这个方向发展:
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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是相同的输出:

| 归档时间: |
|
| 查看次数: |
43296 次 |
| 最近记录: |