带有屏幕调整大小的Swing GridBagLayout [英] Swing GridBagLayout with screen resizing

查看:111
本文介绍了带有屏幕调整大小的Swing GridBagLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Java Swing GUI,我有两个主要组件:

For my Java Swing GUI I have two major components:

一部分是几个复选框的垂直列表,另一部分是图像.

One part is a vertical list of a few check boxes, the other is an image.

当调整/最大化jframe窗口时,比例将保持不变,而我宁愿复选框列表的绝对尺寸保持不变,而图像的尺寸却像这样:

When the jframe window is resized/maximised the proportions stay the same when I would much rather the absolute size of the check box list stay the same and just the image resize like:

使用GridBagLayout可以吗?

我一直使用以下方法将所有内容组合成一个JPanel(ContentPane):

I have been laying everthing out into one JPanel (ContentPane) using the following:

复选框:

for(int i=0; i<5; i++){
    GridBagConstraints gbc = new GridBagConstraints();
    JCheckBox checkbox = new JCheckBox("CheckBox " + (i+1));
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = i;// Increases by one for each checkbox
    gbc.insets = new Insets(0, 10, 5, 5);
    gbc.weightx = 1;
    gbc.weighty = 1;
    contentPane.add(checkbox,gbc);
}

图像:注意,由于没有摆动图像组件,我已将其替换为空白JList.我需要该组件与contentPane.add(...)一起使用.

Image: Note I have replaced this with a blank JList as there is no swing Image component I need the component to work with contentPane.add(...).

GridBagConstraints gbc = new GridBagConstraints();
JList list = new JList(new String[]{""});
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 10;
gbc.gridheight = 5;
gbc.gridx = 1;
gbc.gridy = 0;// Increases by one for each checkbox
gbc.insets = new Insets(0, 10, 5, 5);
gbc.weightx = 1;
gbc.weighty = 1;
contentPane.add(list,gbc);

推荐答案

您可以使用BoxLayout来避免复杂性!

You could use BoxLayout instead to avoid complexity!

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

  public class NewClass {

 static JFrame aWindow = new JFrame("This is BOX LAYOUT"); 
 public static void main(String[] args) {

  JPanel panel1 = new JPanel(); 
  panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
  panel1.setBackground(Color.yellow);

  JPanel panel2 = new JPanel(); 
  panel2.setLayout(new FlowLayout());
  panel2.setBackground(Color.ORANGE);

 for(int i = 0; i <5; i++)
   panel1.add(new JCheckBox("CheckBox " + (i+1)));     

    aWindow.add(panel1,BorderLayout.WEST);
    aWindow.add(panel2, BorderLayout.CENTER);
    aWindow.setVisible(true);                    
    aWindow.setSize(300,300); 
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }
}

这篇关于带有屏幕调整大小的Swing GridBagLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆