2017-07-25 71 views
0

我有一个JPanel三个按钮和一个标签。一个按钮和标签位于第一行,另外两个按钮位于第二行。我需要标签位于右上方,但这样做会导致标签意外增大,并将两个向下按钮向左推。Java Swing GridBagLayout:JLabel占用太多空间

public class UserInterface extends JFrame { 
     int width; 
     int height; 

     JPanel panel; 
     public static void main(String[] args) { 
       run(); 
     } 
     public UserInterface() { 
       setup(); 
     } 

     private void setup() { 
       width=800; 
       height=600; 

       panel=new UserInterfacePanel(); 
       getContentPane().add(panel, BorderLayout.NORTH); 


       setSize(width, height); 

       setLocationRelativeTo(null); 
       setDefaultCloseOperation(EXIT_ON_CLOSE); 
     } 

     public static void run() { 
       UserInterface gui=new UserInterface(); 
       gui.setVisible(true); 
     } 
} 

class UserInterfacePanel extends JPanel { 
      private JToggleButton startButton; 
      private JToggleButton stopButton; 

      private JButton homeStatusButton; 
      private JLabel timeStatusLabel; 

      public static void main(String[] args) { 

      } 

      public UserInterfacePanel() { 
       setup(); 
      } 

      private void setup() { 
       setLayout(new GridBagLayout()); 

       setupButtons(); 

       timeStatusLabel=new JLabel(); 
       timeStatusLabel.setMinimumSize(new Dimension(180,200)); 
       timeStatusLabel.setPreferredSize(new Dimension(180,200)); 
       timeStatusLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2)); 

       GridBagConstraints c1 = new GridBagConstraints(); 
       c1.fill = GridBagConstraints.HORIZONTAL; 

       c1.weightx=0; 
       c1.weighty=0; 
       c1.anchor=GridBagConstraints.CENTER; 
       c1.gridx=0; 
       c1.gridy=0; 
       c1.insets=new Insets(20, 20, 250, 50); 
       add(homeStatusButton, c1); 
       c1.gridx=1; 
       c1.gridy=0; 
       c1.insets=new Insets(0, 50, 10, 10); 
       c1.weightx=1; 
       add(timeStatusLabel, c1); 


       GridBagConstraints c2=new GridBagConstraints(); 
       c2.insets=new Insets(30,20,30,20); 
       c2.anchor=GridBagConstraints.CENTER; 
       c2.weightx=0.0; 
       c2.gridx=0; 
       c2.gridy=1; 
       add(startButton, c2); 

       c2.gridx=1; 
       c2.gridy=1; 
       add(stopButton, c2); 
      } 

      private void setupButtons() { 
        startButton=new JToggleButton("Start Button"); 
        stopButton=new JToggleButton("Stop Button"); 

        homeStatusButton=new JButton("OUT"); 
        homeStatusButton.setBackground(Color.RED); 
        homeStatusButton.setForeground(Color.BLACK); 
        homeStatusButton.setSize(new Dimension(100, 20)); 
      } 
    } 

我不明白为什么会发生这种情况。看起来我隐含地说明了按钮的位置以及标签的位置。为什么标签变大,按钮被推到左边。

enter image description here

+0

您正在为标签设置插页。 – camickr

+0

@camickr,是的,但它仍然太大,并且对按钮来说太大了。 – parsecer

+1

你是什么意思太大。您可以使用(250,50)的添加插入来指定大小(180,200)。 “按钮的推动力太大” - 所有组件都使用约束,除非每次将组件添加到面板时重置约束。 – camickr

回答

1

但它仍然太大的标签

的你与(250,50)附加插图指定一个大小为(180,200)。

太大的推的按钮

约束被使用的所有组件,除非你每次都重新设置限制你一个组件添加到面板上。

因此,除非您重置插页,否则标签的插页也用于按钮。

此外,请记住,GridBagLayout将定位与单元格的compnents。因此,第一列的单元格等于两个按钮的最大宽度。第二列的宽度将是标签的宽度,因为它是该列中最大的组件。

如果您想将开始/停止按钮放在一起,请先将它们添加到面板,然后将按钮面板添加到GridBagLayout面板。