[英]items are not visible in JFrame when created on button click
On button click, I am creating new JFrame, adding a JButton inside it and setting it visible.单击按钮时,我正在创建新的 JFrame,在其中添加一个 JButton 并将其设置为可见。 JFrame is visible but the JButton is not visible.
JFrame 可见,但 JButton 不可见。
I tried finding answers on stackoverflow but everyone says to set the JFrame visible after adding the components.我尝试在 stackoverflow 上找到答案,但每个人都说在添加组件后将 JFrame 设置为可见。 I did that also but still, the issue is not solved
我也这样做了,但问题仍然没有解决
below is button code下面是按钮代码
@Override
public void actionPerformed(ActionEvent arg0) {
popup.showPopup();
}
I have made a class named "popup" with a show popup method.我用 show popup 方法创建了一个名为“popup”的类。
Below is the code of popup.java class下面是 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);
}
}
It should show the JFrame with items added in it on the click of button.单击按钮时,它应该显示 JFrame,其中添加了项目。
Please check the below link for my complete code:请查看以下链接以获取我的完整代码:
https://github.com/jamesfdz/Justin-code https://github.com/jamesfdz/Justin-code
Since I can't see the rest of your code, I have posted an example instead.由于我看不到您的其余代码,因此我发布了一个示例。
public class ControlInterface{
public ControlInterface() {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.add(new JButton("Test"));
frame.setVisible(true);
}
}
And the calling class:和调用类:
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();
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.