2012-04-12 68 views
10

當鼠標在該文本字段中單擊時,需要使該程序清除文本字段中的文本。我嘗試了一些東西,但是他們都沒有爲我工作。如何在鼠標單擊JTextField時清除JTextField

下面是全部代碼:

public class TimerClassPanel extends JFrame implements MouseListener{ 

    public TimerClassPanel(){ 
     setTitle("Timer Class"); 
     setSize(WIDTH, HEIGHT); 

     timer = new Timer(DELAY, new TimerEventHandler()); 

     pane = getContentPane(); 
     pane.setLayout(null); 

     int r = (int)(9.0 * Math.random()) + 1; 
     String str2 = Integer.toString(r); 

     label = new JLabel(str2, SwingConstants.CENTER); 
     label.setSize(150,30); 
     label.setLocation(0,0); 

     textField = new JTextField(); 
     textField.setSize(150,30); 
     textField.setLocation(150,0); 

     startB = new JButton("Start"); 
     startbh = new StartButtonHandler(); 
     startB.addActionListener(startbh); 
     startB.setSize(100,30); 
     startB.setLocation(0,30); 

     stopB = new JButton("Stop"); 
     stopbh = new StopButtonHandler(); 
     stopB.addActionListener(stopbh); 
     stopB.setSize(100,30); 
     stopB.setLocation(100,30); 

     exitB = new JButton("Exit"); 
     ebHandler = new ExitButtonHandler(); 
     exitB.addActionListener(ebHandler); 
     exitB.setSize(100,30); 
     exitB.setLocation(200,30);  

     pane.add(label); 

     pane.add(textField); 
     pane.add(startB); 
     pane.add(stopB); 
     pane.add(exitB); 

     timer = new Timer(DELAY, new TimerEventHandler()); 

     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    private class TimerEventHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      int r = (int)(9.0 * Math.random()) + 1; 
      String str = Integer.toString(r); 
      currentNum = ""; 
      currentNum = str; 
      label.setText(str); 
      repaint(); 
     } 
    } 

    public class StartButtonHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      timer.start(); 
     } 
    } 

    public class StopButtonHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      timer.stop(); 
     } 
    } 

    private class ExitButtonHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      System.exit(0); 
     } 
    } 

    public static void main(String[] args){ 
     TimerClassPanel timerPanel = new TimerClassPanel(); 
     JOptionPane.showMessageDialog(null, "Type your guess (int between 1-9)" + 
       " in the field then press 'ENTER'"); 
    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 
     if(e.getX() > 150 && e.getX() < 300 && e.getY() > 0 && e.getY() < 30) 
     { 
      textField.setText(""); 
      repaint(); 
     } 
    } 

    @Override 
    public void mouseEntered(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseExited(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mousePressed(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseReleased(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
} 
+1

*「這是整個代碼:」'整個'類將需要導入。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-04-13 07:28:15

回答

23

TL; DR

反正註冊MouseAdapter和壓倒一切的mouseClicked工作對我來說,

import java.awt.FlowLayout; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class ClickAndClearDemo { 
    private static void createAndShowGUI(){ 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20)); 

     final JTextField textField = new JTextField("Enter text here..."); 
     textField.addMouseListener(new MouseAdapter(){ 
      @Override 
      public void mouseClicked(MouseEvent e){ 
       textField.setText(""); 
      } 
     }); 

     frame.add(textField); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      @Override 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

我希望這個例子得到你開始了正確的方向!

+0

謝謝!這對我很好! – 2012-04-13 00:18:05

+0

@JimHalpert,不客氣,吉姆。 – user1329572 2012-04-13 00:22:24

+0

@JimHalpert,如果這個答案有幫助,請[接受它](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – user1329572 2012-04-13 10:18:27

3

這對我有用。當然,單擊時文本會被清除,並且您可以輸入新文本。要通過點擊再次清除文本,文本框必須失去焦點,然後重新獲得鼠標的焦點。我不完全確定你在這裏尋找什麼。

import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.JFrame; 
import javax.swing.JTextField; 

public class ClickTextField extends JTextField implements MouseListener{ 

public static void main(String[] args) { 
    new ClickTextField(); 
} 

public ClickTextField() { 
    addMouseListener(this); 

    JFrame J = new JFrame(); 
    J.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    J.setSize(100,100); 
    J.getContentPane().add(this); 
    setText("Texty text..."); 
    J.show(); 
} 

@Override 
public void mouseClicked(MouseEvent e) { 

    setText(""); 

} 

@Override 
public void mousePressed(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseReleased(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseEntered(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseExited(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

} 
+0

子類化是次優的,如果你不喜歡不添加真正的新功能(又名:不能通過配置平易近人)。暴露公共API並不意味着被公開使用是OO世界中的罪惡:_) – kleopatra 2013-07-05 15:37:14

8

您可以簡單地添加一個FocusListener到文本框。

final JTextField textField = new JTextField("Enter text here..."); 
    textField.addFocusListener(new FocusListener(){ 
     @Override 
     public void focusGained(FocusEvent e){ 
      textField.setText(""); 
     } 
    }); 
2

是否清除'提示'文本?

我想這是你想要做什麼......

textField.addMouseListener(new MouseAdapter()) 
    { 
     public void mouseClicked(MouseEvent e) 
     { 
      if(textField.getText().equals("Default Text")) 
      { 
       textField.setText(""); 
       repaint(); 
       revalidate(); 
      }   
     } 
    }); 
0

我不得不做這一點。我所做的只是做一個自定義JTextField。喜歡的東西:

import javax.swing.JMenuItem; 
import javax.swing.JPopupMenu; 
import javax.swing.JTextField; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

@SuppressWarnings("serial") 
public class InputField extends JTextField implements MouseListener,ActionListener 
{ 
public InputField(String text) 
{ 
    super(text); 
    super.setHorizontalAlignment(RIGHT); 
    super.addMouseListener(this); 
} 

@Override 
public void mouseClicked(MouseEvent e) 
{ 
    // TODO Auto-generated method stub 
    if (getText().equals("0.0")) 
    { 
     setText(""); 
    } 
} 

@Override 
public void mouseEntered(MouseEvent e) 
{ 

} 

@Override 
public void mouseExited(MouseEvent e) 
{ 

} 

@Override 
public void mousePressed(MouseEvent e) { 
    maybeShowPopup(e); 
    // if the mouse is pressed and "0.0" is the text, we erase the text 
    if (getText().equals("0.0")) 
    { 
     setText(""); 
    } 
} 

@Override 
public void mouseReleased(MouseEvent e) 
{ 
    maybeShowPopup(e); 
} 

private void maybeShowPopup(MouseEvent event) 
{ 
    //if the user clicked the right mouse button 
    if (javax.swing.SwingUtilities.isRightMouseButton(event)) 
    { 
     //create (and show) the popup 
     createPopup().show(event.getComponent(), event.getX(), event.getY()); 
    } 
} 

private JPopupMenu createPopup() 
{ 
    JPopupMenu popupMenu = new JPopupMenu(); 
    //add the clearTextOption to the JPopupMenu 
    JMenuItem clearTextOption = new JMenuItem("Clear text"); 
    clearTextOption.addActionListener(this); 
    popupMenu.add(clearTextOption); 
    return popupMenu; 
} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    //clear the TextField 
    setText(""); 
} 

} //end custom TextField 

在此自定義文本字段,我只是用一個MouseListener。製作一個自定義的優點是:

  1. 我可以把它直接實現的MouseListener(而不必使用一些容易混淆的匿名內部類)
  2. 我可以進行自定義的crapton(包括用戶的選擇右鍵單擊TextField並從PopupMenu中選擇一個項目//我正在處理用戶的複製,粘貼和拖放選項
  3. 我可以在不佔用主文件夾.java的情況下完成所有操作有額外的代碼,可以讓更多的東西在後面挖掘儘管MikeWarren.getAnswer(this) extends richard.getAnswer(this),我想我會詳細說明一點,並顯示我在其中一個實際使用的代碼 程式。
+0

1.是錯誤的:從不公開使用的API不會公開2.不安裝彈出窗口的方式是setComponentPopupMenu,所有textComponents已經有複製/粘貼/剪切操作,可以在彈出窗口中使用3.聽起來像是你連接了太多東西:從視圖中分離數據將清除大部分「擁擠」 - 總結:沒有理由到目前爲止的子類:-) – kleopatra 2013-07-05 15:34:11

0

public JTextField userInput;

執行文本之後:

userInput.setText( 「」); //空

這應該做。

0
jTextField2.addMouseListener(new MouseListener() { 
      @Override 
      public void mouseClicked(MouseEvent e) { 
       if (e.getButton()==1) { 
        jTextField2.setText(""); 
       }//3 = for right click 
       //2 for middlemouse 
      } 

      @Override 
      public void mousePressed(MouseEvent e) { 

      } 

      @Override 
      public void mouseReleased(MouseEvent e) { 

      } 

      @Override 
      public void mouseEntered(MouseEvent e) { 

      } 

      @Override 
      public void mouseExited(MouseEvent e) { 

      } 
     }); 

你也可以嘗試這種方法。