Rumah > Java > Tetingkap GUI Java membeku semasa permulaan

Tetingkap GUI Java membeku semasa permulaan

王林
Lepaskan: 2024-02-09 12:09:33
ke hadapan
779 orang telah melayarinya

Tetingkap GUI Java membeku semasa permulaan adalah masalah biasa yang dihadapi oleh banyak pembangun. Dalam kes ini, pengguna tidak boleh berbuat apa-apa dan tetingkap kelihatan seperti tersekat. Sebabnya secara amnya kerana sesetengah operasi semasa proses pemulaan tetingkap mengambil masa terlalu lama, menyebabkan antara muka gagal bertindak balas secara normal. Untuk menyelesaikan masalah ini, kita perlu mencari punca pembekuan dan mengambil langkah yang sepadan. Dalam artikel ini, editor PHP Baicao akan memperkenalkan beberapa penyelesaian biasa untuk membantu anda menyelesaikan masalah pembekuan apabila tetingkap Java GUI bermula.

Kandungan soalan

Ini adalah program gui cuci kereta. Mengandungi dua kelas Demo dan Jumlah, yang selepas pengiraan akan memaparkan jumlah dalam tetingkap baharu. Semuanya berfungsi dengan baik, memutuskan untuk menjalankannya sekali lagi untuk menyerahkan tangkapan skrin, tetapi ia tersekat, seperti program sedang berjalan tetapi tetingkapnya dibekukan. Saya menggunakan kaedah seret dan lepas kacang bersih. Masukkan penerangan imej di sini Kelas demonstrasi

package car.wash_05;

public class demo extends javax.swing.jframe {
    private double small_engine = 100.0;
    private double big_engine = 200.0;
    private double battery = 100.0;
    private double oi_filter = 70.0;
    private double brakes = 100.0;
    private double gears = 100.0;
    private double regular_wash = 50.0;
    private double wash_compound = 100.0;

    // declare variables to store the selected services
    private boolean smallengineselected = false;
    private boolean bigengineselected = false;
    private boolean batteryselected = false;
    private boolean oilandfilterselected = false;
    private boolean brakesselected = false;
    private boolean gearsselected = false;
    private boolean regularwashselected = false;
    private boolean washandcompoundselected = false;

    // declare a variable to store the subtotal price
    private double subtotal = 0.0;
    
    public demo() {
        system.out.println("before gui initialization");
initcomponents();
system.out.println("after gui initialization");
    }
    
     private void updatesubtotal() {
        subtotal = 0.0;

        if (smallengineselected) 
            subtotal += small_engine;

        if (bigengineselected)
            subtotal += big_engine;

        if (batteryselected) 
            subtotal += battery;

        if (oilandfilterselected)
            subtotal += oi_filter;

        if (brakesselected) 
            subtotal += brakes;

        if (gearsselected) 
            subtotal += gears;
     }
    @suppresswarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="generated code">                          
    private void initcomponents() {

        jradiobutton2 = new javax.swing.jradiobutton();
        jlabel1 = new javax.swing.jlabel();
        jpanel1 = new javax.swing.jpanel();
        jradiobutton1 = new javax.swing.jradiobutton();
        jradiobutton3 = new javax.swing.jradiobutton();
        jpanel2 = new javax.swing.jpanel();
        jradiobutton4 = new javax.swing.jradiobutton();
        jradiobutton5 = new javax.swing.jradiobutton();
        jradiobutton6 = new javax.swing.jradiobutton();
        jpanel3 = new javax.swing.jpanel();
        jcheckbox1 = new javax.swing.jcheckbox();
        jcheckbox2 = new javax.swing.jcheckbox();
        jcheckbox3 = new javax.swing.jcheckbox();
        jcheckbox4 = new javax.swing.jcheckbox();
        jbutton1 = new javax.swing.jbutton();
        jbutton2 = new javax.swing.jbutton();

        jradiobutton2.settext("jradiobutton2");

        setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);
        settitle("auto tech and mech services");
        setenabled(false);

        jlabel1.settext("welcome to auto tech and mech services");

        jpanel1.setborder(javax.swing.borderfactory.createetchedborder());
        jpanel1.settooltiptext("");

        jradiobutton1.settext("small");
        jradiobutton1.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jradiobutton1actionperformed(evt);
            }
        });

        jradiobutton3.settext("big");
        jradiobutton3.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jradiobutton3actionperformed(evt);
            }
        });

        javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1);
        jpanel1.setlayout(jpanel1layout);
        jpanel1layout.sethorizontalgroup(
            jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
            .addgroup(jpanel1layout.createsequentialgroup()
                .addcontainergap()
                .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
                    .addcomponent(jradiobutton1)
                    .addcomponent(jradiobutton3))
                .addcontainergap(10, short.max_value))
        );
        jpanel1layout.setverticalgroup(
            jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
            .addgroup(jpanel1layout.createsequentialgroup()
                .addgap(39, 39, 39)
                .addcomponent(jradiobutton1)
                .addgap(29, 29, 29)
                .addcomponent(jradiobutton3)
                .addcontainergap(javax.swing.grouplayout.default_size, short.max_value))
        );

        jpanel2.setborder(javax.swing.borderfactory.createetchedborder());
        jpanel2.setname(""); // noi18n

        jradiobutton4.settext("none");
        jradiobutton4.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jradiobutton4actionperformed(evt);
            }
        });

        jradiobutton5.settext("regular wash");
        jradiobutton5.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jradiobutton5actionperformed(evt);
            }
        });

        jradiobutton6.settext("wash & compound");
        jradiobutton6.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jradiobutton6actionperformed(evt);
            }
        });

        javax.swing.grouplayout jpanel2layout = new javax.swing.grouplayout(jpanel2);
        jpanel2.setlayout(jpanel2layout);
        jpanel2layout.sethorizontalgroup(
            jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
            .addgroup(jpanel2layout.createsequentialgroup()
                .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
                    .addcomponent(jradiobutton4)
                    .addcomponent(jradiobutton5)
                    .addcomponent(jradiobutton6))
                .addgap(0, 0, short.max_value))
        );
        jpanel2layout.setverticalgroup(
            jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
            .addgroup(jpanel2layout.createsequentialgroup()
                .addgap(28, 28, 28)
                .addcomponent(jradiobutton4)
                .addgap(28, 28, 28)
                .addcomponent(jradiobutton5)
                .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value)
                .addcomponent(jradiobutton6)
                .addgap(14, 14, 14))
        );

        jpanel3.setborder(javax.swing.borderfactory.createetchedborder());
        jpanel3.settooltiptext("");
        jpanel3.setname(""); // noi18n

        jcheckbox1.settext("battery");
        jcheckbox1.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jcheckbox1actionperformed(evt);
            }
        });

        jcheckbox2.settext("oil filters");
        jcheckbox2.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jcheckbox2actionperformed(evt);
            }
        });

        jcheckbox3.settext("brakes");
        jcheckbox3.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jcheckbox3actionperformed(evt);
            }
        });

        jcheckbox4.settext("gear");
        jcheckbox4.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jcheckbox4actionperformed(evt);
            }
        });

        javax.swing.grouplayout jpanel3layout = new javax.swing.grouplayout(jpanel3);
        jpanel3.setlayout(jpanel3layout);
        jpanel3layout.sethorizontalgroup(
            jpanel3layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
            .addgroup(jpanel3layout.createsequentialgroup()
                .addcontainergap()
                .addgroup(jpanel3layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
                    .addcomponent(jcheckbox1)
                    .addcomponent(jcheckbox2)
                    .addcomponent(jcheckbox3)
                    .addcomponent(jcheckbox4))
                .addcontainergap(105, short.max_value))
        );
        jpanel3layout.setverticalgroup(
            jpanel3layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
            .addgroup(jpanel3layout.createsequentialgroup()
                .addgap(19, 19, 19)
                .addcomponent(jcheckbox1)
                .addgap(18, 18, 18)
                .addcomponent(jcheckbox2)
                .addgap(18, 18, 18)
                .addcomponent(jcheckbox3)
                .addgap(18, 18, 18)
                .addcomponent(jcheckbox4)
                .addcontainergap(8, short.max_value))
        );

        jbutton1.settext("calculate");
        jbutton1.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jbutton1actionperformed(evt);
            }
        });

        jbutton2.settext("exit");
        jbutton2.addactionlistener(new java.awt.event.actionlistener() {
            public void actionperformed(java.awt.event.actionevent evt) {
                jbutton2actionperformed(evt);
            }
        });

        javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane());
        getcontentpane().setlayout(layout);
        layout.sethorizontalgroup(
            layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
            .addgroup(layout.createsequentialgroup()
                .addcontainergap()
                .addcomponent(jpanel1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)
                .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)
                .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
                    .addcomponent(jlabel1)
                    .addgroup(layout.createsequentialgroup()
                        .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing, false)
                            .addgroup(layout.createsequentialgroup()
                                .addcomponent(jbutton1)
                                .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value)
                                .addcomponent(jbutton2))
                            .addcomponent(jpanel3, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size))
                        .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)
                        .addcomponent(jpanel2, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)))
                .addcontainergap(javax.swing.grouplayout.default_size, short.max_value))
        );
        layout.setverticalgroup(
            layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)
            .addgroup(layout.createsequentialgroup()
                .addgap(12, 12, 12)
                .addcomponent(jlabel1)
                .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated)
                .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing, false)
                    .addcomponent(jpanel3, javax.swing.grouplayout.alignment.leading, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)
                    .addcomponent(jpanel1, javax.swing.grouplayout.alignment.leading, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)
                    .addcomponent(jpanel2, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value))
                .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)
                .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline)
                    .addcomponent(jbutton1)
                    .addcomponent(jbutton2))
                .addcontainergap(javax.swing.grouplayout.default_size, short.max_value))
        );

        jpanel1.getaccessiblecontext().setaccessiblename("");
        jpanel2.getaccessiblecontext().setaccessiblename("");

        pack();
    }// </editor-fold>                        
    //small engine ratio button
    private void jradiobutton1actionperformed(java.awt.event.actionevent evt) {                                              

                smallengineselected = true;
                bigengineselected = false;
                jradiobutton3.setselected(false);
                updatesubtotal();
    }                                             
    
    //big engine ratio button
    private void jradiobutton3actionperformed(java.awt.event.actionevent evt) {                                              
        bigengineselected = true;
        smallengineselected = false;
        jradiobutton1.setselected(false);
        updatesubtotal();
    }                                             

    //oil filter check button
    private void jcheckbox2actionperformed(java.awt.event.actionevent evt) {                                           
       oilandfilterselected = jcheckbox2.isselected();
                updatesubtotal();
    }                                          

    //none ration button
    private void jradiobutton4actionperformed(java.awt.event.actionevent evt) {                                              
washandcompoundselected = false;
         regularwashselected = false ; 
         jradiobutton5.setselected(false);
         jradiobutton6.setselected(false);
    }                                             
    
    //battery check button
    private void jcheckbox1actionperformed(java.awt.event.actionevent evt) {                                           
        batteryselected = jcheckbox1.isselected();
                updatesubtotal();
    }                                          

    //brakes check button
    private void jcheckbox3actionperformed(java.awt.event.actionevent evt) {                                           
        brakesselected = jcheckbox3.isselected();
                updatesubtotal();
    }                                          
    
    //gear check button
    private void jcheckbox4actionperformed(java.awt.event.actionevent evt) {                                           
        gearsselected = jcheckbox4.isselected();
                updatesubtotal();
    }                                          

    //rugular was ration button
    private void jradiobutton5actionperformed(java.awt.event.actionevent evt) {                                              
        regularwashselected = true;
        washandcompoundselected =false;
        jradiobutton6.setselected(false);
                updatesubtotal();
    }                                             

    //wash and compound ration button
    private void jradiobutton6actionperformed(java.awt.event.actionevent evt) {                                              
         washandcompoundselected = true;
         regularwashselected = false;
         jradiobutton5.setselected(false);
                updatesubtotal();
    }                                             
    
    //calculate button
    private void jbutton1actionperformed(java.awt.event.actionevent evt) {                                         
        double total = subtotal + (subtotal * 0.1);
        double tax=subtotal * 0.1;
        total totalframe = new total(total, subtotal, tax);
        totalframe.setvisible(true);
        
    }                                        

    //exit button
    private void jbutton2actionperformed(java.awt.event.actionevent evt) {                                         
        system.exit(0);
    }                                        

    public static void main(string args[]) {
        java.awt.eventqueue.invokelater(new runnable() {
            public void run() {
                new demo().setvisible(true);
            }
        });
    }

    // variables declaration - do not modify                     
    private javax.swing.jbutton jbutton1;
    private javax.swing.jbutton jbutton2;
    private javax.swing.jcheckbox jcheckbox1;
    private javax.swing.jcheckbox jcheckbox2;
    private javax.swing.jcheckbox jcheckbox3;
    private javax.swing.jcheckbox jcheckbox4;
    private javax.swing.jlabel jlabel1;
    private javax.swing.jpanel jpanel1;
    private javax.swing.jpanel jpanel2;
    private javax.swing.jpanel jpanel3;
    private javax.swing.jradiobutton jradiobutton1;
    private javax.swing.jradiobutton jradiobutton2;
    private javax.swing.jradiobutton jradiobutton3;
    private javax.swing.jradiobutton jradiobutton4;
    private javax.swing.jradiobutton jradiobutton5;
    private javax.swing.jradiobutton jradiobutton6;
    // end of variables declaration                   
}
Salin selepas log masuk

Jumlah kelas

package car.wash_05;
public class Total extends javax.swing.JFrame {

    public Total() {
        
    }

    public Total(double tota, double subtota, double ta) {
        initComponents();
        jLabel1.setText("Subtotal: $" + subtota);
        jLabel2.setText("tax: $" + ta);
        jLabel3.setText("Total: $" + tota);
        
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Total");

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jLabel2.setText("jLabel2");

        jLabel3.setText("jLabel3");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(jButton1)))
                .addContainerGap(105, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        
//ok button
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         this.dispose();
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Total().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration                   
}
Salin selepas log masuk

Saya cuba menambah cek, memulakan semula sistem dan memasang semula java. Ia tidak berfungsi. Ia hanya membeku dan butang tutup dan lain-lain tidak berfungsi, ia perlu dipilih secara paksa daripada pengurus tugas.

Penyelesaian

Apa salah awak

Dalam initcomponents 方法的第 75 行,您正在使用 setenabled(false) Ini akan melumpuhkan tetingkap dan semua anak-anaknya (semua komponen pada bingkai), jadi ia akan berasa seperti tetingkap anda beku.

Untuk menyelesaikan masalah ini hanya keluarkan baris daripada kod berikut setenabled(false)

/* rest of your code */

jradiobutton2.settext("jradiobutton2");

setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);
settitle("auto tech and mech services");
setenabled(false); //line causing this issue

jlabel1.settext("welcome to auto tech and mech services");

/* rest of your code */
Salin selepas log masuk

Beberapa perkara yang lebih penting untuk diingat

• Ingat konvensyen penamaan! Elakkan menamakan kelas demo.java,而是使用 demo.java.

• Kedua, anda tidak memerlukan kaedah berasingan untuk mengendalikan tindakan yang dilaksanakan, cuma alihkannya terus ke dalam pendengar tindakan button.addactionlistener.

• Ketiga (dan ini adalah pilihan peribadi), anda tidak perlu menggunakan -

java.awt.eventqueue.invokelater(new runnable() {
    public void run() {
         new demo().setvisible(true);
    }
});
Salin selepas log masuk

Anda boleh memendekkan kod ini menggunakan lambda (ketahui lebih lanjut di sini) dan jadikan kod hanya satu baris seperti yang ditunjukkan di bawah.

java.awt.EventQueue.invokeLater(() -> new Demo().setVisible(true));
Salin selepas log masuk

Atas ialah kandungan terperinci Tetingkap GUI Java membeku semasa permulaan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:stackoverflow.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan