2

当我需要向用户展示一些带有保存或取消按钮的非常复杂的界面并且需要这个界面来正确处理不同的显示器分辨率时,我遇到了无穷无尽的问题。例如,假设这个接口需要在一个 1280 x 768 的显示器中容纳 17 个 JTextField 和一个可调整大小的 JTextArea(我的 13 英寸笔记本电脑的垂直尺寸为 760 像素)。

这是一个SSCCE:

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

public class OptionPanePanel extends JFrame
{
    private static Container layoutComponents(String title, float alignment)
    {
        JPanel container = new JPanel();
        BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
        container.setLayout(layout);

        for (int i = 0, n = 7; i < n; i++)
        {
            JTextField jtextField= new JTextField("jtextfield "+i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add( new javax.swing.Box.Filler(new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 20), 
                    new java.awt.Dimension(32767, 20)));
        }
        JTextArea jTextArea = new JTextArea(15, 30);
        container.add(jTextArea);
        for (int i = 6, n = 13; i < n; i++)
        {
            JTextField jtextField= new JTextField("jtextfield "+i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add( new javax.swing.Box.Filler(new java.awt.Dimension(0, 20), new java.awt.Dimension(0, 20), 
                    new java.awt.Dimension(32767, 20)));
        }
        return container;
    }

    public static void main(String args[])
    {
        Container panel1 = layoutComponents("Left", Component.LEFT_ALIGNMENT);
        JOptionPane.showConfirmDialog(
            null, panel1, "addRecord", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);  
    }
}

现在我希望上面的例子表现得这样:

  1. 窗口大小调整不裁剪任何内容
  2. 根据显示器的分辨率,窗口的大小会有所不同。
  3. 我不必静态指定 maximumSize、MinimumSize 和 preferredSize(例如使用 NetBeans GUI 编辑器),因此每次我必须进行大量测试才能找出正确的大小
  4. JtextArea 根据最大​​垂直分辨率垂直调整自身大小。
4

3 回答 3

3

您可以将选项窗格添加到对话框中,如此此处所示。

顺便说一句,调用setSize() after pack()

附录:这是您的sscce的一个变体,它将选项窗格放置在一个滚动窗格中,该窗格具有基于屏幕几何形状的初始大小,如此处所示

图片

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

public class OptionPanePanel extends JFrame {

    private static Container layoutComponents(String title, float alignment) {
        JPanel container = new JPanel();
        BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
        container.setLayout(layout);

        for (int i = 0, n = 7; i < n; i++) {
            JTextField jtextField = new JTextField("jtextfield " + i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add(createFiller());
        }
        JTextArea jTextArea = new JTextArea(15, 30);
        container.add(jTextArea);
        for (int i = 6, n = 13; i < n; i++) {
            JTextField jtextField = new JTextField("jtextfield " + i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add(createFiller());
        }
        return container;
    }

    private static Box.Filler createFiller() {
        return new Box.Filler(new Dimension(0, 20), new Dimension(0, 20),
            new Dimension(Short.MAX_VALUE, 20));
    }

    public static void main(String args[]) {
        Container panel = layoutComponents("Left", Component.LEFT_ALIGNMENT);
        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JScrollPane jsp = new JScrollPane(panel){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 2 * screenSize.height / 3);
            }
        };
        JOptionPane optPane = new JOptionPane();
        optPane.setMessage(jsp);
        optPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
        optPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
        JFrame f = new OptionPanePanel();
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.add(optPane);
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }
}
于 2013-03-09T13:47:59.323 回答
2

我怎样才能显示类似上面的内容,以便:

我不知道那是什么意思。发布您的 SSCCE,向我们准确展示您遇到的问题。

这对我来说很好:

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

public class OptionPanePanel extends JFrame
{
    public OptionPanePanel()
    {
        JPanel panel = new JPanel( new BorderLayout() );
        JPanel north = new JPanel();
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );
        north.add( new JTextField(10) );

        JTextArea textArea = new JTextArea(5, 20);

        panel.add(north, BorderLayout.NORTH);
        panel.add(new JScrollPane(textArea));

        int result = JOptionPane.showConfirmDialog(
            this, panel, "addRecord", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    }


    public static void main(String[] args)
    {
        JFrame frame = new OptionPanePanel();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}
于 2013-03-09T17:19:42.183 回答
0

谢谢你的回答,到目前为止,我提出了这个解决方案:获取监视器高度,如果小于 1024,则在 JscrollPane 中显示一个小对话框(感谢垃圾神指向它),否则显示一个标准高度的普通对话框(我不幸的是,必须通过试验来计算)

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

public class OptionPanePanel extends JFrame
{

    private static Container layoutComponents(String title, float alignment)
    {
        JPanel container = new JPanel();
        BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
        container.setLayout(layout);

        for (int i = 0, n = 7; i < n; i++)
        {
            JTextField jtextField = new JTextField("jtextfield " + i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add(createFiller());
        }
        JTextArea jTextArea = new JTextArea(15, 30);
        container.add(jTextArea);
        for (int i = 6, n = 13; i < n; i++)
        {
            JTextField jtextField = new JTextField("jtextfield " + i, n);
            jtextField.setAlignmentX(alignment);
            container.add(jtextField);
            container.add(createFiller());
        }
        return container;
    }

    private static Box.Filler createFiller()
    {
        return new Box.Filler(new Dimension(0, 20), new Dimension(0, 20),
                new Dimension(Short.MAX_VALUE, 20));
    }

    public static void main(String args[])
    {
        Container panel = layoutComponents("Left", Component.LEFT_ALIGNMENT);
        /*Let's check the monitor height in multi monitor setup */
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int monitorHeight = gd.getDisplayMode().getHeight();
        int result;
        if (monitorHeight <= 1024)
        {
            final Dimension preferredDimension = new Dimension(500, monitorHeight - 110);
            panel.setPreferredSize(preferredDimension);
            JScrollPane jsp = new JScrollPane(panel)
            {
                @Override
                public Dimension getPreferredSize()
                {
                    return new Dimension(500, 700);
                }
            };
            result = JOptionPane.showOptionDialog(null, jsp,
                    "",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    new String[]
                    {
                        ("save"), ("cancel")
                    }, // this is the array
                    "default");
        }
        else
        {
            final Dimension preferredDimension = new Dimension(500, 700);
            panel.setPreferredSize(preferredDimension);
            result = JOptionPane.showOptionDialog(null, panel,
                    "",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    new String[]
                    {
                        ("save"), ("cancel")
                    }, // this is the array
                    "default");
        }

        if (result == JOptionPane.OK_OPTION)
        {
            //do something
        }
    }
}
于 2013-03-12T12:52:52.820 回答