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);
}
}
现在我想上面的例子来的表现,使:
- 窗口的大小调整大小不裁切任何内容
- 的基于显示器的分辨率,窗口的大小以某种方式处理不同。
- 我不''必须静态指定maximumSize,MinimumSize和preferredSize(例如NetBeans GUI编辑器),以便每次我必须进行大量测试以找出正确的大小时,JtextArea会自动垂直调整其自身的大小垂直分辨率达到最大。
谢谢你的回答,到目前为止,我想出了这个解决方案:让显示器高度,如果小于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
}
}
}
另外,拨打setSize()
后pack()
。
附录:下面是sscce的一个变体,它将选项窗格置于具有基于屏幕几何图形的初始大小的滚动窗格中,如here所示。
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);
}
}
我该怎么让像上述让:
我不知道这意味着什么。张贴你的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);
}
}
我添加了一个具有特定问题的SSCCE,你的SSCCE横向排列所有的JTextFields,而问题是显示器的垂直空间... – dendini 2013-03-11 09:47:01
*“不过,我可以叫包()和的setSize经过一番尝试找到正确的尺寸。 “*目标是在每台计算机上”尝试“这个计划上?因为一个数字不一定适用于另一个屏幕/ PLAF/OS ... – 2013-03-09 13:14:22
如果这里的答案不是'JScrollPane',那么我不明白这个问题(这也是一个强大的可能性)。 – 2013-03-09 13:15:52
目标是让Java根据屏幕分辨率为窗口计算正确的大小,并且可能是我指定的最小大小。 JScrollPane对于解决方案来说太极端了,因为我不想隐藏元素。 – dendini 2013-03-09 13:24:31