我有以下情况。我用GridBagLayout创建了一个JPanel,其比例如下。我想弄清楚这一点,然后我会添加更多的组件。现在我有点像
------------------------------------
|LABEL 45% | LABEL 10% | LABEL 45%|
------------------------------------
每个标签水平和垂直各取1个单元格(网格宽度/高度=1)
JPanel topPanel = new JPanel(new GridBagLayout());
topPanel.add(new JLabel("1"), new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
topPanel.add(new JLabel("2"), new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
topPanel.add(new JLabel("3"), new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
此时一切正常,第一个标签获得与右标签完全相同的空间(这是可用宽度的45%,另一个获得10%)。完美。
但是,当我试图添加另一个带有GridBagLayout的JPanel而不是标签(左/右)时,比例就不按定义保持了。
在左边,我想添加一个JPanel,它的定义如下
----------------------
|JLabel..............|
|--------------------|
|JTextArea...........|
|....................|
----------------------
JTextArea-加权1.0(x)、1.0,西锚,填充两者(以拉伸)
myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE", new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
myPanel.add(new JTextArea(5,25), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
在右边,我想添加一个带有GridBagLayout的JPanel,它包含另外两个带有GridBagLaouts的JPanels(现在描述如何定义没有意义)。
-->因此,当我添加左面板而不是标签时,它得到了大约60%的屏幕(这不对,它应该得到45%)。
你能看出哪里出了问题吗?我没主意了
勒:这是一个简单的例子
public class Example extends JFrame {
public Example() {
init();
}
private JPanel createPanel(Component comp) {
JPanel topPanel = new JPanel(new GridBagLayout());
topPanel.add(comp, new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
topPanel.add(new JLabel("2"), new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
topPanel.add(new JLabel("3"), new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
return topPanel;
}
private JPanel getPanelWithLabel() {
return createPanel(new JLabel("1"));
}
private JPanel getPanelWithPanel() {
JPanel myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
myPanel.add(new JTextArea(5,25), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
return createPanel(myPanel);
}
private void init() {
setTitle("Simple example");
setSize(800, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
panel.add(getPanelWithLabel(), BorderLayout.CENTER);
//panel.add(getPanelWithPanel(), BorderLayout.CENTER);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Example ex = new Example();
}
});
}
}
问题出在jtextarea
的行上。
由于25
行比45%的行宽,因此它本身就会消耗。
我不使用25
,而是使用容器的width
(MyPanel
)
myPanel.add(new JTextArea(5,myPanel.WIDTH), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
因为这种方法的尺寸仍然不同。我尝试首先创建一个JPanel
并向其添加JLabel
。
JPanel threeCont = new JPanel(new GridBagLayout());
threeCont.add(new JLabel("3"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
// We add myPanel and two(JLabel) to topPanel first and than the below.
topPanel.add(threeCont, new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
System.out.println("First Panel's Size : " + myPanel.getSize());
System.out.println("Second Panel's Size : " +two.getSize());
System.out.println("Third Panel's Size : " +threeCont.getSize());
First Panel's Size : java.awt.Dimension[width=441,height=96]
Second Panel's Size : java.awt.Dimension[width=98,height=96]
Third Panel's Size : java.awt.Dimension[width=441,height=16]
public static void main(String[]a) {
JFrame fr = new JFrame();
fr.setSize(new Dimension(1000,1000));
fr.setLocationRelativeTo(null);
JPanel topPanel = new JPanel(new GridBagLayout());
Insets INSETS_0PX = new Insets(0,0,0,0);
JLabel one = new JLabel("1");
JLabel two = new JLabel("2");
one.setBackground(Color.yellow);
two.setBackground(Color.blue);
JPanel myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
myPanel.add(new JTextArea(5,myPanel.WIDTH), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
JPanel threeCont = new JPanel(new GridBagLayout());
threeCont.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
topPanel.add(myPanel, new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
topPanel.add(two, new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
topPanel.add(threeCont, new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
fr.add(topPanel);
fr.setVisible(true);
System.out.println("First Panel's Size : " + myPanel.getSize());
System.out.println("Second Panel's Size : " +two.getSize());
System.out.println("Third Panel's Size : " +threeCont.getSize());
}
任何帮助都是非常感谢的!
编辑:我应该提到的是,我知道纵横比公式:原始高度/原始宽度x新宽度=新高度,但是,我不知道如何正确地使用它,以我的优势。
我试图降低视频的维度: FFMPEG(libx264)“不能被2整除的高度”
我希望我的解释足够好,有人能帮助我:-) 编辑:随着camickr的改进而更新,垂直尺寸问题得到解决。
问题内容: 我正在使用Bootstrap创建轮播,我的图像很大,因此当屏幕小于图像时,不会保持该比例。 我该如何改变? 这是我的代码: 我需要图片适合100%的宽度,但要保持其500px的高度(我认为是500px),这意味着在较小的屏幕上我们看不到图片的最左侧和最右侧。 我尝试将图像包含在div中并添加 但这不起作用 谢谢! 问题答案: 问题在于引导CSS: 设置图像不会超过视口。您需要在CSS上