2017-03-13 2 views
-1

GridBagLayoutを使用してプログラム用のツールバーを構築していますが、weightxとfillを使用しても、コンポーネントはサイズではなく、JPanelを埋めていません。例えばGridBagLayoutが塗りつぶされないJPanel

jidest.x_sizeの合計サイズが1920である場合、成分の合計サイズは、将来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

あなたが最初にgridyを設定するのを忘れたように見えます。グリッドウェイトを使用しないで試してみてください。これは、コンポーネントが生成する行または列の数です。 –

答えて

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)してください。 –

関連する問題