Java 将 JPanel 装入模态 JDialog
Java Fitting JPanel into Modal JDialog
我有一个 class 那个 returns 一个 JPanel:
public static JPanel program(String csvName) {
JPanel f = new JPanel();
try {
String path = System.getProperty("user.dir");
String datafile = path+"/files/logic/"+csvName+".csv";
FileReader fin = new FileReader(datafile);
DefaultTableModel m = createTableModel(fin, null);
JTable table = new JTable(m);
JScrollPane stable = new JScrollPane (table);
stable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
stable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
f.add(stable);
f.setMinimumSize(new Dimension(900,500));
JFrame desktopFrame = new JFrame();
desktopFrame.add(f);
desktopFrame.setSize(900, 500);
desktopFrame.setVisible(true);
toExcel(m, new File(path+"/files/logic/"+csvName+".csv"));
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
这就是用于模态显示 JPanel 的内容。
String csv = "war";
JPanel f = T1Data.program(csv);
JDialog desktopFrame = new JDialog();
desktopFrame.add(f);
desktopFrame.setModal(true);
desktopFrame.setSize(900, 500);
desktopFrame.setVisible(true);
但是我得到的结果是 JPanel 居中并且不适合 JDialog。
看起来像这样:
http://gyazo.com/4bc360e7d2c7cf7117a95d748d520838.png
我该如何解决这个问题?
要使面板适合对话框的大小,您可以更改对话框的 LayoutManager
,或者,因为这显然应该是添加到对话框中的唯一面板,只需将面板设置为内容窗格(desktopFrame.setContentPane(f)
而不是 desktopFrame.add(f)
)。
JPanel
使用的是 FlowLayout
,如果您将其更改为 BorderLayout
,滚动面板的布局将填满整个容器。
您还应该考虑使用 JDialog#pack
而不是 setSize
我有一个 class 那个 returns 一个 JPanel:
public static JPanel program(String csvName) {
JPanel f = new JPanel();
try {
String path = System.getProperty("user.dir");
String datafile = path+"/files/logic/"+csvName+".csv";
FileReader fin = new FileReader(datafile);
DefaultTableModel m = createTableModel(fin, null);
JTable table = new JTable(m);
JScrollPane stable = new JScrollPane (table);
stable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
stable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
f.add(stable);
f.setMinimumSize(new Dimension(900,500));
JFrame desktopFrame = new JFrame();
desktopFrame.add(f);
desktopFrame.setSize(900, 500);
desktopFrame.setVisible(true);
toExcel(m, new File(path+"/files/logic/"+csvName+".csv"));
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
这就是用于模态显示 JPanel 的内容。
String csv = "war";
JPanel f = T1Data.program(csv);
JDialog desktopFrame = new JDialog();
desktopFrame.add(f);
desktopFrame.setModal(true);
desktopFrame.setSize(900, 500);
desktopFrame.setVisible(true);
但是我得到的结果是 JPanel 居中并且不适合 JDialog。
看起来像这样: http://gyazo.com/4bc360e7d2c7cf7117a95d748d520838.png
我该如何解决这个问题?
要使面板适合对话框的大小,您可以更改对话框的 LayoutManager
,或者,因为这显然应该是添加到对话框中的唯一面板,只需将面板设置为内容窗格(desktopFrame.setContentPane(f)
而不是 desktopFrame.add(f)
)。
JPanel
使用的是 FlowLayout
,如果您将其更改为 BorderLayout
,滚动面板的布局将填满整个容器。
您还应该考虑使用 JDialog#pack
而不是 setSize