JDialog vs JOptionPane vs JPanel正确调整屏幕大小

时间:2013-03-09 13:00:45

标签: java swing screen-resolution joptionpane jdialog

当我需要向用户显示一个带有保存或取消按钮的非常复杂的界面并且需要此界面来正确处理不同的显示器分辨率时,我遇到了无穷无尽的问题。 例如,假设这个接口需要在1280 x 768显示器中安装17个JTextFields和一个可调整大小的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根据垂直分辨率垂直调整自身的大小。

3 个答案:

答案 0 :(得分:3)

您可以向对话框添加选项窗格,如herehere所示。

另外,请在 setSize()之后致电pack()

附录:这是sscce的变体,它将选项窗格放在滚动窗格中,其初始大小基于屏幕几何图形,如here所示。

image

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

答案 1 :(得分: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);
    }
}

答案 2 :(得分:0)

感谢您的回答,到目前为止我提供了这个解决方案:获取显示器高度,如果小于1024,则在JscrollPane中显示一个小对话框(感谢trashgod指向它),否则显示标准的正常对话框身高(我不得不通过试验计算)

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