oli*_*olz 5 java swing modal-dialog jframe
我有一个主框架和一个第二帧和一个模式对话框,主框架作为父框架.
但现在两个帧都被模态对话框阻挡了.
当主框架有模态对话框时,如何让第二帧可以访问?
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 );
}
} );
}
}
Run Code Online (Sandbox Code Playgroud)
你需要设置模态字段来记录.
JDialog d = new JDialog( Example.this, "Dialog" ,Dialog.ModalityType.DOCUMENT_MODAL);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
894 次 |
| 最近记录: |