简体   繁体   English

如何重置 jframe 并保存 jtextfield 中的文本

[英]How to reset a jframe and saving text from a jtextfield

I want to write a little programm.我想写一个小程序。 There is defined number of cycles the programm shall run.程序应运行的周期数已定义。 The User inserts text in a Jtextfield.用户在 Jtextfield 中插入文本。 Everytime the user hits enter, one cycle is counted and the same JFrame shall be created again.每次用户按回车键,就计算一个循环,并再次创建相同的 JFrame。 This procedure repeats until the max.这个过程重复直到最大。 number of cycles is reached.达到循环次数。 The programm shall create another Jframe.程序将创建另一个 Jframe。 I tried to write the code, but the program does not increase the number of cycles.我试着写代码,但程序并没有增加循环次数。

public class oneJFrame extends javax.swing.JFrame {

    private int Cycles = 0;
    
    
    public oneJFrame() {
        initComponents();
    }                    

    private void clsWinBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        dispose();
    }                                         

    protected void playerNameFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        if (Cycles <= 4) {
            System.out.println(Cycles );
            dispose();
            oneJFrame JFrame1 = new oneJFrame();
            JFrame1.setVisible(true);
            Cycles++;
        } else {
            anotherJFrame Jframe2 = new anotherJFrame ();
            Jframe2.setVisible(true);
        }
        
  
    } 

I think I know the problem that I´ve got: Evertime the If Statement is true and the Jframe is disposed and a new one is created the int Cycles is set to 0. If this is the problem, I have no Idea to solve this.我想我知道我遇到的问题:Evertime If 语句为真并且 Jframe 被处理并创建一个新的 int Cycles 设置为 0。如果这是问题,我不知道要解决这个问题. Is there a need to dispose the Jframe or is there a way just to save the text in the textfield and then reload the frame?是否需要处理 Jframe 或者有没有办法将文本保存在文本字段中然后重新加载框架? Or is there something else I could do?或者还有什么我可以做的吗?

I am using the NetBeans GUI Builder.我正在使用 NetBeans GUI 构建器。

This is happend because when you create a new OneFrame , new Cycles created and automaticly equals to 0. To fix that, change the variable Cycles to static , so will be 1 variable to every OneFrame you will create, and will not reset to 0 every time you create a new OneFrame .发生这种情况是因为当你创建一个新的OneFrame ,新的Cycles创建并自动等于 0。要解决这个问题,请将变量Cycles更改为static ,因此您将创建的每个OneFrame都将是 1 个变量,并且不会每次都重置为 0当你创建一个新的OneFrame For example, use:例如,使用:

private static int Cycles = 0;

Instead of:代替:

private int Cycles = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个 JFrame 设置 JFrame 的 JTextField 的文本? - How to set text of a JTextField of a JFrame from another JFrame? 如何从JTextField到另一个JFrame中的JLabel获取文本? - How to get text from JTextField to JLabel that is in another JFrame? 如何将JTextField从JFrame传递到另一个JFrame? - How to pass JTextField from JFrame into another JFrame? 如何将JTextField从JFrame传递到另一个JFrame - How to pass JTextField from JFrame into another JFrame 将文本从Jtextfield(Jframe)发送到另一个类 - Send Text From Jtextfield (Jframe) To another class JFrame:如何在没有JTextArea / JTextField的情况下显示文本 - JFrame: How to display text without JTextArea/JTextField 如何将Text设置为jFrame内的jPanel内的jTextField到其他jFrame - How to setText into a jTextField that is inside a jPanel that is inside a jFrame to from Other jFrame 如何将JTextfield信息从JDialog传递到JFrame(单独的类) - How to pass JTextfield info from a JDialog into a JFrame (separate classes) 如何在JFrame中调整JTextField的大小? - How to resize a JTextField within a JFrame? 如何将JLabel从JFrame中的一个JPanel拖动到同一JFrame中另一个JPanel的JTextField中? - How should I drag a JLabel from one JPanel in a JFrame onto a JTextField in another JPanel in the same JFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM