I have the following code to run a simple number guessing program on the click of a button:public class test3 implements ActionListener {public void create(){ Dimension s=new Dimension(400,400); JFrame l=new JFrame(); l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); l.setSize(s); l.setResizable(true); Dimension s1=new Dimension(400,200); JPanel me=new JPanel(); JPanel ne=new JPanel(); JLabel kingsman=new JLabel ("GUESS THE KING'S NUMBER!"); kingsman.setFont(new Font("Serif", Font.BOLD, 45)); JPanel common=new JPanel(); BoxLayout n1=new BoxLayout(common,1); common.setLayout(n1); JButton p=new JButton("START"); p.setFont(new Font("Serif", Font.BOLD, 40)); p.setPreferredSize(s1); //l.add(p); me.add(kingsman); ne.add(p); common.add(me); common.add(ne); l.add(common); // l.pack(); l.setVisible(true); p.addActionListener(this);} public void actionPerformed(ActionEvent e) { System.out.println("Welcome to Guess the number Game"); System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10"); gamer2 game=new gamer2(); game.generatenum(); }public static void main(String args[]){ test3 ob=new test3(); ob.create();}}But on clicking the button,the JFrame deteriorates:The JFrame also refuses to close when the button is clicked even though setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is mentioned.But it closes if I don't click the button.generatenum function in class gamer2:void generatenum() { int ran=(int)(Math.random()*10); System.out.println("For developer-no.selected "+ran); gamer3 ob1=new gamer3(); //gamer3 takes user input ob1.getUserInput(ran); }Can someone explain me what is actually going wrong?I have been trying to crack it but nothing has helped me so far.The event handling part is working perfectly.On clicking the button,generatenum() runs successfully.Th rest of the programs works on the Eclipse console.I understand that JFrame does not close till the complete program gets executed,but in this case,the rest of the program is not dependent on JFrame.Is it refusing to close because the object of gamer2 is created within the action handling method which exists only if JFrame exists?The JFrame is only changing it's looks when I resize it.Resizing it destroys the looks but this doesn't occur if the button is not yet clicked.Class gamer3 (accepting user input):public class gamer3 { int getIt; int gotValue; static int i=0; gamer1 l=new gamer1(); static gamer3 g=new gamer3(); void getUserInput(int k) { i++; System.out.println("print now-Chance "+i); g.gotValue=k; InputStreamReader j=new InputStreamReader(System.in); BufferedReader s=new BufferedReader(j); try { int getIt1=Integer.parseInt(s.readLine()); g.getIt=getIt1; } catch (IOException e) { e.printStackTrace(); } l.checker(g.getIt,g.gotValue ); }Class gamer1:static int trial=0; int result=0; int value=0; String d; public void checker(int get,int craze) { value=get; result=craze; ++trial; if(value==result) { System.out.println("Successful"); } else { System.out.println("wrong entry"); if(trial<=3) { int c=3; int rem=c-trial; System.out.println("You have " +rem+ " chance(s) left!"); if(trial<3) { System.out.println("Enter y to play again or n to exit!"); System.out.println("D you want to retry?_"); InputStreamReader k =new InputStreamReader(System.in); BufferedReader read=new BufferedReader(k); try { d= read.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(d.equals("y")) { d="null"; gamer3 mele=new gamer3(); mele.getUserInput(result); } } } else { System.out.println("you are out of the game.No chances left!"); } } } }Class gamer2:package test;public class gamer2 {int getIt;//complete generation of the goddamn random numbervoid generatenum(){ int ran=(int)(Math.random()*10); System.out.println("For developer-no.selected "+ran); gamer3 ob1=new gamer3(); ob1.getUserInput(ran);}public static void main(String[] args) {System.out.println("Welcome to Guess the number Game");System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");gamer2 newj=new gamer2();newj.generatenum();}} 解决方案 You're blocking the Event Dispatching Thread by trying to read user input from the console.This is just a horribly bad idea, see Concurrency in Swing for more detailsGUI's are event driven environments, that is, something happens and you respond to it. Console programs tend to be far more linear in nature. It's not a good idea to mix them.If you're using a GUI, get your input from the user via the GUI. Take a closer look at Creating a GUI With JFC/Swing for more details. 这篇关于JFrame失去了结构,并在单击按钮后重新调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 13:35