【问题标题】:items are not visible in JFrame when created on button click在单击按钮时创建的项目在 JFrame 中不可见
【发布时间】:2019-12-09 05:44:55
【问题描述】:

单击按钮时,我正在创建新的 JFrame,在其中添加一个 JButton 并将其设置为可见。 JFrame 可见,但 JButton 不可见。

我尝试在 stackoverflow 上找到答案,但每个人都说在添加组件后将 JFrame 设置为可见。我也这样做了,但问题仍然没有解决

下面是按钮代码

@Override
public void actionPerformed(ActionEvent arg0) {

  popup.showPopup();

}

我用 show popup 方法创建了一个名为“popup”的类。

下面是popup.java类的代码

package justin;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class popupFrame {
    private JFrame f = new JFrame("Please wait...");

    public void showPopup() {
        System.out.println("Showing Popup");

        f.setSize(300, 150);

        f.setLayout(new FlowLayout());

        f.add(new JButton("Test"));

        f.setVisible(true);
    }
}

它应该显示 JFrame 并在单击按钮时在其中添加项目。

请查看以下链接以获取我的完整代码:

https://github.com/jamesfdz/Justin-code

【问题讨论】:

标签: java eclipse swing awt


【解决方案1】:

由于我看不到您的其余代码,因此我发布了一个示例。

public class ControlInterface{

    public ControlInterface() {
        JFrame frame = new JFrame();
        frame.setSize(400, 400);
        frame.add(new JButton("Test"));
        frame.setVisible(true);
    }
}

以及调用类:

public class BusinessLogic{

    public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    JButton button = new JButton("Popup");
    frame.add(button);
    frame.setVisible(true);

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == button) {
                new ControlInterface();
            }
        }
    });
    }
}

【讨论】:

  • 我试过了,但它不起作用。我会把完整的代码上传到 GitHub 上分享给大家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 2011-10-10
相关资源
最近更新 更多