gpt4 book ai didi

java - 为什么我的标题边框面板这么小

转载 作者:行者123 更新时间:2023-11-30 07:29:03 25 4
gpt4 key购买 nike

我正在创建一个带有图形面板、命令面板和命令列表面板的 GUI。我已经使用 BorderLayout South 将命令面板放置在框架底部,但我的侧面板很小且无法读取。

我将提供一张我希望框架最后的样子的图片:

enter image description here

我目前拥有的:

enter image description here

谁能解释一下为什么 TitledBorder 面板这么小?

我的代码如下:

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);

}

}

并且

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);

enter image description here

仍然不是我想要的,也不确定这是否是我应该做的。我应该更改驱动程序文件而不是直接更改 JPanel 吗?

请注意“清除图形”按钮右侧仍然有一个间隙。有什么办法可以摆脱这个吗?

最佳答案

Could anyone explain why the TitledBorder panel is so small?

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

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

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 像素。

关于java - 为什么我的标题边框面板这么小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36405080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com