简体   繁体   English

如何获取TitledBorder的标题以在GUI中正确显示

[英]How to get the TitledBorder's title to display properly in the GUI

I have the GUI displaying properly for the most part, except for one thing. 除了一件事外,我的GUI大部分都能正常显示。 The TitledBorder("Numeric Type") does not display the entire title for the right JPanel. TitledBorder(“ Numeric Type”)不会显示右侧JPanel的整个标题。 I believe it has something to do with the BorderLayout Manager. 我相信这与BorderLayout Manager有关。 Instead of displaying "Numeric Type" within the border, just "Numeric..." displays. 而不是在边框内显示“数字类型”,仅显示“数字...”。 Any help will be greatly appreciated. 任何帮助将不胜感激。

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

}

The problem is that the right JPanel is too small to display the entire title, and so it gets truncated. 问题在于,正确的JPanel太小而无法显示整个标题,因此它会被截断。 I'd suggest placing the bottom two JPanels into another that uses GridLayout , and then place them in such a way that they expand to fit the bottom of your GUI. 我建议将底部的两个JPanels放置在另一个使用GridLayout JPanel中,然后以使其扩展为适合GUI底部的方式放置它们。 When spread out, the title has a much greater chance of being fully displayed (but not a guarantee, mind you!). 散布后,标题更有可能被完全显示(但请注意,这不是保证!)。

For example, if you make the main GUI use a BorderLayout , and add this GridLayout using JPanel into the BorderLayout.CENTER position, it will fill it completely. 例如,如果使主GUI使用BorderLayout ,并使用JPanel将此GridLayout添加到BorderLayout.CENTER位置,它将完全填充它。 Then the top components, the TextField s and JButton can be added to another JPanel , say one that uses a GridBagLayout , and add that to the main JPanel 's BorderLayout.PAGE_START position. 然后,可以将顶级组件TextFieldJButton添加到另一个JPanel ,例如使用GridBagLayoutJPanel ,并将其添加到主JPanelBorderLayout.PAGE_START位置。

For example, the following code produces this GUI: 例如,以下代码生成此GUI:

在此处输入图片说明

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class P3GUI2 extends JPanel {
    private static final int COLS = 20;
    private JTextField originalSort = new JTextField(COLS);
    private JTextField newSort = new JTextField(COLS);
    private JButton performSort = new JButton("Perform Sort");
    private JRadioButton ascending = new JRadioButton("Ascending");
    private JRadioButton descending = new JRadioButton("Descending");
    private ButtonGroup sort = new ButtonGroup();
    private JRadioButton integer = new JRadioButton("Integer");
    private JRadioButton fraction = new JRadioButton("Fraction");
    private ButtonGroup numType = new ButtonGroup();

    public P3GUI2() {
        JPanel topPanel = new JPanel(new GridBagLayout());
        topPanel.add(new JLabel("Original List:"), createGbc(0, 0));
        topPanel.add(originalSort, createGbc(1, 0));
        topPanel.add(new JLabel("Sorted List:"), createGbc(0, 1));
        topPanel.add(newSort, createGbc(1, 1));

        performSort.setMnemonic(KeyEvent.VK_P);
        JPanel btnPanel = new JPanel();
        btnPanel.add(performSort);

        JPanel sortOrderPanel = createTitlePanel("Sort Order");
        JPanel numericPanel = createTitlePanel("Numeric Type");

        sortOrderPanel.add(ascending);
        sortOrderPanel.add(descending);
        sort.add(ascending);
        sort.add(descending);

        numericPanel.add(integer);
        numericPanel.add(fraction);
        numType.add(integer);
        numType.add(fraction);

        JPanel radioPanels = new JPanel(new GridLayout(1, 0, 3, 3));
        radioPanels.add(sortOrderPanel);
        radioPanels.add(numericPanel);        

        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        setLayout(new BorderLayout(3, 3));
        add(topPanel, BorderLayout.PAGE_START);
        add(btnPanel, BorderLayout.CENTER);
        add(radioPanels, BorderLayout.PAGE_END);
    }

    private JPanel createTitlePanel(String title) {
        JPanel panel = new JPanel(new GridLayout(0, 1, 3, 3));
        panel.setBorder(BorderFactory.createTitledBorder(title));
        return panel;
    }

    private GridBagConstraints createGbc(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST;
        gbc.insets = new Insets(3, 3, 3, 3);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        return gbc;
    }

    private static void createAndShowGui() {
        P3GUI2 mainPanel = new P3GUI2();

        JFrame frame = new JFrame("Binary Search Tree Sort");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Or you could place the above btnPanel into the main one BorderLayout.CENTER and then place the radioPanels into the main one BorderLayout.PAGE_END . 或者,您可以将上面的btnPanel放在主要的BorderLayout.CENTER ,然后将radioPanels放在主要的BorderLayout.PAGE_END This will display a GUI of the same appearance but it will expand differently if re-sized. 这将显示具有相同外观的GUI,但是如果重新设置大小,它将以不同的方式扩展。

The preferred size of the panel (as determined by the layout manager) does not consider the size of the text in the TitledBorder so the title can get truncated. 面板的首选大小(由布局管理器确定)不考虑TitledBorder本的大小,因此标题可能会被截断。

Here is a custom JPanel that can be used with a TitleBorder. 这是可以与TitleBorder一起使用的自定义JPanel。 The getPreferredSize() method has been customized to use the maximum width of: 自定义getPreferredSize()方法以使用以下最大宽度:

  1. the default getPreferredSize() calculation 默认的getPreferredSize()计算
  2. the width of the text in the TitledBorder TitledBorder中文本的宽度

Here is a simple example: 这是一个简单的示例:

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();
            }
        });
*/
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM