java - 无法更改 GridLayout 中 JLabel 的大小

标签 java swing user-interface jlabel layout-manager

我有一个标签,它会显示从“0 ft”到“999 ft”的字符串,因此最大需要大小为 6 的字符串。我正在使用网格布局,以便我可以将该标签放置在特定的位置列应在的位置。我也想要标签的背景。但问题是我无法正确设置背景的大小。

I want to size down the black part to only the required amount which im not ablet to do

    public Controller() {
    super(new GridLayout(9, 9));
    try {
        Background = ImageIO.read(getClass().getResourceAsStream(
                "background.jpg"));
        TestImage = ImageIO.read(getClass().getResourceAsStream(
                "VTOLHorizontalLevelIndicator.png"));
        setPreferredSize(new Dimension(Background.getWidth(null),
                Background.getHeight(null)));

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    for (Direction direction : Direction.values()) {
        directionMap.put(direction, false);
    }
    setKeyBindings();
    Timer timer = new Timer(TIMER_DELAY, new TimerListener());
    timer.start();
    int i = 3;
    int j = 11;
    JLabel[][] panelHolder = new JLabel[i][j];
    setLayout(new GridLayout(i, j));

    for (int m = 0; m <= 2; m++) {
        for (int n = 0; n <= 10; n++) {
            if (m == 1 && n == 1) {
                lblAltitude.setFont(new Font("Gotham Bold",1,12));
                lblAltitude.setToolTipText("Altitude");
                lblAltitude.setOpaque(true);
                lblAltitude.setBackground(Color.black);
                lblAltitude.setMinimumSize(new Dimension(50,25));
                add(lblAltitude);
            } else {
                panelHolder[m][n] = new JLabel(" ");
                add(panelHolder[m][n]);
            }
        }
    }

}

上面的代码显示了我从中初始化 GridLayout 和标签的构造函数。 我使用的布局是否正确? 我主要关心的是在正确的位置放置标签,可能不是以网格方式,而是在此 GUI 中的某些坐标处。

最佳答案

GridLayout 将展开所有组件以填充面板(使每个组件大小相等)。

关于java - 无法更改 GridLayout 中 JLabel 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039357/

相关文章:

html - 如何在 Bootstrap 上使用模态创建多页表单

java - 在框架中的面板中绘图,我有标题,但冒号到目前为止不起作用

java - 如何在 Kotlin 中向 Gson 注册一个 InstanceCreator?

java - JLabel 鼠标点击图标或文本

java - 具有指定分辨率的独立于平台的全屏 JFrame?

java - 是否有可能在 Web 前端显示 IMS 屏幕?

java - Android,不生成ActionBar

java - Swagger 不扫描位于不同 jar 文件中的实体类中的 ApiModel 和 ApiModelProperty 注释

java - 找出 YearMonth 实例的最后一天(在 LocalDate 中) - joda 库

java - 如何从 jinternalframe 调用 jdialog