Java JDialog上不显示JButton
我在这段代码中遇到了一个问题,即,我在Java JDialog上不显示JButton,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,我在这段代码中遇到了一个问题,即,我在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; publi
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);
}
}
我相信您的
组件
在添加按钮之前已呈现。在渲染组件之前,请尝试添加按钮。在调用setVisible
或repaint
您的组件之前尝试添加它
在调用setVisible