无法将文本追加/添加到jTextArea

时间:2016-07-11 11:49:44

标签: java forms

我正在尝试构建一个具有文本区域控制台的应用程序,我无法将文本追加或添加到jtextarea内的文本中。我想知道是否有人可以帮助我解决为什么我的代码没有显示错误?

代码如下:

表格1:

package com.company;
public class Form1 extends javax.swing.JFrame {

private javax.swing.JButton btnComboBox;
private javax.swing.JComboBox<String> comboOne;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;

public Form1() {
    initComponents();
}

@SuppressWarnings("unchecked")

private void initComponents() {

    jLabel2 = new javax.swing.JLabel();
    comboOne = new javax.swing.JComboBox<>();
    jLabel3 = new javax.swing.JLabel();
    btnComboBox = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel2.setText("SELECT PRINTER:");

    comboOne.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Printer 1", "Printer 2", "Printer 3", "Printer 4" }));
    comboOne.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            comboOneActionPerformed(evt);
        }
    });

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
    jLabel3.setText("ZEBRA PRINTER ");

    btnComboBox.setText("GO");
    btnComboBox.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnComboBoxActionPerformed(evt);
        }
    });

    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(154, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(comboOne, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                                            .addComponent(btnComboBox, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGap(153, 153, 153))
    );
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel3)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(comboOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(btnComboBox)
                            .addContainerGap(176, Short.MAX_VALUE))
    );

    pack();
}

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

    Object selected = comboOne.getSelectedItem().toString();

}

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

    // Opens a new form, converts the object selected in combobox too a string
    // Passes string to openMe on form 2
    Form2 f2 = new Form2();
    f2.openMe(comboOne.getSelectedItem().toString());
    this.setEnabled(false);

}

public static void main(String args[]) {

    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(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }


    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Form1().setVisible(true);
        }
    });



    }

}

表格2:

package com.company;
public class Form2 extends javax.swing.JFrame {

private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jta;

public Form2() {

    initComponents();
    jta.append("----- Console Output ------\n");
    jta.append("----------------------------------");

}

@SuppressWarnings("unchecked")

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jta = new javax.swing.JTextArea();
    jScrollPane2 = new javax.swing.JScrollPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Console");

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
    jLabel3.setText("ZEBRA PRINTER ");

    jta.setColumns(20);
    jta.setRows(5);
    jScrollPane2.setViewportView(jta);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(layout.createSequentialGroup()
                                                            .addContainerGap()
                                                            .addComponent(jLabel1))
                                                    .addGroup(layout.createSequentialGroup()
                                                            .addGap(158, 158, 158)
                                                            .addComponent(jLabel3)))
                                            .addGap(0, 192, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                            .addContainerGap()
                                            .addComponent(jScrollPane2)))
                            .addContainerGap())
    );
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addGap(12, 12, 12)
                            .addComponent(jLabel3)
                            .addGap(17, 17, 17)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                            .addContainerGap())
    );

    pack();
}

public void openMe(String message) {


    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(Form2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Form2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Form2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Form2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Form2().setVisible(true);
            new Form1().setVisible(false);

        }

    });

    System.out.println("----- Console Output ------");
    System.out.println("---------------------------");
    System.out.println("Printer selected: "+ message);
    System.out.println("---------------------------");
    System.out.println("\n");

    jta.setText(message);
    jta.append("Printer selected:"+message);


    }
}

任何帮助一如既往的赞赏。

0 个答案:

没有答案