Java 如何获取标题边界';要在GUI中正确显示的标题
除了一件事之外,我的GUI大部分都能正常显示。标题边框(“数字类型”)不显示右侧JPanel的整个标题。我相信这与BorderLayout经理有关。不在边框内显示“数字类型”,只显示“数字…”。任何帮助都将不胜感激Java 如何获取标题边界';要在GUI中正确显示的标题,java,swing,layout-manager,Java,Swing,Layout Manager,除了一件事之外,我的GUI大部分都能正常显示。标题边框(“数字类型”)不显示右侧JPanel的整个标题。我相信这与BorderLayout经理有关。不在边框内显示“数字类型”,只显示“数字…”。任何帮助都将不胜感激 public class P3GUI extends JFrame { private JLabel originalList; private JTextField originalSort; private JLabel sortedList;
public class P3GUI extends JFrame {
private JLabel originalList;
private JTextField originalSort;
private JLabel sortedList;
private JTextField newSort;
private JPanel panel;
private JButton performSort;
private JRadioButton ascending;
private JRadioButton descending;
private ButtonGroup sort;
private JRadioButton integer;
private JRadioButton fraction;
private ButtonGroup numType;
private JPanel inputPanel, outputPanel, calculatePanel, radioPanel;
private JPanel left, right;
public P3GUI () {
super("Binary Search Tree Sort");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
originalList = new JLabel("Original List");
originalSort = new JTextField(20);
inputPanel = new JPanel();
inputPanel.add(originalList);
inputPanel.add(originalSort);
sortedList = new JLabel("Sorted List");
newSort = new JTextField(20);
newSort.setEditable(false);
outputPanel = new JPanel();
outputPanel.add(sortedList);
outputPanel.add(newSort);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(inputPanel);
panel.add(outputPanel);
add(panel, BorderLayout.NORTH);
performSort = new JButton("Perform Sort");
calculatePanel = new JPanel();
calculatePanel.add(performSort);
add(calculatePanel, BorderLayout.CENTER);
ascending = new JRadioButton("Ascending");
descending = new JRadioButton("Descending");
sort = new ButtonGroup();
sort.add(ascending);
sort.add(descending);
integer = new JRadioButton("Integer");
fraction = new JRadioButton("Fraction");
numType = new ButtonGroup();
numType.add(integer);
numType.add(fraction);
radioPanel = new JPanel();
radioPanel.setLayout(new FlowLayout());
left = new JPanel();
left.setLayout(new GridLayout(2,1));
left.setBorder(BorderFactory.createTitledBorder("Sort Order"));
left.add(ascending);
left.add(descending);
right = new JPanel();
right.setLayout(new GridLayout(2,1));
right.setBorder(BorderFactory.createTitledBorder("Numeric Type"));
right.add(integer);
right.add(fraction);
radioPanel.add(left);
radioPanel.add(right);
add(radioPanel, BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new P3GUI().setVisible(true);
}
});
}
}
问题是右侧的
JPanel
太小,无法显示整个标题,因此会被截断。我建议将底部的两个JPanel放在另一个使用GridLayout
的JPanel中,然后将它们放在适当的位置,使它们能够扩展以适应GUI的底部。当展开时,标题有更大的机会被充分展示(但请注意,这不是保证!)
例如,如果您使主GUI使用BorderLayout
,并使用JPanel
将此GridLayout
添加到BorderLayout.CENTER
位置,它将完全填充该位置。然后,可以将顶部组件文本字段和JButton
添加到另一个JPanel
,例如使用网格布局的组件,并将其添加到主JPanel
的边界布局。PAGE_START
位置
例如,以下代码生成此GUI:
或者,您可以将上述BTN面板放入主面板边框布局。居中
,然后将RadioPanel放入主面板边框布局。页面结束
。这将显示相同外观的GUI,但如果重新调整大小,其扩展将不同 问题是右侧的JPanel
太小,无法显示整个标题,因此被截断。我建议将底部的两个JPanel放在另一个使用GridLayout
的JPanel中,然后将它们放在适当的位置,使它们能够扩展以适应GUI的底部。当展开时,标题有更大的机会被充分展示(但请注意,这不是保证!)
例如,如果您使主GUI使用BorderLayout
,并使用JPanel
将此GridLayout
添加到BorderLayout.CENTER
位置,它将完全填充该位置。然后,可以将顶部组件文本字段和JButton
添加到另一个JPanel
,例如使用网格布局的组件,并将其添加到主JPanel
的边界布局。PAGE_START
位置
例如,以下代码生成此GUI:
或者,您可以将上述BTN面板放入主面板边框布局。居中
,然后将RadioPanel放入主面板边框布局。页面结束
。这将显示相同外观的GUI,但如果重新调整大小,其扩展将不同 < P>面板的首选大小(由布局管理器确定)不考虑在<代码>标题边框< /代码>中的文本大小,因此标题可以被截断。< /P>
下面是一个自定义JPanel,可以与标题顺序一起使用。getPreferredSize()
方法已定制为使用以下最大宽度:
默认的getPreferredSize()计算
标题边框中文本的宽度
下面是一个简单的例子:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TitledBorderPanel extends JPanel
{
/**
** The preferred width on the panel must consider the width of the text
** used on the TitledBorder
*/
@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);
}
private static void createAndShowGUI()
{
JPanel panel = new TitledBorderPanel();
panel.setBorder( BorderFactory.createTitledBorder("File Options Command List:") );
panel.setLayout( new BoxLayout(panel, BoxLayout.Y_AXIS) );
panel.add( new JLabel("Open") );
panel.add( new JLabel("Close") );
// panel.add( new JLabel("A really wierd file option longer than border title") );
JFrame frame = new JFrame("TitledBorderPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
面板的首选大小(由布局管理器确定)不考虑<代码>标题边框中的文本大小,因此标题可以截断。< /P>
下面是一个自定义JPanel,可以与标题顺序一起使用。
getPreferredSize()
方法已定制为使用以下最大宽度:
默认的getPreferredSize()计算
标题边框中文本的宽度
下面是一个简单的例子:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TitledBorderPanel extends JPanel
{
/**
** The preferred width on the panel must consider the width of the text
** used on the TitledBorder
*/
@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);
}
private static void createAndShowGUI()
{
JPanel panel = new TitledBorderPanel();
panel.setBorder( BorderFactory.createTitledBorder("File Options Command List:") );
panel.setLayout( new BoxLayout(panel, BoxLayout.Y_AXIS) );
panel.add( new JLabel("Open") );
panel.add( new JLabel("Close") );
// panel.add( new JLabel("A really wierd file option longer than border title") );
JFrame frame = new JFrame("TitledBorderPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
请查看要回答的编辑。请查看要回答的编辑。