![](/img/trans.png)
[英]How to set text of a JTextField of a JFrame from another JFrame?
[英]How to reset a jframe and saving text from a jtextfield
我想写一个小程序。 程序应运行的周期数已定义。 用户在 Jtextfield 中插入文本。 每次用户按回车键,就计算一个循环,并再次创建相同的 JFrame。 这个过程重复直到最大。 达到循环次数。 程序将创建另一个 Jframe。 我试着写代码,但程序并没有增加循环次数。
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);
}
}
我想我知道我遇到的问题:Evertime If 语句为真并且 Jframe 被处理并创建一个新的 int Cycles 设置为 0。如果这是问题,我不知道要解决这个问题. 是否需要处理 Jframe 或者有没有办法将文本保存在文本字段中然后重新加载框架? 或者还有什么我可以做的吗?
我正在使用 NetBeans GUI 构建器。
发生这种情况是因为当你创建一个新的OneFrame
,新的Cycles
创建并自动等于 0。要解决这个问题,请将变量Cycles
更改为static
,因此您将创建的每个OneFrame
都将是 1 个变量,并且不会每次都重置为 0当你创建一个新的OneFrame
。 例如,使用:
private static int Cycles = 0;
代替:
private int Cycles = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.