繁体   English   中英

从父JFrame访问JDialog的JtextArea?

[英]Accessing JDialog's JtextArea from parent JFrame?

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

}

温暖的问候

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

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

艰难的是,我以后认为不推荐使用-为什么您应该从外部对对话框的内部对象执行某些操作? 似乎是个坏主意。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM