[英]JButton does not appear on JDialog
我在此代码中存在问题,即我在JDialog
上添加了JButton
,但是当出现对话框时按钮不可见。 请帮忙。
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;
public class A implements ActionListener {
JFrame f = new JFrame();
public A() {
JButton b = new JButton("JDialog");
f.add(b);
b.addActionListener(this);
f.setVisible(true);
f.setSize(500,500);
}
public static void main(String arg[]) {
new A();
}
public void actionPerformed(ActionEvent e) {
JDialog d = new JDialog(f,"Dialog",true);
d.setSize(100,100);
d.setVisible(true);
d.setLayout(new FlowLayout());
JButton b = new JButton("OK");
d.add(b);
}
}
在调用setvisible(true)
之前添加按钮。
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;
public class A implements ActionListener {
JFrame f = new JFrame();
public A() {
JButton b = new JButton("JDialog");
f.add(b);
b.addActionListener(this);
f.setVisible(true);
f.setSize(500,500);
}
public static void main(String arg[]) {
new A();
}
public void actionPerformed(ActionEvent e) {
JDialog d = new JDialog(f,"Dialog",true);
d.setSize(100,100);
d.setLayout(new FlowLayout());
JButton b = new JButton("OK");
d.add(b);
d.setVisible(true);
}
}
我相信您的Component
已渲染,然后再向其中添加按钮。 在呈现Component
之前,尝试添加按钮。 在调用setVisible
或repaint
Component
之前,尝试添加它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.