User input causes frame.getContentPane.removeAll()

2019-07-09 05:34发布

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

2条回答
我想做一个坏孩纸
2楼-- · 2019-07-09 06:08

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();
    }
}
查看更多
The star\"
3楼-- · 2019-07-09 06:17

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.

查看更多
登录 后发表回答