这是我在这里的第一篇文章,所以请原谅我是否有一些格式化错误并告诉我该怎么做。
我对使用GridBagLayout进行编码有点新意,所以我已经设法理解它的基本概念。有时会发生格式化完全消失的情况。
怎么样,一个很好的TextPane:
有时怎么样,TP真的很小:
触发此问题的唯一方法是从JButton调用窗口。我粘贴代码在一堆位置调用窗口(Error.showDialog(new Exception());
),似乎只要ActionListener调用它就会发生。
这是我打开错误消息的对话框。
public static void showDialog(Exception myException) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
myException.printStackTrace(ps);
try {
ErrorString = os.toString("UTF8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JFrame window = new JFrame("Error");
window.setSize(550, 600);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
c.gridy = 0;
tpError = new JTextPane();
tpError.setPreferredSize(new Dimension(500, 500));
tpError.setEditable(false);
tpError.setText(ErrorString);
JScrollPane scrollPane = new JScrollPane(tpError, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scrollPane, c);
c.gridy++;
JPanel panelBtns = new JPanel(new FlowLayout());
JButton btnClose = new JButton("Schließen");
btnClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
window.setVisible(false);
window.dispose();
}
});
panelBtns.add(btnClose);
JButton btnSend = new JButton("Bericht");
btnSend.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent AE) {
try {
Desktop.getDesktop().browse(new URI("mailto:[email protected]?subject=Fehler%20im%20Converter"));
} catch (IOException | URISyntaxException ex) {
ex.printStackTrace();
}
}
});
panelBtns.add(btnSend);
panel.add(panelBtns,c );
window.add(panel);
window.setVisible(true);
window.repaint();
}
如图所示,格式有时会重置。我之前多次遇到过这个问题,但我忽略了它。这次重要的是它能够满足我的需求。
tpError.setPreferredSize(new Dimension(500, 500));
不要硬编码首选尺寸。
当您使用GridBagLayout时,如果没有足够的空间以其首选大小显示组件,则组件将以其“最小大小”显示。相反,您需要使用其他约束(如“填充”约束)来控制组件随可用空间的变化而增长/缩小的方式。
但是,更好的解决方案是不使用GridBagLayout。而只是使用框架的默认layot,这是一个BorderLayout
。那么基本代码将是:
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane( textPane );
add(scrollPane, BorderLayout.CENTER);
add(panelButtons, BorderLayout.PAGE_END);
当您不必担心更复杂的约束时,会更简单。
您忘记为文本窗格设置weightx和weighty属性。
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1; // set weightx/y
c.weighty = 1;
c.gridy = 0;
JTextPane tpError = new JTextPane();
tpError.setEditable(false);
try {
tpError.setText(os.toString("UTF8"));
} catch (Exception e) {
e.printStackTrace();
}
JScrollPane scrollPane = new JScrollPane(tpError, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scrollPane, c);
c.gridy++;
c.weightx = 0; // clean weightx/y
c.weighty = 0;
JPanel panelBtns = new JPanel(new FlowLayout());
另一种可能性是将首选大小设置为滚动窗格(而不是JTextPane)
JTextPane tpError = new JTextPane();
tpError.setEditable(false);
try {
tpError.setText(os.toString("UTF8"));
} catch (Exception e) {
e.printStackTrace();
}
JScrollPane scrollPane = new JScrollPane(tpError, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(500, 500));
panel.add(scrollPane, c);