想实现这样一个功能:有两个输入框A和B,如果A为空的话,B不可以编辑。
请大家帮忙看看我的code为什么没有实现
- Java code
package Test;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Demo implements MouseListener, KeyListener { JFrame jf; JPanel jp; JTextField text1; JTextField text2; String t2; String st = "不能为空"; public Demo() { jf = new JFrame("Demo"); jf.setSize(200, 200); text1 = new JTextField(); text1.setText("text1"); text1.setPreferredSize(new Dimension(100, 20)); text1.addMouseListener(this); text1.addKeyListener(this); text2 = new JTextField(); text2.setText("text2"); //text2.requestFocus(); text2.setPreferredSize(new Dimension(100, 20)); text2.addMouseListener(this); text2.addKeyListener(this); jp = new JPanel(); jp.setSize(100, 200); jp.add(text1); jp.add(text2); jf.getContentPane().add(jp, BorderLayout.CENTER); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub Demo test = new Demo(); } public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub checkValue(e); } public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub System.out.println("keyPressed@"); Object o = e.getSource(); if(o == text2){ String str = text1.getText(); str = text1.getText(); System.out.println("keyPressed"+str+e.getKeyCode()); if(str == null || ("").equals(str.trim()) || str.equals(st)){ System.out.println(text2.getText()); e.setKeyCode(KeyEvent.VK_UNDEFINED); System.out.println("keyPressed@" + e.getKeyCode()); System.out.println("------"+text2.getText()); } } } public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub System.out.println("keyReleased"); Object o = e.getSource(); if(o == text2){ String str = text1.getText(); str = text1.getText(); System.out.println("keyReleased"+str+ e.getKeyCode()); if(str == null || ("").equals(str.trim()) || str.equals(st)){ System.out.println(text2.getText()); e.setKeyCode(KeyEvent.VK_UNDEFINED); System.out.println("keyReleased@"+e.getKeyCode()); } } System.out.println("keyReleased@"); } public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub System.out.println("keyTyped@"); Object o = e.getSource(); if(o == text2){ String str = text1.getText(); str = text1.getText(); System.out.println("keyTyped"+str+e.getKeyCode()); if(str == null || ("").equals(str.trim()) || str.equals(st)){ System.out.println(text2.getText()); e.setKeyCode(KeyEvent.VK_UNDEFINED); System.out.println("keyTyped@" + e.getKeyCode()); System.out.println("++++++"+text2.getText()); } } } private void checkValue(MouseEvent e) { Object o = null; o = e.getSource(); if (o == text2) { t2 = text2.getText(); String str = text1.getText(); if (str == null || str.trim().equals("") || str.equals(st)) { text1.setBackground(Color.yellow); text1.setForeground(Color.lightGray); text1.setText(st); JOptionPane.showMessageDialog(null, "禁止", "alert", JOptionPane.ERROR_MESSAGE); text1.requestFocus(); } } if (o == text1) { String str = text1.getText(); if (str.equals(st)) { JTextField temp = (JTextField) o; temp.setText(""); temp.setBackground(Color.white); temp.setForeground(Color.black); } } } }