有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法设置JTextField,但可以输出变量

我以前也问过类似的问题,但这个问题更奇怪。简单地说,我有两个类,在第一个类(defineequationsquared)中,我将一个值传递给selectSolutionsquared。在SelectSolutionSquadratic中,我试图设置一个文本字段来显示这个值,但它不起作用。然而,工作的是将其输出到控制台/输出。我很困惑

public class DefineEquationQuadratic extends javax.swing.JFrame {

private void nextActionPerformed(java.awt.event.ActionEvent evt) {                                     
    this.setVisible(false);
    new SelectSolutionsQuadratic().setVisible(true);
    String dataSent = aQuadraticSpinner.getValue().toString() + " " + addSubQuadraticComboBox.getSelectedItem(); 
    SelectSolutionsQuadratic transferMe = new SelectSolutionsQuadratic();
    transferMe.displayText(dataSent);
}                                    

public class SelectSolutionsQuadratic extends javax.swing.JFrame {

public void displayText(String textToSet) {
    displayQuadraticEquation.setText(textToSet);
    displayQuadraticEquation.setText("Help); //This doesn't work by the way
    System.out.println(textToSet);
}

我觉得这些都是相关的代码,现在我将发布完整的类

定义方程式二次:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

/**
 *
 * @author MatthewAlanTroutman
 */
public class DefineEquationQuadratic extends javax.swing.JFrame {

/**
 * Creates new form DefineEquation
 */
public DefineEquationQuadratic() {
    initComponents();
}

public DefineEquationQuadratic(String transferData) {
    this.transferData = transferData;
    initComponents();
}
public String transferData;

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    graphSelectedLabel = new javax.swing.JLabel();
    defineTextLabel = new javax.swing.JLabel();
    quadraticEquationTextField = new javax.swing.JTextField();
    defineLabel = new javax.swing.JLabel();
    xSquaredQuadraticLabel = new javax.swing.JLabel();
    addSubQuadraticComboBox = new javax.swing.JComboBox();
    xQuadraticLabel = new javax.swing.JLabel();
    addSubQuadraticComboBox2 = new javax.swing.JComboBox();
    aQuadraticSpinner = new javax.swing.JSpinner();
    cQuadraticSpinner = new javax.swing.JSpinner();
    bQuadraticSpinner = new javax.swing.JSpinner();
    help = new javax.swing.JButton();
    close = new javax.swing.JButton();
    next = new javax.swing.JButton();
    back = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setMaximumSize(new java.awt.Dimension(1280, 800));

    graphSelectedLabel.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N
    graphSelectedLabel.setText("You selected the Quadratic graph type");

    defineTextLabel.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    defineTextLabel.setText("Please define your graph as an equation in the form of:");

    quadraticEquationTextField.setEditable(false);
    quadraticEquationTextField.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    quadraticEquationTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    quadraticEquationTextField.setText("ax^2+bx+c");
    quadraticEquationTextField.setToolTipText("");
    quadraticEquationTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

    defineLabel.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    defineLabel.setText("Define:");

    xSquaredQuadraticLabel.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    xSquaredQuadraticLabel.setText("x^2");

    addSubQuadraticComboBox.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    addSubQuadraticComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-" }));

    xQuadraticLabel.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    xQuadraticLabel.setText("x");

    addSubQuadraticComboBox2.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    addSubQuadraticComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-" }));

    aQuadraticSpinner.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    aQuadraticSpinner.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.0d), null, null, Double.valueOf(1.0d)));
    aQuadraticSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(javax.swing.event.ChangeEvent evt) {
            aQuadraticSpinnerStateChanged(evt);
        }
    });

    cQuadraticSpinner.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    cQuadraticSpinner.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.0d), null, null, Double.valueOf(1.0d)));
    cQuadraticSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(javax.swing.event.ChangeEvent evt) {
            cQuadraticSpinnerStateChanged(evt);
        }
    });

    bQuadraticSpinner.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    bQuadraticSpinner.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.0d), null, null, Double.valueOf(1.0d)));
    bQuadraticSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(javax.swing.event.ChangeEvent evt) {
            bQuadraticSpinnerStateChanged(evt);
        }
    });

    help.setFont(new java.awt.Font("Lucida Grande", 0, 48)); // NOI18N
    help.setText("Help");

    close.setFont(new java.awt.Font("Lucida Grande", 0, 48)); // NOI18N
    close.setText("Close");
    close.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            closeActionPerformed(evt);
        }
    });

    next.setFont(new java.awt.Font("Lucida Grande", 0, 48)); // NOI18N
    next.setText("Next");
    next.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nextActionPerformed(evt);
        }
    });

    back.setFont(new java.awt.Font("Lucida Grande", 0, 48)); // NOI18N
    back.setText("Back");
    back.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            backActionPerformed(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()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(defineTextLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(quadraticEquationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(154, 154, 154))
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(26, 26, 26)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(graphSelectedLabel)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(defineLabel)
                            .addGap(18, 18, 18)
                            .addComponent(aQuadraticSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(xSquaredQuadraticLabel)
                            .addGap(18, 18, 18)
                            .addComponent(addSubQuadraticComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(bQuadraticSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(12, 12, 12)
                            .addComponent(xQuadraticLabel)
                            .addGap(18, 18, 18)
                            .addComponent(addSubQuadraticComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(cQuadraticSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(layout.createSequentialGroup()
                    .addGap(233, 233, 233)
                    .addComponent(help, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(back, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(close, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(next, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)))))
            .addContainerGap(39, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(77, 77, 77)
            .addComponent(graphSelectedLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(defineTextLabel)
                .addComponent(quadraticEquationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(77, 77, 77)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(defineLabel)
                .addComponent(xSquaredQuadraticLabel)
                .addComponent(addSubQuadraticComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(xQuadraticLabel)
                .addComponent(addSubQuadraticComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(aQuadraticSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(bQuadraticSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(cQuadraticSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 211, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(help, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(close, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(next, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addComponent(back, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(65, 65, 65))
    );

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

private void closeActionPerformed(java.awt.event.ActionEvent evt) {                                      
    System.exit(0);
}                                     

private void nextActionPerformed(java.awt.event.ActionEvent evt) {                                     
    this.setVisible(false);
    new SelectSolutionsQuadratic().setVisible(true);
    String dataSent = aQuadraticSpinner.getValue().toString() + " " + addSubQuadraticComboBox.getSelectedItem(); 
    SelectSolutionsQuadratic transferMe = new SelectSolutionsQuadratic();
    transferMe.displayText(dataSent);
}                                    

private void backActionPerformed(java.awt.event.ActionEvent evt) {                                     
    this.setVisible(false);
    new GraphEquationSolverGUI().setVisible(true);
}                                    

private void aQuadraticSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                               
    String aQuadraticSpinnerValue = (String)aQuadraticSpinner.getValue().toString();
    if (!"0.0".equals(aQuadraticSpinnerValue))
    {
        aQuadraticSpinnerValue = aQuadraticSpinnerValue + "x^2";

    }
    else
    {
        aQuadraticSpinnerValue = "";
    }
    System.out.println(aQuadraticSpinnerValue);
    transferData = (String)addSubQuadraticComboBox.getSelectedItem();
    System.out.println(transferData);
}                                              

private void bQuadraticSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                               
    String bQuadraticSpinnerValue = (String)bQuadraticSpinner.getValue();
}                                              

private void cQuadraticSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                               
    String cQuadraticSpinnerValue = (String)cQuadraticSpinner.getValue();
}     
/**
 * @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(DefineEquationQuadratic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(DefineEquationQuadratic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(DefineEquationQuadratic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(DefineEquationQuadratic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    //</editor-fold>

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


    });
}

// Variables declaration - do not modify                     
private javax.swing.JSpinner aQuadraticSpinner;
private javax.swing.JComboBox addSubQuadraticComboBox;
private javax.swing.JComboBox addSubQuadraticComboBox2;
private javax.swing.JSpinner bQuadraticSpinner;
private javax.swing.JButton back;
private javax.swing.JSpinner cQuadraticSpinner;
private javax.swing.JButton close;
private javax.swing.JLabel defineLabel;
private javax.swing.JLabel defineTextLabel;
private javax.swing.JLabel graphSelectedLabel;
private javax.swing.JButton help;
private javax.swing.JButton next;
private javax.swing.JTextField quadraticEquationTextField;
private javax.swing.JLabel xQuadraticLabel;
private javax.swing.JLabel xSquaredQuadraticLabel;
// End of variables declaration                   
}

选择解决方案:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

/**
 *
 * @author MatthewAlanTroutman
 */
public class SelectSolutionsQuadratic extends javax.swing.JFrame {
    /**
     * Creates new form SelectSolutionsQuadratic
     */
public SelectSolutionsQuadratic() {
    initComponents();
}

public SelectSolutionsQuadratic(String theData) {
    this.theData = theData;
    initComponents();
}

public String theData;

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    displayQuadraticEquation = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setSize(new java.awt.Dimension(1280, 800));

    displayQuadraticEquation.setEditable(false);
    displayQuadraticEquation.setColumns(1);
    displayQuadraticEquation.setToolTipText("");
    displayQuadraticEquation.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            displayQuadraticEquationActionPerformed(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(568, 568, 568)
            .addComponent(displayQuadraticEquation, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(422, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(75, 75, 75)
            .addComponent(displayQuadraticEquation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(697, Short.MAX_VALUE))
    );

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

private void displayQuadraticEquationActionPerformed(java.awt.event.ActionEvent evt) {                                                         
    // TODO add your handling code here:
}                                                        
public void displayText(String textToSet) {
    displayQuadraticEquation.setText(textToSet);
    displayQuadraticEquation.setText("Help); //This doesn't work by the way
    System.out.println(textToSet);
}


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

}

// Variables declaration - do not modify                     
public javax.swing.JTextField displayQuadraticEquation;
// End of variables declaration                   
}

共 (1) 个答案

  1. # 1 楼答案

    您正在创建两个不同的JFrame实例,特别是SelectSolutionsQuadratic

        new SelectSolutionsQuadratic().setVisible(true);//1 
        ... 
        SelectSolutionsQuadratic transferMe = new SelectSolutionsQuadratic();//2
    

    这将创建两个不同的实例

    • 第一个是可见的,但您没有调用displayText
    • 您正在将值设置为第二个值,但它不可见

    因此,只创建一个实例并调用jFrame.setVisible(true)


    解决方案:

    DefineEquationQuadratic#nextActionPerformed

    //new SelectSolutionsQuadratic().setVisible(true);//REMOVE THIS LINE
    String dataSent = aQuadraticSpinner.getValue().toString() + " " +   
                        addSubQuadraticComboBox.getSelectedItem(); 
    SelectSolutionsQuadratic transferMe = new SelectSolutionsQuadratic();
    transferMe.displayText(dataSent);
    transferMe.setVisible(true);//ADD THIS LINE