【问题标题】:Accessing JDialog's JtextArea from parent JFrame?从父 JFrame 访问 JDialog 的 JtextArea?
【发布时间】:2014-04-04 12:50:09
【问题描述】:

我有 2 个案例,我在从父框架或 JDialog 本身打印和控制 JTextArea 时遇到问题。另外,当我单击它时,我将 Jbutton 添加到父 JFrame 中,出现 JDialog 但仍然存在打印问题。此外,这个 JButton 调用另一个类并执行扫描操作,我想将结果实际传递给我的 JDialog 的 JTextArea,我也为它编写代码,但是当我单击扫描按钮 JDialog 出现时,在我关闭它之后扫描操作开始.我希望在 JFrame 内开始扫描操作并将其打印到 JDialog 的组件,该组件是添加到我的 JDialog 的 JTextArea。

这是更好地理解我的问题的示例代码

// 父级 JFrame

  public class JDialogPrinting extends javax.swing.JFrame {


   JDialogSample jd ; 
   /**
    * Creates new form JDialogPrinting
    */
public JDialogPrinting() {
    initComponents();
}


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

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("Print Text To JDialog");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(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()
            .addGap(78, 78, 78)
            .addComponent(jButton1)
            .addContainerGap(97, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(72, 72, 72)
            .addComponent(jButton1)
            .addContainerGap(94, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

     jd = new JDialogSample(JDialogPrinting.this , true);

      JDialogPrinting.this.jd.tarea.setText("Test Print");
}                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(JDialogPrinting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(JDialogPrinting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(JDialogPrinting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JDialogPrinting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new JDialogPrinting().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
// End of variables declaration                   

}

//这是对话代码

   public class JDialogSample extends javax.swing.JDialog {

/**
 * Creates new form JDialogSample
 */
public JDialogSample(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    this.setVisible(modal);

}


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

    jScrollPane1 = new javax.swing.JScrollPane();
    tarea = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    tarea.setColumns(20);
    tarea.setRows(5);
    jScrollPane1.setViewportView(tarea);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(22, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(21, 21, 21))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(24, 24, 24)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(22, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        


// Variables declaration - do not modify                     
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTextArea tarea;
// End of variables declaration                   

}

热烈的问候

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。 2) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。
  • 自定义JDialog中声明的JTextArea在哪里?
  • 好的,我会简化案例
  • 是的,netbeans 会自动创建 JTextArea,它是隐藏代码,我也公开了,但仍然无法在其中打印。

标签: java swing jframe jtextarea jdialog


【解决方案1】:

为什么不让你的 ScanResultsDialog 几个额外的方法:

public void setText(String msg) { textArea.settext(msg); }; // good
public JTextArea getTextArea() { return textArea; }; // bad

很难,我以后会认为它非常不受欢迎——你为什么要从外部对对话框的内部对象做一些事情?似乎是个坏主意。

【讨论】:

  • 我建议你扔掉所有这些 netbeans 生成的布局代码,专注于基本要素。用你需要的 wat 做一个小例子。在您的示例中,我仍然看不到 setText() 。也许它在布局代码中丢失了:)
  • 大家好,我发现在设置文本之前调用 setVisible() 时出错 // Dialog 的构造函数 this.setVisible(modal); // 然后 JTextArea.settext("Test Print");这是错误的!只是扭转它!它会起作用的!问候
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
相关资源
最近更新 更多