我正在使用具有标题边框的 GridBag
布局制作一个表单。第一个 TitledBorder
panel
是客户详细信息,工作正常,只是我想知道如何在第一个标题和 textfield
之间添加一些间距(例如名字)和以下标题以及其下方的文本字段
(例如姓氏)。
第二个面板
(即“房间详细信息”)的问题在于,当我放大/扩展窗口时,它会扩展,并且当发生这种情况时,其中的组件也会发生变化。我希望它像第一个面板
中的组件一样保持固定。
这是 form.java 类:
public class form extends JFrame{
JPanel pnl= new JPanel();
JPanel pnl1= new JPanel();
JLabel fname= new JLabel("First name: ");
JLabel lname= new JLabel("Last name: ");
JLabel contact= new JLabel("Contact number: ");
JLabel email= new JLabel("Email address: ");
JLabel address= new JLabel("Address: ");
JLabel numpsns= new JLabel("Number of persons: ");
JTextField fnameField= new JTextField(25);
JTextField lnameField= new JTextField(25);
JTextField contactField= new JTextField(25);
JTextField emailField= new JTextField(25);
JTextArea txtadd= new JTextArea(5, 25);
SpinnerModel sm= new SpinnerNumberModel(1,0,30,1);
JSpinner spinner= new JSpinner(sm);
public form(){
this.setTitle("Reservation Form");
pnl.setBorder(new TitledBorder(null,"Customer Details", TitledBorder.CENTER, TitledBorder.TOP, null, null));
getContentPane().add(pnl, BorderLayout.NORTH);
pnl.setLayout(new GridBagLayout());
GridBagConstraints gc= new GridBagConstraints();
//first column of the grid//
gc.anchor= GridBagConstraints.EAST;
gc.weightx=0.5;
gc.weighty=0.5;
gc.gridx=0;
gc.gridy=0;
pnl.add(fname, gc);
gc.gridx=0;
gc.gridy=1;
pnl.add(lname,gc);
gc.gridx=0;
gc.gridy=2;
pnl.add(contact, gc);
gc.gridx=0;
gc.gridy=3;
pnl.add(email, gc);
gc.gridx=0;
gc.gridy=4;
pnl.add(address, gc);
//second column//
gc.anchor= GridBagConstraints.WEST;
gc.gridx=1;
gc.gridy= 0;
pnl.add(fnameField,gc);
gc.gridx=1;
gc.gridy=1;
pnl.add(lnameField, gc);
gc.gridx=1;
gc.gridy=2;
pnl.add(contactField, gc);
gc.gridx=1;
gc.gridy=3;
pnl.add(emailField, gc);
gc.gridx=1;
gc.gridy=4;
pnl.add(txtadd, gc);
//second Titled Border//
pnl1.setBorder(BorderFactory.createTitledBorder(null, "Booking Details", TitledBorder.CENTER, TitledBorder.CENTER));
add(pnl1, BorderLayout.CENTER);
pnl1.setLayout(new GridBagLayout());
GridBagConstraints gc1= new GridBagConstraints();
//first column//
gc1.weightx= 0.5;
gc1.weighty=0.5;
gc1.gridx=0;
gc1.gridy=0;
pnl1.add(numpsns, gc1);
gc1.anchor= GridBagConstraints.WEST;
gc1.gridx=1;
gc1.gridy= 0;
pnl1.add(spinner,gc1);
}
}
form_main.java类
public class form_main {
public static void main(String[] args) {
form form_display= new form();
form_display.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
form_display.pack();
form_display.setSize(500,280);
form_display.setVisible(true);
}
}
最佳答案
您已将第二个面板添加到 BorderLayout
的 CENTRE
位置,这是此类布局的预期行为。也许您应该使用另一个 GridBagLayout 来布局两个面板
要向面板添加内部间距,您可以使用 CompoundLayout
,将 TitledBorder
和 EmptyBorder
包装在一起,或者设置 GridBagConstraints#insets
属性
简单示例...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class Form extends JFrame {
JPanel pnl = new JPanel();
JPanel pnl1 = new JPanel();
JLabel fname = new JLabel("First name: ");
JLabel lname = new JLabel("Last name: ");
JLabel contact = new JLabel("Contact number: ");
JLabel email = new JLabel("Email address: ");
JLabel address = new JLabel("Address: ");
JLabel numpsns = new JLabel("Number of persons: ");
JTextField fnameField = new JTextField(25);
JTextField lnameField = new JTextField(25);
JTextField contactField = new JTextField(25);
JTextField emailField = new JTextField(25);
JTextArea txtadd = new JTextArea(5, 25);
SpinnerModel sm = new SpinnerNumberModel(1, 0, 30, 1);
JSpinner spinner = new JSpinner(sm);
public Form() {
this.setTitle("Reservation Form");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = gbc.HORIZONTAL;
Border border = new CompoundBorder(
new TitledBorder(null, "Customer Details", TitledBorder.CENTER, TitledBorder.TOP, null, null),
new EmptyBorder(10, 10, 10, 10));
pnl.setBorder(border);
getContentPane().add(pnl, gbc);
pnl.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
//first column of the grid//
gc.anchor = GridBagConstraints.EAST;
gc.weightx = 0.5;
gc.weighty = 0.5;
gc.gridx = 0;
gc.gridy = 0;
pnl.add(fname, gc);
gc.gridx = 0;
gc.gridy = 1;
pnl.add(lname, gc);
gc.gridx = 0;
gc.gridy = 2;
pnl.add(contact, gc);
gc.gridx = 0;
gc.gridy = 3;
pnl.add(email, gc);
gc.gridx = 0;
gc.gridy = 4;
pnl.add(address, gc);
//second column//
gc.anchor = GridBagConstraints.WEST;
gc.gridx = 1;
gc.gridy = 0;
pnl.add(fnameField, gc);
gc.gridx = 1;
gc.gridy = 1;
pnl.add(lnameField, gc);
gc.gridx = 1;
gc.gridy = 2;
pnl.add(contactField, gc);
gc.gridx = 1;
gc.gridy = 3;
pnl.add(emailField, gc);
gc.gridx = 1;
gc.gridy = 4;
pnl.add(txtadd, gc);
//second Titled Border//
pnl1.setBorder(BorderFactory.createTitledBorder(null, "Booking Details", TitledBorder.CENTER, TitledBorder.CENTER));
add(pnl1, gbc);
pnl1.setLayout(new GridBagLayout());
GridBagConstraints gc1 = new GridBagConstraints();
//first column//
gc1.weightx = 0.5;
gc1.weighty = 0.5;
gc1.gridx = 0;
gc1.gridy = 0;
pnl1.add(numpsns, gc1);
gc1.anchor = GridBagConstraints.WEST;
gc1.gridx = 1;
gc1.gridy = 0;
pnl1.add(spinner, gc1);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Form frame = new Form();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Thanks a lot. Can you show me how I can change the font and size of the titles(like Customer Details, Booking Details) of the borders? Font.BOLD doesn't seem to work in createTitledBorder
...
new TitledBorder(null, "Customer Details", TitledBorder.CENTER, TitledBorder.TOP, UIManager.getFont("Label.font").deriveFont(Font.BOLD), null);
...
That didn't work
对我来说效果很好...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
JPanel p1 = new JPanel();
p1.setBorder(new TitledBorder(null, "Customer Details", TitledBorder.CENTER, TitledBorder.TOP, null, null));
JPanel p2 = new JPanel();
p2.setBorder(new TitledBorder(null, "Customer Details", TitledBorder.CENTER, TitledBorder.TOP, UIManager.getFont("Label.font").deriveFont(Font.BOLD), null));
JPanel p3 = new JPanel();
p3.setBorder(new TitledBorder(null, "Customer Details", TitledBorder.CENTER, TitledBorder.TOP, UIManager.getFont("Label.font").deriveFont(Font.ITALIC), null));
JPanel p4 = new JPanel();
p4.setBorder(new TitledBorder(null, "Customer Details", TitledBorder.CENTER, TitledBorder.TOP, UIManager.getFont("Label.font").deriveFont(Font.BOLD | Font.ITALIC), null));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(p1, gbc);
add(p2, gbc);
add(p3, gbc);
add(p4, gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
关于Java-网格袋布局中的 TitledBorder 随着窗口扩展而扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35259009/