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

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

问题描述:

当我需要向用户显示一些非常复杂的界面时,保存或取消按钮并需要此界面正确处理不同的显示器分辨率时,我遇到了无穷无尽的问题。 假设这个接口需要适合17个JTextFields和一个1280 x 768显示器(我13英寸笔记本电脑的垂直尺寸为760像素)的可调整大小的JTextArea。JDialog vs JOptionPane vs JPanel正确的屏幕调整大小

这里是一个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编辑器),以便每次我必须进行大量测试以找出正确的大小时,JtextArea会自动垂直调整其自身的大小垂直分辨率达到最大。
+0

*“不过,我可以叫包()和的setSize经过一番尝试找到正确的尺寸。 “*目标是在每台计算机上”尝试“这个计划上?因为一个数字不一定适用于另一个屏幕/ PLAF/OS ... – 2013-03-09 13:14:22

+2

如果这里的答案不是'JScrollPane',那么我不明白这个问题(这也是一个强大的可能性)。 – 2013-03-09 13:15:52

+0

目标是让Java根据屏幕分辨率为窗口计算正确的大小,并且可能是我指定的最小大小。 JScrollPane对于解决方案来说太极端了,因为我不想隐藏元素。 – dendini 2013-03-09 13:24:31

谢谢你的回答,到目前为止,我想出了这个解决方案:让显示器高度,如果小于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 
     } 
    } 
} 

您可以将选项窗格添加到对话框,如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); 
    } 
} 
+0

这是我的另一种选择,但它似乎没有比我的两个建议的例子更好地处理大小.. – dendini 2013-03-09 13:52:38

+0

然后,我也不明白这个问题。通常的问题是屏幕溢出的选项窗格,为此可能需要[滚动窗格](http://stackoverflow.com/a/14011536/230513)。 – trashgod 2013-03-09 13:57:15

+0

我用SSCCE编辑我的问题,因为我意识到它太泛化了。 – dendini 2013-03-11 09:45:41

我该怎么让像上述让:

我不知道这意味着什么。张贴你的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); 
    } 
} 
+0

我添加了一个具有特定问题的SSCCE,你的SSCCE横向排列所有的JTextFields,而问题是显示器的垂直空间... – dendini 2013-03-11 09:47:01