我正在尝试用 Swing 制作一个 UI,只使用Container
一个GridBagLayout
!
我的问题是我想在界面中的一个标题()下重新组合一些JtextFields
,有没有办法直接在我的容器中添加边框,或者我应该创建另一个来重新组合我的组件,然后将孔面板添加到我的?Jlabels
TitledBorder
JPanel
GridBagLayout
我正在尝试用 Swing 制作一个 UI,只使用Container
一个GridBagLayout
!
我的问题是我想在界面中的一个标题()下重新组合一些JtextFields
,有没有办法直接在我的容器中添加边框,或者我应该创建另一个来重新组合我的组件,然后将孔面板添加到我的?Jlabels
TitledBorder
JPanel
GridBagLayout
根据您提供的图片,典型的解决方案是有 2 个单独的面板,每个面板都有自己的 TitledBorder,然后将这两个面板放在第三个外部面板上。
但是,您可以通过将 TitledBorders 替换为 JLabel 和 JSeparator 的组合来在单个面板上创建类似的效果。
不同之处在于,字段的逻辑“组”现在仅由不围绕整个组的标题定义。有些人喜欢这个,有些人不喜欢。
这是您的图片示例,可以为您提供想法:
import java.awt.*;
import javax.swing.*;
public class Test implements Runnable
{
private JTextField firstName;
private JTextField lastName;
private JTextField title;
private JTextField nickname;
private JComboBox format;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Test());
}
public Test()
{
firstName = new JTextField(20);
lastName = new JTextField(20);
title = new JTextField(20);
nickname = new JTextField(20);
format = new JComboBox();
}
public void run()
{
JFrame frame = new JFrame();
frame.getContentPane().add(createPanel());
frame.pack();
frame.setVisible(true);
}
private JPanel createPanel()
{
JPanel p = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4,4,4,4);
gbc.ipadx = 1;
gbc.ipady = 1;
gbc.anchor = GridBagConstraints.WEST;
JLabel nameHeader = new JLabel("Name:");
nameHeader.setForeground(Color.RED.darker());
p.add(nameHeader, gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
p.add(new JSeparator(JSeparator.HORIZONTAL), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
p.add(new JLabel("First Name"), gbc);
gbc.gridx = 1;
p.add(firstName, gbc);
gbc.gridx = 2;
p.add(new JLabel("Last Name"), gbc);
gbc.gridx = 3;
p.add(lastName, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
p.add(new JLabel("Title"), gbc);
gbc.gridx = 1;
p.add(title, gbc);
gbc.gridx = 2;
p.add(new JLabel("Nickname"), gbc);
gbc.gridx = 3;
p.add(nickname, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
p.add(new JLabel("Format"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
p.add(format, gbc);
return p;
}
}
您可以使用约束来完善它,但您明白了。
这种方法的一个优点是,当为电子邮件部分添加更多字段时,您可以让它们与名称部分中的字段对齐。使用单独的面板,这将更加困难(您可以为此使用一堆 Box.createHorizontalStrut(...) )。
这种方法的缺点是您现在有一个包含许多字段的大型面板,如果您需要添加更多字段,维护起来可能会有点笨拙。
试试这个 :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class SwingLayoutDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public SwingLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();
swingLayoutDemo.showGridBagLayoutDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setBorder(new TitledBorder (
new LineBorder (Color.black, 5),
"Title String"));
controlPanel.add(new JLabel("TitledBorder using LineBorder"));
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showGridBagLayoutDemo(){
headerLabel.setText("Layout in action: GridBagLayout");
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
panel.setSize(300,300);
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Button 1"),gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(new JButton("Button 2"),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new JButton("Button 3"),gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(new JButton("Button 4"),gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
panel.add(new JButton("Button 5"),gbc);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}