Domanda

I am trying to make an UI with Swing using only one Container with the GridBagLayout !

My problem is that I want to regroup some JtextFields and Jlabels under one title (TitledBorder) in my interface, is there a way to add the border directly in my container, or should I create another JPanel to regroup my components and then add the hole Panel to my GridBagLayout ?

È stato utile?

Soluzione

Based on the pic you provided, the typical solution would be to have 2 separate panels, each with their own TitledBorder, and then place both of these panels on a third outer panel.

However, you could create a similar effect on a single panel by replacing the TitledBorders with a combination of a JLabel followed by a JSeparator.

The difference is that the logical "group" of fields is now only defined by title that isn't surrounding the whole group. Some people prefer this, others do not.

Here's a sample of your pic to give you the idea:

enter image description here

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

You could play with the constraints to polish this up a bit, but you get the idea.

An upside to this approach is that when adding more fields for the Email section, you can get them to line up with the fields in the Name section. With separate panels, this would be more difficult (you could use a bunch of Box.createHorizontalStrut(...) for this).

The downside to this approach is that you now have a large panel with many fields, and it could get a bit unwieldy to maintain if you need to add more fields.

Altri suggerimenti

Try This :

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

enter image description here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top