Swing中Frame窗口显示dialog窗口再弹出对话框,背景为白色不显示组件解决办法

来源:互联网 发布:aso如何优化 编辑:程序博客网 时间:2024/05/16 13:53

将Frame上显示Dialog构造函数的Frame 变为JDialog或者Dialog

public BigDialog(JDialog parent){...}

package com.test.gui;import java.awt.BorderLayout;import java.awt.Frame;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.JLabel;import javax.swing.JPanel;class C extends JDialog{public C(Frame parent, boolean modal, String title, String message){super(parent, modal);    initComponents();    //Container container=getContentPane();    //container.setBackground(Color.green);    this.setModal(modal);    //this.getRootPane().setDefaultButton(btnContinue);    setAlwaysOnTop(true);        this.setTitle(title);    } private void initComponents() {        add(new JLabel("C - initComponents"), BorderLayout.CENTER);JPanel panel = new JPanel();JButton ok = new JButton("Okay");panel.add(ok);add(panel, BorderLayout.SOUTH);setSize(300, 300);setAlwaysOnTop(true);java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();        setBounds((screenSize.width-373)/2, (screenSize.height-204)/2, 373, 204); }}class BigDialog extends JDialog{public BigDialog(JDialog parent){super(parent, "About Dialog Test", true);add(new JLabel("--------BigDialog - 2---------"), BorderLayout.CENTER);JPanel panel = new JPanel();JButton ok = new JButton("Okay");ok.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {//setVisible(false);C obj = new C(null, true, "C", "34");obj.setVisible(true);}});panel.add(ok);add(panel, BorderLayout.SOUTH);setSize(1300, 800);setAlwaysOnTop(true);}}public class TestDialog extends JDialog{public TestDialog(JFrame owner){super(owner, "About Dialog Test", true);add(new JLabel("-------main dialog - 1----------"), BorderLayout.CENTER);JPanel panel = new JPanel();JButton ok = new JButton("Okay");ok.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {//setVisible(false);BigDialog obj = new BigDialog(null);obj.setVisible(true);}});panel.add(ok);add(panel, BorderLayout.SOUTH);setSize(500, 300);}public static void main(String[] args) {TestDialog obj = new TestDialog(null);obj.setVisible(true);}}


原创粉丝点击