随着窗口扩展,网格布局中的Java-TitledBorder扩展

时间:2016-02-07 20:53:59

标签: java swing gridbaglayout titled-border

我正在使用带有标题边框的GridBag布局制作表单。客户详细信息的第一个TitledBorder panel工作正常,但我想知道如何在第一个标题和textfield(例如名字)和以下标题之间添加一些间距{ {1}}(例如姓氏)在它下面。

第二个textfield是房间详细信息的问题在于,当我放大/扩展窗口时它会扩展,当这种情况发生时,它内部的组件也会发生变化。我希望它像第一个panel中的组件一样保持固定。

这是form.java类:

panel

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




       }

 }

以下是截图: enter image description here

1 个答案:

答案 0 :(得分:2)

您已将第二个面板添加到CENTRE的{​​{1}}位置,这是此类布局的预期行为。也许您应该使用另一个BorderLayout来布局两个面板

要向面板添加内部间距,您可以使用GridBagLayout,将CompoundLayoutTitledBorder包裹在一起或设置EmptyBorder属性

简单示例......

Simple

GridBagConstraints#insets
  

非常感谢。您能告诉我如何更改边框标题的字体和大小(如客户详细信息,预订详细信息)吗? Font.BOLD似乎不适用于createTitledBorder

...

  

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

...

  

那不起作用

对我来说很好......

TitledBorder

new TitledBorder(null, "Customer Details", TitledBorder.CENTER, TitledBorder.TOP, UIManager.getFont("Label.font").deriveFont(Font.BOLD), null);