如何在我的JTextArea中添加新字符而不重复...当我按下我使用JAVA添加JButton时...我的代码我做了2个类(Form和FormRunner)期待有人能提前帮助thnx ...抱歉长代码-PaLoS(新手)
在JButtons中我真的需要最大的HelP
public class Form extends JFrame{
int validator = 0;
public String ID,Fname,Lname,Course,Year;
private final static String newline = "\n";
private final static String newtab = "\t";
List<Form> myList = new ArrayList<Form>();
private JTextArea summary,result;
private JPanel labelPanel,summaryPanel,resultPanel;
private JLabel iD,fName,lName,yearL,courseL,labelIcon,labelIconS,labelM;
private JButton searchB,addB,editB,deleteB,updateB;
private String labels[] = {"ID Number :","First Name :","Last Name :","Course :","Year :"};
private String namesB[] = {"Search" , "Add" , "Edit" , "Delete" , "Update" };
private JTextField formTitle,id,fname,lname,year,course;
Form(){
super("MAIN FORM: CASE STUDY");
setupGUI();
}
public void setupGUI(){
Container c = getContentPane();
c.setLayout(null);
ActionEventHandler handler = new ActionEventHandler();
Insets insets = c.getInsets();
formTitle = new JTextField("FILE MAINTENANCE ",53);
labelPanel = new JPanel();
//===================== LABELS ===========================
enter code here
//================================ Palos TEXT FIELDS ===================
id = new JTextField(20);
id.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isDigit(char1))) && (char1 != '\b') ){
ke.consume();
}
}
});
id.addActionListener(handler);
fname = new JTextField(20);
fname.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isLetter(char1))) && (char1 != '\b') ) {
ke.consume();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
});
fname.addActionListener(handler);
lname = new JTextField(20);
lname.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isLetter(char1))) && (char1 != '\b') ){
ke.consume();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
});
lname.addActionListener(handler);
year = new JTextField(20);
year.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar();
if((!(Character.isDigit(char1))) && (char1 != '\b') ) {
ke.consume();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
});
year.addActionListener(handler);
course = new JTextField(20);
course.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent ke){
char char1 = ke.getKeyChar(); if((!(Character.isLetter(char1))) && (char1 != '\b') ) {
ke.consume();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
});
course.addActionListener(handler);
//=============================== Palos Buttons ===========================
searchB = new JButton(namesB[0]);
searchB.setHorizontalAlignment(JTextField.CENTER);
**addB = new JButton(namesB[1]);
addB.setHorizontalAlignment(JTextField.CENTER);
addB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
id.selectAll();
fname.selectAll();
lname.selectAll();
course.selectAll();
year.selectAll();
String textID = id.getSelectedText();
String textFName = fname.getSelectedText();
String textLName = lname.getSelectedText();
String textCourse = course.getSelectedText();
String textYear = year.getSelectedText();
for(int i = 0; i < 1;i++){
for(int j = i;j < myList.size(); j++){
myList.add(Form.generateForm(textID));
if( !myList.contains(id.getSelectedText() )){
summary.append("["+ j + "]"+ textID +newtab + textFName + newtab
+ textLName + newtab + textCourse + newtab + textYear + newline);
}
else{
continue;
}
}
}
summary.setCaretPosition(summary.getDocument().getLength());
}
});
editB = new JButton(namesB[2]);
editB.setHorizontalAlignment(JTextField.CENTER);
deleteB = new JButton(namesB[3]);
deleteB.setHorizontalAlignment(JTextField.CENTER);
updateB = new JButton(namesB[4]);
updateB.setHorizontalAlignment(JTextField.CENTER);
updateB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
for(int i = 0; i < myList.size();i++ ){
summary.add(myList.get(i));
}
}
});***
//=================== Palos TextArea ===========================
summaryPanel = new JPanel();
summary.setEditable(false);
resultPanel = new JPanel();
result = new JTextArea(4,30);
result.setEditable(false);
summaryPanel.add(new JScrollPane(summary));
resultPanel.add(new JScrollPane(result));
//===================================== Palos Container ====================
some code here...
//================================== Palos Setting Of Insets ==================
setSize(700,400);
setVisible(true);
show();;
}
public static Form generateForm(final String Id){
Form stud = new Form();
stud.setID(Id);
return stud;
}
private class ActionEventHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == addB){
}
else if(e.getSource() == updateB){
}
}
}
public String getID() {
return ID;
}
public void setID(String id) {
ID = id;
}
}
答案 0 :(得分:1)
使用java.util.Set来跟踪唯一身份用户。在添加新的任何checkc之前,如果它已经存在于Set中 - 如果没有添加到Set并继续。
答案 1 :(得分:0)
在JTextArea的keyTyped方法中,添加以下内容:
char c = ke.getKeyChar();
if(yourTextArea.getText().contains(String.valueOf(c))){
//duplicate found, consume the event
}else{
//no duplicates
}