5
Tôi có khung chính và khung thứ hai và hộp thoại phương thức với khung chính làm cha mẹ.Cách để Hộp thoại Modal không chặn Khung hình cấp cao thứ hai của tôi
Nhưng bây giờ cả hai khung bị chặn bởi hộp thoại phương thức.
Làm cách nào để cho khung thứ hai có thể truy cập được trong khi khung chính có hộp thoại phương thức?
public class Example extends JFrame {
public Example() {
super("MainFrame");
JButton btn1 = new JButton(new AbstractAction("Frame") {
@Override
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Frame");
f.getContentPane().add(new JLabel("This shoud be not blocked by ModalDialog."));
f.setLocation(50, 200);
f.setSize(300, 200);
f.setVisible(true);
}
});
}
});
JButton btn2 = new JButton(new AbstractAction("Modal") {
@Override
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JDialog d = new JDialog(Example.this, "Dialog");
d.getContentPane().add(new JLabel("This shoud block only MainFrame."));
d.setModal(true);
d.setLocation(50, 100);
d.setSize(300, 200);
d.setVisible(true);
}
});
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(btn1, BorderLayout.NORTH);
getContentPane().add(new JLabel("MainFrame"), BorderLayout.CENTER);
getContentPane().add(btn2, BorderLayout.SOUTH);
setLocation(50, 50);
setSize(200, 150);
btn1.doClick();
btn2.doClick();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example().setVisible(true);
}
});
}
}
+1, xem thêm [Phương thức AWT] (http://docs.oracle.com/javase/7/docs/api/java/awt/doc-files/Modality.html) – aterai