JFrame丢失其结构并在按钮单击后调整大小

时间:2015-12-27 09:52:32

标签: java swing 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();

}

}

This is how the JFrame looks initially

但点击按钮后,JFrame会恶化: This is how it looks after clicking the button(on resizing).

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

课程generatenum中的

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

    }

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

  • 我知道JFrame在完成程序执行之前不会关闭,但在这种情况下,程序的其余部分不依赖于JFrame。它拒绝关闭,因为gamer2的对象是在{只有在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!");
               }
       }

    }

    }

类游戏玩家2:

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

}

2 个答案:

答案 0 :(得分:1)

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

这只是一个非常糟糕的主意,请参阅Concurrency in Swing了解更多详情

GUI是事件驱动的环境,也就是说,某些事情会发生并且您会对其做出响应。控制台程序本质上往往更加线性。将它们混合起来并不是一个好主意。

如果您使用的是GUI,请通过GUI从用户那里获取您的输入。仔细查看Creating a GUI With JFC/Swing了解更多详情。

答案 1 :(得分:-2)

尝试将<?php foreach ($declaredClasses as $key => $classWithNamespace) { $class = new ReflectionClass($classWithNamespace); $methods = $class->getMethods(); foreach ($methods as $method) { //analyze your methods } } 设置为默认关闭操作。

同时尝试实现DISPOSE_ON_CLOSE界面,如下所示:

WindowListener