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

将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(){
			@Override
			public 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(){
			@Override
			public 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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承JOptionPane类,重写createDialog()方法,在该方法添加自定义的小键盘组件,然后在调用JOptionPane.showInputDialog()时传入自定义的JOptionPane子类对象即可。 以下是一个简单的示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CustomInputDialog extends JOptionPane { private JPanel keyboardPanel; private JTextField inputField; public CustomInputDialog(Component parentComponent, Object message, String title, int messageType) { super(message, messageType); this.inputField = new JTextField(); this.keyboardPanel = createKeyboardPanel(); setInputValue(inputField); setMessage(new Object[] {message, inputField}); JDialog dialog = createDialog(parentComponent, title); dialog.setContentPane(keyboardPanel); dialog.pack(); dialog.setVisible(true); } private JPanel createKeyboardPanel() { JPanel panel = new JPanel(new BorderLayout()); // 添加自定义的小键盘组件 KeyboardPanel keyboard = new KeyboardPanel(); keyboard.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { inputField.setText(inputField.getText() + e.getActionCommand()); } }); panel.add(keyboard, BorderLayout.CENTER); return panel; } public static String showCustomInputDialog(Component parentComponent, Object message, String title, int messageType) { CustomInputDialog pane = new CustomInputDialog(parentComponent, message, title, messageType); pane.setWantsInput(true); pane.selectInitialValue(); return (String) pane.getInputValue(); } public static void main(String[] args) { String input = showCustomInputDialog(null, "Please input something:", "Input", JOptionPane.PLAIN_MESSAGE); System.out.println("Input value: " + input); } } ``` 其,KeyboardPanel是一个自定义的小键盘面板,可以根据具体需求进行实现。在showCustomInputDialog()方法,创建自定义的CustomInputDialog对象,并将其作为参数传入到JOptionPane.showInputDialog()方法。最后,通过CustomInputDialog.getInputValue()方法获取用户输入的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值