为什么我的“标题边框"面板这么小 [英] Why is my Titled Border Panel so small

查看:89
本文介绍了为什么我的“标题边框"面板这么小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建带有图形面板,命令面板和命令列表面板的GUI.我使用BorderLayout South将命令面板放在框架底部,但我的侧面板很小而且不可读.

I am creating a GUI with a graphics panel, a command panel and a Command List panel. I've got the command panel where I want it at the bottom of the frame using BorderLayout South but my side panel is just tiny and unreadable.

下面将提供一张我想要我的框架外观的图片:

Ill provide a picture of what I want my frame to look like at the end:

我目前拥有的东西:

有人可以解释为什么TitledBorder面板这么小吗?

Could anyone explain why the TitledBorder panel is so small?

我的代码如下:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class PenDriver {

public static void main(String[] args) {

    JFrame frame = new JFrame("Pen Simulator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);

    penPanel panel = new penPanel();

    frame.add(panel);

    frame.setVisible(true);

}

}

AND

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;

public class penPanel extends JPanel {

private JTextField userCommand;
private JLabel instruction1;
private JButton instruct, clear;
private JLabel cmd1;

public penPanel() {

    setLayout(new BorderLayout());

    // CREATE THE COMMAND PANEL///////

    // Set Layout
    JPanel command = new JPanel();
    command.setLayout(new BoxLayout(command, BoxLayout.LINE_AXIS));
    // Create Label and add to panel
    instruction1 = new JLabel("Enter Command:");
    // Create Buttons
    instruct = new JButton("Execute");
    clear = new JButton("Clear Graphics");

    // Create Text Field to panel
    userCommand = new JTextField(10);

    command.add(instruction1);
    command.add(Box.createRigidArea(new Dimension(4, 0)));
    command.add(userCommand);
    command.add(Box.createRigidArea(new Dimension(2, 0)));
    command.add(instruct);
    command.add(Box.createRigidArea(new Dimension(2, 0)));
    command.add(clear);

    // COMMAND PANEL FINISHED////////

    // CREATE THE COMMAND LIST PANEL//////////
    JPanel cmdList = new JPanel();
    cmdList.setBorder(BorderFactory.createTitledBorder("Command List:"));
    cmdList.setLayout(new BoxLayout(cmdList, BoxLayout.PAGE_AXIS));
    cmd1 = new JLabel("UP = up");
    cmdList.setSize(new Dimension(50, 400));
    cmdList.add(cmd1);

    add(command, BorderLayout.SOUTH);
    add(cmdList, BorderLayout.EAST);

}

}

谢谢!

修改此代码后:

    cmdList.setPreferredSize(new Dimension(120, 800));
    cmdList.add(cmd1);

    add(command, BorderLayout.SOUTH);
    command.add(Box.createRigidArea(new Dimension(120, 0)));
    add(cmdList, BorderLayout.EAST);

仍然不太了解我要做什么,并且不确定这是否是我应该做的.我应该直接更改驱动程序文件而不是JPanels吗?

Still not quite what im going for and not sure if it's what I am supposed to do. Should I be altering the driver file rather than the JPanels directly?

请注意,清除图形"按钮右侧仍然存在间隙.有什么办法摆脱它?

Notice how there is still a gap to the right of the "Clear Graphics" Button. Any way to get rid of that?

推荐答案

有人可以解释为什么TitledBorder面板这么小吗?

Could anyone explain why the TitledBorder panel is so small?

边框中文本的大小不用于确定组件的大小.因此,宽度取决于您添加到面板中组件的首选大小.

The size of the text in the border is not used to determine the size of the component. So the width is determined by the preferred size of the component you add to the panel.

因此,您需要覆盖面板的getPreferredSize()方法以返回默认的首选大小计算的最大值或标题边框的大小:

So you need to override the getPreferredSize() method of the panel to return the maximum of the default preferred size calculation or the size of the titled border:

JPanel cmdList = new JPanel()
{
    @Override
    public Dimension getPreferredSize()
    {
        Dimension preferredSize = super.getPreferredSize();

        Border border =    getBorder();
        int borderWidth = 0;

     if (border instanceof TitledBorder)
     {
         Insets insets = getInsets();
         TitledBorder titledBorder = (TitledBorder)border;
         borderWidth = titledBorder.getMinimumSize(this).width + insets.left + insets.right;
     }

     int preferredWidth = Math.max(preferredSize.width, borderWidth);

     return new Dimension(preferredWidth, preferredSize.height);
    }
};

请注意,清除图形"按钮右侧仍然存在间隙.有什么办法摆脱它?

Notice how there is still a gap to the right of the "Clear Graphics" Button. Any way to get rid of that?

command.add(Box.createRigidArea(new Dimension(120, 0)));

您刚刚在命令面板中添加了刚性区域,因此您要求在末尾增加120像素.

You just added the rigid area to the command panel so you asked to have the extra 120 pixels at the end.

这篇关于为什么我的“标题边框"面板这么小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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