hashCode方法实现不显示等于JTextField

时间:2017-10-23 14:26:25

标签: java swing hashtable jtextfield hashcode

我创建了一个继承自JTextField的类。该类的实例应存储在Hashtable中,它还可以存储JTable,因此声明为Hashtable。 当我使用方法hashCode()时,JTextFields消失,除了第一个。如果我删除或注释hashCode()方法,它的工作原理。 我不知道什么是heppening,所有的代码都很长很复杂所以我无法发布它,它是由netbeans生成的。 测试此代码,您可以看到不显示等于文本字段。 这是班级:

         import java.awt.*;
         import java.awt.event.*;
         import javax.swing.*;
         import java.util.*;

    public class TextDemo extends JPanel implements ActionListener {
          protected static SelectableTextField textField;
          protected static SelectableTextField textField1;
          protected static SelectableTextField textField2;
          protected static SelectableTextField textField3;
          protected JTextArea textArea;
          private final static String newline = "\n";
          static int i = 0;

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

    textField = new SelectableTextField("sox", "name");
    textField1 = new SelectableTextField("sox", "surname");
    textField2 = new SelectableTextField("anam", "ali");
    textField3 = new SelectableTextField("general", "ali");
    textField.addActionListener(this);

    textArea = new JTextArea(5, 20);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Add Components to this panel.
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;

    c.fill = GridBagConstraints.HORIZONTAL;
    add(textField, c);
    add(textField1, c);
    add(textField2, c);
            add(textField3, c);

    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    add(scrollPane, c);
}

public void actionPerformed(ActionEvent evt) {
    String text = textField.getText();
    textArea.append(text + newline);
    textField.selectAll();

    //Make sure the new text is visible, even if there
    //was a selection in the text area.
    textArea.setCaretPosition(textArea.getDocument().getLength());
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TextDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add contents to the window.
    frame.add(new TextDemo());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
            Hashtable<Object, String> h = new Hashtable<Object, String>();
            h.put(textField, "hello");
            h.put(textField1, "world");
            h.put(textField2, "holy");

        }
    });
}

class SelectableTextField extends javax.swing.JTextField {
   private String tablename = "";
   private String columnname = "";

   public SelectableTextField(String tabname, String colname) {
        super(Integer.toString((i++)));
    //this.addMouseListener(this);
       tablename = tabname;    //get table name
       columnname = colname;    //get column name
   }


@Override
public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof SelectableTextField))
        return false;
    SelectableTextField st = (SelectableTextField) o;
    return st.getTablename().equals(this.tablename);
}

@Override
public int hashCode() { 
    return tablename.hashCode();
}

public String getTablename() { return tablename; }
public String getColumnname() { return columnname; }
 }




  }

0 个答案:

没有答案