【问题标题】:duplicate jframes appear in netbeans重复的jframe出现在netbeans中
【发布时间】:2015-07-01 08:30:51
【问题描述】:

我正在将数据从 frame1 中的 jtable 传输到 frame2 中的另一个 jtable。 问题是,每次我从我的第一个表中选择一行并按下按钮(将其传输到另一个表)时,都会弹出另一个框架,并且数据被输入到第一行。我希望所有选定的数据都在同一个表中。

我知道我在代码中写错了,但我不知道正确的解决方案!这是我的代码

 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int row = jTable2.getSelectedRow();        
    String a1=jTable2.getModel().getValueAt(row, 1).toString();
    String a2=jTable2.getModel().getValueAt(row, 4).toString();
    String a3=jTable2.getModel().getValueAt(row, 21).toString();
    String a4=jTable2.getModel().getValueAt(row, 5).toString();
    String a5=jTable2.getModel().getValueAt(row, 22).toString();
     NewJFrame2 fr2 = new NewJFrame2();
     fr2.gencode(a1, a2, a3, a4, a5);
        //this.dispose();
        fr2.setVisible(true);
}

这个在另一个框架中

public NewJFrame2() {
    initComponents();
}
        int i=0;
        public void gencode(String a1, String a2, String a3, String a4, String a5){
            System.out.print(i);
            i++;
        jTable1.setValueAt(a1, i, 0);
        jTable1.setValueAt(a2, i, 1);
        jTable1.setValueAt(a3, i, 2);
        jTable1.setValueAt(a4, i, 3);
        jTable1.setValueAt(a5, i, 4);

   }

【问题讨论】:

  • 您在actionPerformed 方法中创建了JFrame 的新实例,作为NewJFrame2 fr2 = new NewJFrame2();,因此很明显,新的JFrame 必然会出现。而是使用相同的引用,并且不要在 actionPerformed (...) 方法中使用 new NewJFrame2 ()。只需将NewJFrame2 fr2 设为实例变量,而不是将其设为局部变量,并仅在某处初始化一次。
  • 每次创建新的 NewJFrame2 fr2 = new NewJFrame2();对象,这就是它创建的原因。尝试在类中创建一次它作为其数据成员并使用它 jButton3ActionPerformed
  • @nIcEcOw 是的,我知道。我该如何纠正它?
  • @AnmolKhanna 你想让 jtable 2 出现在新框架还是同一框架中??
  • @FastSnail 我在不同的框架中将行从 jtable2 馈送到 jtable1

标签: java swing netbeans jtable jframe


【解决方案1】:

问题是您正在按钮单击事件中创建新框架

 NewJFrame2 fr2 = new NewJFrame2(); //here is the problem

在你的第一帧中声明一个实例变量 frame2

class One{
  NewJFrame2 fr2;
}

并在需要时初始化它,但只有一次 并在动作方法中使用

  if(fr2==null){
      fr2=new  NewJFrame2(); //initialize if it's null
  }
  fr2.gencode(a1, a2, a3, a4, a5);

【讨论】:

    【解决方案2】:
    NewJFrame2 fr2=null; //class level variable
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            int row = jTable2.getSelectedRow();        
            String a1=jTable2.getModel().getValueAt(row, 1).toString();
            String a2=jTable2.getModel().getValueAt(row, 4).toString();
            String a3=jTable2.getModel().getValueAt(row, 21).toString();
            String a4=jTable2.getModel().getValueAt(row, 5).toString();
            String a5=jTable2.getModel().getValueAt(row, 22).toString();
            //create instance when not initiated
            if(fr1==null){
                fr2 = new NewJFrame2();
            }
             fr2.gencode(a1, a2, a3, a4, a5);
                //this.dispose();
                fr2.setVisible(true);
        }
    

    将fr2设为类级变量,未启动时创建实例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多