来自文本字段的数据不会存储到我的字段变量中

时间:2015-04-05 19:07:10

标签: java swing listener

我尽我所能,我已经尝试用actionlistener注册文本字段并使用了getsource,我尝试存储来自textfield的数据的变量仍然是null,然后试图获取数据按下一个按钮,用actionlistener注册我的按钮,但变量仍然出现null,我做错了什么?

package stackovtest;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class StackovTest extends JFrame {

public StackovTest()
{
    super ("FNA");
    setLayout(new BorderLayout());
    setResizable(false);
    stackOVClass comps = new stackOVClass();
    add(comps);
    pack();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

   public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }
            new StackovTest();
        }
    });
   }   
}


package stackovtest;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;


public class stackOVClass extends JPanel {

 // variable declarations
private final JLabel pLabel;
private final JTextField pTextField; 

private final JLabel newbLabel;
private final JTextField newbTextField;

private final JButton changeButton;

private final JTextArea dispTextArea;

// end of variable declarations

public stackOVClass()
{
    super(new GridBagLayout());

    setPreferredSize(new Dimension(600,400));

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(10,10,10,10);

    Color code = new Color(0, 255, 255);
    Border border = BorderFactory.createLineBorder(code);


    /////////// COLUMN 1//////////////

    pLabel = new JLabel("Policy #");
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.FIRST_LINE_END;
    add(pLabel, gbc);

    newbLabel = new JLabel("NB Date:");
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.WEST;
    add(newbLabel, gbc);

    ////////// COLUMN 2 ///////////

    pTextField = new JTextField(20);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.LINE_START;
    pTextField.setBorder(border);
    add(pTextField, gbc);

    newbTextField = new JTextField(20);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.LINE_START;
    newbTextField.setBorder(border);
    add(newbTextField, gbc);

    changeButton = new JButton("Change");
    gbc.gridx = 1;
    gbc.gridy = 4;
    gbc.weightx = 0.5;
    gbc.weightx = 0.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.WEST;
    add(changeButton, gbc);
    ///////////////////// TEXT AREA ///////////////////////

    dispTextArea = new JTextArea();
    gbc.gridx = 0;
    gbc.gridy = 6;
    gbc.gridwidth = 9;
    gbc.gridheight = 4;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.PAGE_END;
    dispTextArea.setEditable(true);
    dispTextArea.setFont(new Font("Serif", Font.BOLD, 18));
    dispTextArea.setLineWrap(true);
    dispTextArea.setWrapStyleWord(true);

    JScrollPane scroll = new JScrollPane(dispTextArea);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scroll.setPreferredSize(new Dimension(50, 120));
    scroll.setBorder(border);
    add(scroll, gbc);

    CompHandler compHandler = new CompHandler();
    changeButton.addActionListener(compHandler);

   }



    // class to handle text fields
    private class CompHandler implements ActionListener 
    {
        private String pNum;
        private String newbDate;
        private final String change = "See new premium change. ";
        private final String decrease = "See new premium decrease. ";
        private final String increase = "See new premium increase. ";
        private final String comment = "() Policy Number (" + pNum + ") validated prior insurance effective (" + newbDate + ") per documentation in Image Center." 
                                         + "Verified prior BI Limits of () with () days lapse in coverage. ";

        @Override
        public void actionPerformed(ActionEvent e) 
        {

            Object button_command = e.getActionCommand();

            if (button_command.equals("Change"))
            {
                pNum = pTextField.getText();
                newbDate = newbTextField.getText();

                dispTextArea.setText(comment + change);
            }
        }
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates

    } // end component handler class

}

这应该运行它已经过适当的测试......我喜欢这方面的建议,或者更好的方式我可以实现我的代码

1 个答案:

答案 0 :(得分:1)

您将pNum和newbDate字符串添加到另一个字符串,而它们仍然为空。然后更改pNum和newbDate以保存值,但另一个String不会更改,也不会更改。字符串是不可变的,即使它们是可变的,对象也不会被魔法改变,你必须使用新的更新数据设置字符串变量以显示数据。

在坚果壳中,这就是你正在做的事情:

public static void main(String[] args) {
  String foo = null;
  String bar = null;
  String baz = "foo: " + foo + "; bar: " + bar;

  foo = "FOO";
  bar = "BAR";

  System.out.println(baz);
}

返回:

foo: null; bar: null

请理解,因为你已经改变了foo并且之后这个事实,所以String baz不会因魔术而改变。如果您希望baz更改,需要在需要时进行更改:

public static void main(String[] args) {
  String foo = null;
  String bar = null;
  String baz = "foo: " + foo + "; bar: " + bar;

  foo = "FOO";
  bar = "BAR";

  // change baz with new data
  baz = "foo: " + foo + "; bar: " + bar;

  System.out.println(baz);  // now this works!
}