【发布时间】: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