简体   繁体   English

打开新窗口Jframe Java

[英]Open new window Jframe Java

I have a jframe with one textfield and one button. 我有一个带有一个文本框和一个按钮的jframe。 When I tap on button I create a websocket and I receive datas from it. 当我点击按钮时,我创建了一个网络套接字,并从中接收数据。 When data arrives I want to hide this Jframe and show another Jframe. 当数据到达时,我想隐藏这个Jframe并显示另一个Jframe。 For this reason I do in this way: 因此,我这样做:

This is the first JFrame 这是第一个JFrame

public class PrimaSchermata {

    private JFrame mainFrame;
    private JPanel controlPanel;
    private Session sessione;
    private JTextField userText;


    public PrimaSchermata(){

          mainFrame = new JFrame("First Jframe");
          mainFrame.setSize(500,200);
          mainFrame.setLayout(new GridLayout(3, 1));
          mainFrame.setResizable(false);

          controlPanel = new JPanel();
          controlPanel.setLayout(new GridBagLayout());

          this.showTextField();
          mainFrame.add(controlPanel);

          mainFrame.setVisible(true); 
    }
    private void showTextField(){

          JLabel  namelabel= new JLabel("Email: ", JLabel.RIGHT);

          this.userText = new JTextField(15);
          JButton loginButton = new JButton("Login");
          loginButton.addActionListener(new ActionListener() {

              public void actionPerformed(ActionEvent e) {     

                  PrimaSchermata.this.loginEvent(userText);

                }
          }); 


          controlPanel.add(namelabel);
          controlPanel.add(userText);

          controlPanel.add(loginButton);
          mainFrame.setVisible(true);  
       }
private void loginEvent(JTextField tokenText){

           if(tokenText.getText().length() == 0){

               JOptionPane.showMessageDialog(mainFrame, "Inserisci email", "", JOptionPane.ERROR_MESSAGE, null);
               return;

           }

           this.gestioneWebSocket();

       }
      private void gestioneWebSocket(){


            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            URI apiUri = URI.create("wss://websocketurltest");
            try {
                Session session = container.connectToServer(PrimaSchermata.class, apiUri);

            } catch (DeploymentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
     }

     @OnOpen
        public void onOpen(Session session) throws java.io.IOException
        {

            System.out.println("websocket opened");
            session.getBasicRemote().sendText("logintest");

        }
       @OnMessage
        public void onMessage(String message)
        {
            System.out.println("Message received: " + message);

            if(message.length() > 0){

                System.out.println("login ok");
                this.mainFrame.setVisible(false);
                secondFrame x = new secondFrame();
                this.mainFrame.dispose();
            }

         }

This is the second JFrame: 这是第二个JFrame:

public class secondFrame extends JFrame{

    public secondFrame() {

          setBounds(100, 200, 120, 120);
          setTitle("Second JFrame");
          setVisible(true);
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}

My problem is this: 我的问题是这样的:

when I tap on button and I receive data from websocket the second JFrame shows but the first JFrame doesnt disappear despite I set visible = false in the first JFrame. 当我点击按钮并从websocket接收数据时,第二个JFrame会显示,但第一个JFrame不会消失,尽管我在第一个JFrame中设置了visible = false。

Where I wrong? 我哪里错了? Or exist another way to achive this? 还是存在另一种实现这一目标的方法?

Why not just reuse the same JFrame and just reset the view with the setContentPane(JComponent ). 为什么不重用相同的JFrame并使用setContentPane(JComponent)重置视图。 Do you necessarily need to have multiple JFrames? 您是否需要多个JFrame?

Try using mainFrame.dispose(); 尝试使用mainFrame.dispose(); instead of setting visible to false , and do this before you open the second window or send the request. 而不是将visible设置为false ,并打开第二个窗口或发送请求之前执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM