2017-03-13 8 views
-1

GridBagLayout을 사용하여 프로그램 용 도구 모음을 작성하고 있지만 weightx 및 fill를 사용하는 경우에도 구성 요소는 크기가 아니며 JPanel을 채우지 않습니다. jidest.x_size의 전체 크기가 1920 인 경우 예를 들어GridBagLayout이 채워지지 않습니다. JPanel

는 구성 요소의 전체 크기는 1776

package jide.parts; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.BoxLayout; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

import jide.jidest; 

@SuppressWarnings("serial") 
public class BottomBar extends JPanel implements MouseListener{ 
JPanel charCountPanel = new JPanel(); 
JPanel lineCountPanel = new JPanel(); 
JPanel cursorPositionPanel = new JPanel(); 
JPanel spacer1 = new JPanel(); 
JPanel spacer2 = new JPanel(); 
JPanel spacer3 = new JPanel(); 
JPanel morespace = new JPanel(); 
JLabel charCount = new JLabel("charcount"); 
JLabel lineCount = new JLabel("linecount"); 
JLabel cursorPosition = new JLabel("TEST:TEST"); 
public BottomBar(){ 
    this.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    charCountPanel.setToolTipText("Click here for more complete stats"); 
    lineCountPanel.setToolTipText("Click here for more complete stats"); 
    setPreferredSize(new Dimension((int) jidest.x_size,20)); 
    setBackground(Color.red); 
    cursorPositionPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    cursorPositionPanel.add(cursorPosition); 
    //cursorPositionPanel.setSize(50,20); 
    lineCountPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    lineCountPanel.add(lineCount); 
    //lineCountPanel.setSize(50,20); 
    charCountPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    charCountPanel.add(charCount); 
    //charCountPanel.setSize(50,20); 
    morespace.setSize((int) (jidest.x_size-156),20); 
    //add(cursorPositionPanel); 
    spacer1.setBackground(Color.BLACK); 
    //spacer1.setSize(1,20); 
    spacer2.setBackground(Color.BLACK); 
    //spacer2.setSize(1,20); 
    spacer3.setBackground(Color.BLACK); 
    //spacer3.setPreferredSize(new Dimension(1,20)); 
    //size is 156, width is 7 
    gbc.fill=GridBagConstraints.BOTH; 
    gbc.weighty=1; 
    gbc.gridx = 1; 
    gbc.weightx=(1.0)/jidest.x_size; 
    add(spacer1,gbc); 
    gbc.gridx=2; 
    gbc.weightx=(50.0)/jidest.x_size; 
    add(cursorPositionPanel,gbc); 
    gbc.gridx=3; 
    gbc.weightx=(1.0)/jidest.x_size; 
    add(spacer2,gbc); 
    gbc.gridx=4; 
    gbc.weightx=(50.0)/jidest.x_size; 
    add(lineCountPanel,gbc); 
    gbc.gridx=5; 
    gbc.weightx=(1.0)/jidest.x_size; 
    add(spacer3,gbc); 
    gbc.gridx=6; 
    gbc.weightx=(50.0)/jidest.x_size; 
    add(charCountPanel,gbc); 
    gbc.gridy = 0; 
    gbc.gridx=0; 
    gbc.weightx=(jidest.x_size-153)/jidest.x_size; 
    add(morespace,gbc); 
    System.out.println((spacer1.getWidth()+cursorPositionPanel.getWidth()+spacer2.getWidth()+lineCountPanel.getWidth()+spacer3.getWidth()+morespace.getWidth())+" should be " + jidest.x_size); 
} 

@Override 
public void mouseClicked(MouseEvent arg0) { 

} 

@Override 
public void mouseEntered(MouseEvent arg0) { 

} 

@Override 
public void mouseExited(MouseEvent arg0) { 

} 

@Override 
public void mousePressed(MouseEvent arg0) { 

} 

@Override 
public void mouseReleased(MouseEvent arg0) { 

} 

} 
+1

문제를 나타내는 [mcve]를 게시하십시오. – camickr

+1

'weightx'는 구성 요소의 정확한 with를 지정하지 않습니다. 컨테이너의 크기가 기본 크기를 초과하는 경우 여분의 공간 분포를 나타내는 상대 값입니다. 사실, 값은 서로 관련이 있기 때문에 모든 weightx 값에서'/ jidest.x_size'를 제거하면 동일한 결과를 얻을 수 있습니다. – VGR

+0

처음부터 그리드를 설정하지 않은 것처럼 보입니다. gridweights를 사용하지 않고 시도해보십시오. 구성 요소가 생성하는 행이나 열의 수는 –

답변

1

미래에서, MCVE을 게시하시기 바랍니다 것입니다. 이렇게하면 막대 너비에 걸쳐 구성 요소가 분산됩니다.

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

public class BottomBarGUI { 

    private JComponent ui = null; 

    BottomBarGUI() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui != null) { 
      return; 
     } 

     ui = new JPanel(new BorderLayout(4, 4)); 
     ui.setBorder(new EmptyBorder(4, 4, 4, 4)); 

     ui.add(new BottomBar()); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       BottomBarGUI o = new BottomBarGUI(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 

class BottomBar extends JPanel { 

    JPanel charCountPanel = new JPanel(); 
    JPanel lineCountPanel = new JPanel(); 
    JPanel cursorPositionPanel = new JPanel(); 
    JPanel spacer1 = new JPanel(); 
    JPanel spacer2 = new JPanel(); 
    JPanel spacer3 = new JPanel(); 
    JPanel morespace = new JPanel(); 
    JLabel charCount = new JLabel("charcount"); 
    JLabel lineCount = new JLabel("linecount"); 
    JLabel cursorPosition = new JLabel("TEST:TEST"); 

    public BottomBar() { 
     this.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridy = 0; 
     gbc.weightx = .5; 
     charCountPanel.setToolTipText("Click here for more complete stats"); 
     lineCountPanel.setToolTipText("Click here for more complete stats"); 
     setBackground(Color.red); 
     cursorPositionPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     cursorPositionPanel.add(cursorPosition); 
     lineCountPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     lineCountPanel.add(lineCount); 
     charCountPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     charCountPanel.add(charCount); 
     spacer1.setBackground(Color.BLACK); 
     spacer2.setBackground(Color.BLACK); 
     spacer3.setBackground(Color.BLACK); 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weighty = 1; 
     gbc.gridx = 0; 
     add(spacer1, gbc); 
     gbc.gridx = 1; 
     add(cursorPositionPanel, gbc); 
     gbc.gridx = 2; 
     add(spacer2, gbc); 
     gbc.gridx = 3; 
     add(lineCountPanel, gbc); 
     gbc.gridx = 4; 
     add(spacer3, gbc); 
     gbc.gridx = 5; 
     add(charCountPanel, gbc); 
     gbc.gridx = 0; 
    } 
} 
+0

입니다. 문제 해결에 도움이된다면 [답변 수락] (http://meta.stackexchange.com/a/5235/155831)하십시오. –