简体   繁体   中英

my app stops working when I put a while loop

im creating a app with java for making app easyer than coding it but when i started i created two inputs for height and width and a button to create a frame with that informatiion. then i made a while loop to if the number of the height or width changes, change the width and height of the new frame. after that my app stops working and freeze.

i tried to making a jpanel not a jframe or creating a method for that but still freesing.

what do i do to not freeze?

Main.java

public class Main {
    public static void main(String[] args) {
        new Frame();
    }
}

Frame.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Frame extends JFrame implements ActionListener {
    SpinnerNumberModel modelheight;
    JSpinner heightinp;
    SpinnerNumberModel modelwidth;
    JSpinner widthinp;
    JButton newframebutton;
    private int heightdyn;
    private int height;
    private int widthdyn;
    private int width;
    private boolean framerunning= false;
    Frame(){
        //-inputs------------------------------------------------------

        modelheight = new SpinnerNumberModel(500,0,2000,1);
        modelwidth = new SpinnerNumberModel(500,0,2000,1);

        heightinp = new JSpinner(modelheight);
            heightinp.setBounds(25,25,250,50);



        widthinp = new JSpinner(modelwidth);
            widthinp.setBounds(25,100,250,50);

        //--give-value-------------------------------------------------
        heightdyn = (int) heightinp.getValue();
        height = heightdyn;
        widthdyn = (int) widthinp.getValue();
        width = widthdyn;
        //--button-manager---------------------------------------------
        newframebutton = new JButton("New Frame");
        newframebutton.setBounds(25,175,50,50);

        newframebutton.addActionListener(this);






        //--frame-manager----------------------------------------------

        JPanel framepanel = new JPanel();
        framepanel.setPreferredSize(new Dimension(600,600));
        framepanel.setLayout(null);

        framepanel.add(heightinp);
        framepanel.add(widthinp);
        framepanel.add(newframebutton);





        this.add(framepanel);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.getContentPane().setBackground(new Color(255, 255, 255));
        this.setLocationRelativeTo(null);
        this.setResizable(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==newframebutton){
            JFrame frame = new JFrame();
            height = heightdyn;
            width = widthdyn;
            framerunning = true;
            frame.setSize(width,height);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(3);
            while (framerunning){
                heightdyn = (int) heightinp.getValue();
                widthdyn = (int) widthinp.getValue();
                if (height != heightdyn || width != widthdyn){
                    frame.setSize(widthdyn,heightdyn);
                    height = heightdyn;
                    width = widthdyn;
                }
            }
        }
    }
}

while (framerunning) after setting framerunning=true. Also, you are not modifying framerunning variable inside the while loop. So, the system will go in an infinite loop. So, ultimately your thread will get blocked at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM