2016-07-27 136 views
-1

我正在学习使用GridBagLayout,我想帮助我使用此发行版的代码。如何使用GridBagBayout执行此操作?

我想画的是在左,但仍然没有成功。做这件事的最好方法是什么?

public class PanelGB extends JPanel { 

    JLabel nombre = new JLabel("Nombre1",SwingConstants.CENTER); 
    JLabel grupo = new JLabel("Grupo1",SwingConstants.LEFT); 
    JLabel cod1 = new JLabel("01",SwingConstants.CENTER); 
    JLabel cod2 = new JLabel("243",SwingConstants.CENTER); 
    JButton btn = new JButton("B"); 

    public PanelGB() { 
     GridBagLayout gbl = new GridBagLayout(); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     setBorder(BorderFactory.createLineBorder(Color.red)); 
     nombre.setBorder(BorderFactory.createLineBorder(Color.black)); 
     grupo.setBorder(BorderFactory.createLineBorder(Color.black)); 
     cod1.setBorder(BorderFactory.createLineBorder(Color.black)); 
     cod2.setBorder(BorderFactory.createLineBorder(Color.black)); 
     grupo.setAlignmentX(0); 

     this.setLayout(gbl); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 5; 
     gbc.gridheight = 2; 
     gbl.setConstraints(nombre, gbc); 
     add(nombre); 

     gbc.gridx = 0; 
     gbc.gridy = 2; 
     gbc.gridwidth = 5; 
     gbc.gridheight = 1; 
     gbl.setConstraints(grupo, gbc); 
     add(grupo); 

     gbc.gridx = 0; 
     gbc.gridy = 3; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     gbl.setConstraints(cod1, gbc); 
     add(cod1); 

     gbc.gridx = 1; 
     gbc.gridy = 3; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     gbl.setConstraints(cod2, gbc); 
     add(cod2); 

     gbc.gridx = 4; 
     gbc.gridy = 3; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     gbl.setConstraints(btn, gbc); 
     add(btn); 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.add(new PanelGB()); 
     frame.pack(); 
     frame.setVisible(true); 

    } 
} 

回答

4

查看使其工作所需的简单更改的注释。

enter image description here

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.CompoundBorder; 
import javax.swing.border.EmptyBorder; 

public class PanelGB extends JPanel { 

    JLabel nombre = new JLabel("Nombre1", SwingConstants.CENTER); 
    JLabel grupo = new JLabel("Grupo1", SwingConstants.LEFT); 
    JLabel cod1 = new JLabel("01", SwingConstants.CENTER); 
    JLabel cod2 = new JLabel("243", SwingConstants.CENTER); 
    JButton btn = new JButton("B"); 

    public PanelGB() { 
     GridBagLayout gbl = new GridBagLayout(); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     // make the entire GUI less cramed up & 'crowded' 
     gbc.insets = new Insets(4, 4, 4, 4); 

     setBorder(BorderFactory.createLineBorder(Color.red)); 
     // give the label more horizontal white space using an empty border.. 
     nombre.setBorder(new CompoundBorder(new EmptyBorder(0, 40, 4, 40), 
       BorderFactory.createLineBorder(Color.black))); 
     grupo.setBorder(BorderFactory.createLineBorder(Color.black)); 
     cod1.setBorder(BorderFactory.createLineBorder(Color.black)); 
     cod2.setBorder(BorderFactory.createLineBorder(Color.black)); 
     grupo.setAlignmentX(0); 

     this.setLayout(gbl); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 5; 
     gbc.gridheight = 2; 
     add(nombre, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 2; 
     gbc.gridwidth = 5; 
     gbc.gridheight = 1; 
     // push the label to the start of the line 
     gbc.anchor = GridBagConstraints.LINE_START; 
     add(grupo, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 3; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     add(cod1, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 3; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     add(cod2, gbc); 

     gbc.gridx = 4; 
     gbc.gridy = 3; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     // push the button to the end 
     gbc.anchor = GridBagConstraints.LINE_END; 
     add(btn, gbc); 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.add(new PanelGB()); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+0

请[接受答案】(http://meta.stackexchange.com/a/5235/155831),如果它有助于解决这个问题。 –