JFrame失去了结构,并在单击按钮后重新调整大小 [英] JFrame losing its structure and looks on resizing after button click

查看:56
本文介绍了JFrame失去了结构,并在单击按钮后重新调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,只需单击一下按钮即可运行一个简单的数字猜测程序:

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();

}

}

但是在单击按钮时,JFrame恶化了:

即使提到了setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,单击按钮时JFrame也拒绝关闭.但是,如果我不单击按钮,JFrame就会关闭.

gamer2中的

generatenum函数:

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);

    }

有人可以向我解释实际出了什么问题吗?我一直在试图破解它,但到目前为止没有任何帮助. 事件处理部分运行正常.单击按钮,generatenum()成功运行.其余程序在Eclipse console上运行.

  • 我知道JFrame直到完整的程序执行后才会关闭,但是在这种情况下,程序的其余部分不依赖于JFrame.它拒绝关闭是因为gamer2的对象是在动作内创建的仅在JFrame存在的情况下存在的处理方法?
  • 当我调整JFrame的大小时,JFrame只会更改它的外观.调整它的大小会破坏外观,但是如果尚未单击按钮,则不会发生这种情况.

gamer3(接受用户输入):

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 );
    }

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!");
               }
       }

    }

    }

一流的Gamer2:

package test;
public class gamer2 {
int getIt;
//complete generation of the goddamn random number
void 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();
}

}

解决方案

您正在尝试通过从控制台读取用户输入来阻止事件调度线程.

这只是一个糟糕的主意,请参阅 Swing中的并发更多详情

GUI是事件驱动的环境,也就是说,发生了某些事情,您对此做出了响应.控制台程序本质上倾向于线性得多.混合它们不是一个好主意.

如果使用的是GUI,请通过GUI从用户那里获取输入.仔细了解使用JFC/Swing创建GUI 以获得更多详细信息.

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 number
void 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 details

GUI'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失去了结构,并在单击按钮后重新调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆