User input causes frame.getContentPane.removeAll() to stop working

眉间皱痕 提交于 2019-12-10 22:39:09

问题


Within a JFrame , i am replacing a Jpanel with another JPanel .

package testing;

import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class Testing extends JPanel {

    JLabel jl;
    ImageIcon icon;
    Point pointer;


    public static void main(String[] args) {

       JFrame jf = new JFrame();

       JPanel jp1 = new JPanel();
       JPanel jp2 = new JPanel();

       JLabel jl1 = new JLabel("Hey1");
       JLabel jl2 = new JLabel("Hey2");

       jp1.add(jl1);
       jp2.add(jl2);

       jf.add(jp1);

       jf.setVisible(true);
       jf.pack();

       Scanner myScanner= new Scanner(System.in);

       int x = myScanner.nextInt(); // the line causes the code to not work , 

                                    //    what is happening

       jf.getContentPane().removeAll();

       jf.add(jp2);


    }
}

The weird part is that the code stops working the moment i try to read user input

int x = myScanner.nextInt();

The code : jf.getContentPane().removeAll(); stops working and i cant remove the current JPanel and add in the new JPanel

i need to read in the user input before the JPanel is replaced , how do i resolve this issue??

note : Even after i type in something , the jf.getContentPane().removeAll() still doesnt work


回答1:


Use validate() to layout the container's subcomponents. Also pack() the Window before setVisible().

Alternatively, use CardLayout to change the view and JTextField to collect user input.

As tested:

import java.util.*;
import java.awt.*;
import javax.swing.*;

public class Testing extends JPanel {

    JLabel jl;
    ImageIcon icon;
    Point pointer;

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JLabel jl1 = new JLabel("Hey1");
        JLabel jl2 = new JLabel("Hey2");
        jp1.add(jl1);
        jp2.add(jl2);
        jf.add(jp1);
        jf.pack();
        jf.setVisible(true);
        Scanner myScanner = new Scanner(System.in);
        int x = myScanner.nextInt(); // the line causes the code to not work , 
        jf.getContentPane().removeAll();
        jf.add(jp2);
        jf.validate();
    }
}



回答2:


The problem that is not that the program is "stop working" at this line:

Scanner myScanner = new Scanner(System.in);

Of course this line needs that you insert some integer value in the console, but this works perfectly.

Actually your program does NOT stop to work: it simply executes all the expressions and then wait for ever, it does not end because there is the GUI thread that loops for ever.

The point is that that the methods that you call are not correct in order to obtain the desired result, please check my code (modified in the last lines) if this can suite your requirments:

public class Testing extends JPanel {

    JLabel jl;
    ImageIcon icon;
    Point pointer;

    public static void main(String[] args) {

        JFrame jf = new JFrame();

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();

        JLabel jl1 = new JLabel("Hey1");
        JLabel jl2 = new JLabel("Hey2");

        jp1.add(jl1);
        jp2.add(jl2);

        jf.add(jp1);

        jf.setVisible(true);
        jf.pack();

        Scanner myScanner = new Scanner(System.in);

        int x = myScanner.nextInt(); // the line causes the code to not work , 

                                    //    what is happening
        jf.setContentPane(jp2);
        jf.pack();
    }
}

I hope this will help you.



来源:https://stackoverflow.com/questions/23150971/user-input-causes-frame-getcontentpane-removeall-to-stop-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!