下面的示例程序显示了一个带有两个按钮的框架。按下使用的第二个按钮MigLayout
会导致异常。使用的第一个按钮FlowLayout
工作得很好。似乎是一个错误MigLayout
?
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
public class Main extends JPanel {
static private JFrame frame = new JFrame("Test MigLayout");
public Main() {
JButton flowLayoutButton = new JButton("FlowLayout");
JButton miglayoutButton = new JButton("MigLayout");
add(flowLayoutButton);
add(miglayoutButton);
flowLayoutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEventIn) {
showDialog(false);
}
});
miglayoutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEventIn) {
showDialog(true);
}
});
}
public static void main(String[] args) {
Main main = new Main();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(main);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.pack();
frame.setVisible(true);
frame.setBounds(200, 200, 200, 200);
}
});
}
private void showDialog(boolean useMidgLayoutIn) {
JPanel panel = new JPanel(useMidgLayoutIn ? new MigLayout() : new FlowLayout());
JTextArea topTextArea = new JTextArea("Here is some junk text to fill up the Text Area.");
panel.add(topTextArea);
panel.add(new JButton("Button"));
JOptionPane optionPane = new JOptionPane(panel, JOptionPane.ERROR_MESSAGE);
JDialog dialog = optionPane.createDialog(frame, "Application Error");
dialog.setResizable(true);
dialog.pack();
dialog.setVisible(true);
}
}