Ho bisogno di fare in modo che questo programma cancelli il testo dal campo di testo quando il mouse fa clic in quel campo di testo. Ho provato alcune cose, ma nessuna di esse deve ancora funzionare per me.Come cancellare JTextField quando il mouse fa clic su JTextField
Ecco il codice nella sua interezza:
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
}
}
* "Ecco il codice nella sua interezza:" La classe "intera" avrebbe bisogno di importazioni.Per un aiuto migliore prima, pubblica un [SSCCE] (http://sscce.org/). –