Java:如何重置 JFrame?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25936239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:58:56  来源:igfitidea点击:

Java: How to reset a JFrame?

javaswingframerestart

提问by DANIELBMZ

setVisible currently use to restart the methods, but the transition does a flashing, is there any instruction other than setVisible? leave it in its initial state. reset button on the last Frame_1 only to return to the initial state.

setVisible 目前用于重启方法,但是transition 会闪烁,除了setVisible 之外还有什么指令吗?将其保留在初始状态。最后 Frame_1 上的 reset 按钮只能返回到初始状态。

   public class frame_1 extends javax.swing.JFrame {

   private void btnfinishActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Period total = new Period(0, totalDuration, PeriodType.yearMonthDay());
        txta.append("________________________________________________" + "\n");
        txta.append("Duracion total: " + total.getYears() + " years " + total.getMonths() + " Months " + total.getDays() + " Days " + "\n");
        txtini.setText(null);
        txtfin.setText(null);
}                                        

private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {                                        
        period();
        txtini.setText(null);
        String fu = principio.txtingresa.getText();
        txtfin.setText(fu);
}                                       

public void period(){
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

        String startDate = txtini.getText();
        String endDate = txtfin.getText();

        Date start = null;
        Date end = null;
    try {
        start = sdf.parse(startDate);
        end = sdf.parse(endDate);
    } catch (ParseException ex) {
        Logger.getLogger(fecha_1.class.getName()).log(Level.SEVERE, null, ex);
    }

        if(end.getTime() < start.getTime()){
            JOptionPane.showMessageDialog(null, "the date finish y higher.!");
            txtini.setText(null);
            txtfin.setText(null);
        }else{
        long duration = end.getTime() - start.getTime();
        Period period = new Period(0, duration, PeriodType.yearMonthDay());

        txta.append(startDate + " - "+ endDate + "     =     " + period.getYears()+ "  years  " + period.getMonths() + "  Months  " + period.getDays() + "  Days  " + "\n");
        totalDuration += duration;
        }
        txtini.requestFocusInWindow();
}

   private void btnrestartActionPerformed(java.awt.event.ActionEvent evt) {                                        
        frame_1 obj1 = new frame_1();
        obj1.setVisible(false);
        obj1.setVisible(true);
        }

     }

回答by Hovercraft Full Of Eels

Based on your presented code, your reset button could be as simple as...

根据您提供的代码,您的重置按钮可能很简单...

private void btnrestartActionPerformed(java.awt.event.ActionEvent evt) {
   txta.setText("");
   txtini.setText("");
   txtfin.setText("");

   // here you have to initialize any other GUI fields and
   // any non-gui fields that need it.
}

回答by ControlAltDel

I think what you want is

我想你想要的是

JFrame.getContentPane().invalidate();
JFrame.getContentPane().validate();
JFrame.getContentPane().repaint();