在按钮单击按钮上创建时,在Jframe中看不可见



on按钮单击,我正在创建新的jframe,在其中添加jbutton并将其设置为可见。jframe是可见的,但jbutton不可见。

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

以下是按钮代码

@Override
public void actionPerformed(ActionEvent arg0) {
  popup.showPopup();
}

我已经用表演弹出方法完成了一个名为"弹出式"的课程。

以下是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

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

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();
            }
        }
    });
    }
}

最新更新