问题描述
我一直试图设置在以下code固定列宽的年龄......的事情是,当我的标签添加到面板左侧,则宽度自动递增......我想列的宽度是固定的...
谁能帮帮我好吗?
这是code:
进口java.awt.AWTException;
进口java.awt.BorderLayout中;
进口java.awt.Color中;
进口java.awt.Cursor中;
进口java.awt.Dimension中;
进口java.awt.Graphics;
进口java.awt.Graphics2D中;
进口java.awt.GridBagConstraints中;
进口java.awt.GridBagLayout中;
进口java.awt.Image中;
进口java.awt.Panel;
进口java.awt.Point中;
进口java.awt.Robot中;
进口java.awt.Toolkit中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.awt.event.KeyEvent中;
进口java.awt.event.KeyListener;
进口java.awt.event.MouseEvent中;
进口java.awt.event.MouseListener;
进口java.io.BufferedReader中;
进口java.io.BufferedWriter中;
进口java.io.ByteArrayInputStream中;
进口java.io.ByteArrayOutputStream中;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.FileReader;
进口java.io.FileWriter;
进口java.io.IOException异常;
进口的java.util.ArrayList;
进口的java.util.Calendar;
进口java.util.Timer中;
进口java.util.TimerTask中;进口javax.sound.sampled.AudioFileFormat;
进口javax.sound.sampled.AudioFormat中;
进口javax.sound.sampled.AudioInputStream中;
进口javax.sound.sampled.AudioSystem;
进口javax.sound.sampled.DataLine中;
进口javax.sound.sampled.LineUnavailableException;
进口javax.sound.sampled.TargetDataLine;
进口javax.swing.BorderFactory中;
进口javax.swing.ImageIcon中;
进口javax.swing.JButton中;
进口javax.swing.JFrame中;
进口javax.swing.JLabel中;
进口javax.swing.JPanel中;
进口javax.swing.SwingUtilities中;
进口javax.swing.border.Border;进口java.io.IOException异常;公共类CURRENTITEM继承JPanel { 私有静态的JFrame currentScreen; 公共CURRENTITEM(JFrame的P){ GridBagConstraints的C =新的GridBagConstraints(); // TODO Borramos阙罗哈亚EN LA pantalla
this.removeAll();
this.revalidate(); currentScreen = P; this.setBackground(Color.LIGHT_GRAY); this.setLayout(新的GridBagLayout()); currentScreen.add(本); // --->
面板leftPanel =新面板();
leftPanel.setBackground(Color.BLACK); c.weightx = 1.5;
c.weighty = 1;
//c.fill = GridBagConstraints.BOTH;
this.add(leftPanel,C);
//< --- // --->
面板secondPanel =新面板();
secondPanel.setBackground(Color.green); c.weightx = 0.25;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH; this.add(secondPanel,C);
//< --- // --->
面板rightPanel =新面板();
rightPanel.setBackground(Color.CYAN); c.weightx = 1.5;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH; this.add(rightPanel,C);
//< --- currentScreen.setVisible(真); this.requestFocusInWindow();
}
}
的GridBagLayout
放置在一个网格的矩形(单元)的组件,然后使用组件的 preferred尺寸的决定细胞应为多大。
权重来确定如何分配列之间的空间(的weightx
)和行(分量
)之间;这对于指定调整行为很重要。
通常权重以0.0和1.0的极端指定:在之间的数目被用作必要
了解更多
样code:(低于code设置固定宽度列用100%的垂直尺寸)
面板leftPanel =新面板(){
@覆盖
公共尺寸的get preferredSize(){
返回新的Dimension(...,...);
}
};
删除的weightx
的第一列,并覆盖的get preferredSize()
方法。
leftPanel.setBackground(Color.BLACK);c.weighty = 1;
c.fill = GridBagConstraints.VERTICAL;
this.add(leftPanel,C);
I've been trying to set fixed column width in the following code for ages...The thing is that when I add a label to the panel on the left, then the width is incremented automatically...and i Would like the column width to be fixed...
Could anyone help me please??
This is the code:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import java.io.IOException;
public class CurrentItem extends JPanel{
private static JFrame currentScreen;
public CurrentItem(JFrame p) {
GridBagConstraints c = new GridBagConstraints();
//Borramos todo lo que haya en la pantalla
this.removeAll();
this.revalidate();
currentScreen = p;
this.setBackground(Color.LIGHT_GRAY);
this.setLayout(new GridBagLayout());
currentScreen.add(this);
// --->
Panel leftPanel = new Panel();
leftPanel.setBackground(Color.BLACK);
c.weightx = 1.5;
c.weighty = 1;
//c.fill = GridBagConstraints.BOTH;
this.add(leftPanel, c);
// <---
// --->
Panel secondPanel = new Panel();
secondPanel.setBackground(Color.green);
c.weightx = 0.25;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
this.add(secondPanel, c);
// <---
// --->
Panel rightPanel = new Panel();
rightPanel.setBackground(Color.CYAN);
c.weightx = 1.5;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
this.add(rightPanel, c);
// <---
currentScreen.setVisible(true);
this.requestFocusInWindow();
}
}
GridBagLayout
places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.
Weights are used to determine how to distribute space among columns (weightx
) and among rows (weighty
); this is important for specifying resizing behavior.
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary.
Read more How to Use GridBagLayout
Sample code: (below code set fixed width column with 100% vertical size.)
Panel leftPanel = new Panel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(..., ...);
}
};
Remove weightx
for first column and override getPreferredSize()
method.
leftPanel.setBackground(Color.BLACK);
c.weighty = 1;
c.fill = GridBagConstraints.VERTICAL;
this.add(leftPanel, c);
这篇关于GridBagLayout的:如何设置固定的列宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!