开发者

Opening a static form/jFrame on clicking on button

开发者 https://www.devze.com 2023-02-14 08:33 出处:网络
I have two JFrames. The first one is defined as public firstJframe and the second one is defined as 开发者_StackOverflow社区public static final jFrame. I want to open the second JFrame on clicking a b

I have two JFrames. The first one is defined as public firstJframe and the second one is defined as 开发者_StackOverflow社区public static final jFrame. I want to open the second JFrame on clicking a button on the first JFrame. How can I do this?

.setVisible does not work for this. I really don't know how to proceed with this.


Try calling revalidate() for the object you want to update(in your case the second frame).

Example:

  JButton myButton = new JButton("Open new window");
  JFrame newFrame = new JFrame("New Window");

   myButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
   newFrame.pack();
   newFrame.setVisible(true);
   newFrame.revalidate();
   }
   });

Update

If that does not work, try calling this:

newFrame.invalidate();
newFrame.validate();


I propose: under the button type:

this.dispose

new public static final jFrame.setvisible(true);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号