【问题标题】:JButton does not appear on JDialogJButton 没有出现在 JDialog 上
【发布时间】:2014-07-11 06:41:59
【问题描述】:

我在这段代码中有一个问题,我在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之前尝试添加它

标签: java swing jframe jbutton


【解决方案1】:

在调用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);
    }

}

【讨论】:

    【解决方案2】:

    我相信您的Component 是在您将按钮添加到它之前呈现的。在渲染 Component 之前尝试添加按钮。尝试在致电 setVisiblerepaint 您的 Component 之前添加它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多