简体   繁体   English

调整JDialog框的大小

[英]resizing JDialog box

I'm having trouble with a dialog I created. 我在创建对话框时遇到问题。 It packs everything in cutting off border titles and input boxes. 它包含了切断边框标题和输入框的所有内容。 I've tried setting the size of the panel and of the components, but to no avail; 我已经尝试设置面板和组件的大小,但无济于事; size never changes. 大小永远不变。 Any help would be appreciated in being able to modify the dimensions of the dialog. 在修改对话框的尺寸方面,我们将不胜感激。

JTextField account = new JTextField(6);
account.setDocument(new JTextFieldLimit(6));
account.setBorder(new TitledBorder("account"));

String[] firstDigitList = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
JComboBox firstDigitCombo = new JComboBox(firstDigitList);
firstDigitCombo.setSelectedIndex(0);
firstDigitCombo.setBorder(new TitledBorder("Leading Digit Change"));

JPanel panel = new JPanel();
panel.add(account);
panel.add(firstDigitCombo);

int result = JOptionPane.showConfirmDialog(null, panel, "Please Enter Values", JOptionPane.OK_CANCEL_OPTION);

The basic problem is that a TitledBorder will not expand a component to the point where it will be big enough to display the entire text. 基本问题是TitledBorder不会将组件扩展到足以显示整个文本的位置。 Instead it will just truncate the text. 相反,它只会截断文本。

The solution is to ensure the components are big enough for the text to display. 解决方案是确保组件足够大,以便显示文本。 I have shown this here by expanding the size of the text field, and by adding a 'full length' label in placed of the 'shortened' title. 我在这里通过扩展文本字段的大小以及在“缩短的”标题的位置添加“全长”标签来展示这一点。

Gui的测试尺寸

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class TestSizeOfGui {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JTextField account = new JTextField(10);
                JPanel accountPanel = new JPanel(new GridLayout());
                accountPanel.add(account);
                accountPanel.setBorder(new TitledBorder("Account"));

                String[] firstDigitList = {
                    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

                JLabel firstDigitListLabel = new JLabel("Leading Digit Change");
                JPanel firstDigitListPanel = new JPanel(new BorderLayout(4,2));
                firstDigitListPanel.add(firstDigitListLabel, BorderLayout.WEST);
                JComboBox firstDigitCombo = new JComboBox(firstDigitList);
                firstDigitListPanel.add(firstDigitCombo);
                firstDigitCombo.setSelectedIndex(0);
                firstDigitListPanel.setBorder(new TitledBorder("LDC"));

                JPanel panel = new JPanel();
                panel.add(accountPanel);
                panel.add(firstDigitListPanel);

                int result = JOptionPane.showConfirmDialog(
                    null,
                    panel,
                    "Please Enter Values",
                    JOptionPane.OK_CANCEL_OPTION);

                }
            });
    }
}

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

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