开发者

Text box cleared when receives focus

I have two text boxes and I'm not sure how or where to insert a line of code that clears the text box when it's clicked in.

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

public class OfficeAreaCalculator extends JFrame implements ActionListener{
  private JTabbedPane jtabbedPane;
  private JPanel calculator;


  JTextField lengthText, widthText, areaText;

  public OfficeAreaCalculator(){
    setTitle("Office Area Calculator");


    JPanel topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );

    createcalculator();

    jtabbedPane = new JTabbedPane();
    jtabbedPane.addTab("Office", calculator);
    topPanel.add(jtabbedPane, BorderLayout.CENTER);
  }


  public void createcalculator(){
    calculator = new JPanel();
    calculator.setLayout( null );
    JLabel lengthLabel = new JLabel("Enter the length of the office:");
    lengthLabel.setBounds(12, 15, 260, 20);
    calculator.add( lengthLabel );
    lengthText = new JTextField();
    lengthText.setBounds(180, 15, 80, 20);
    calculator.add( lengthText );
    JLabel widthLabel = new JLabel("Enter the width of the office:");
    widthLabel.setBounds(15, 40, 260, 20);
    calculator.add( widthLabel );
    widthText = new JTextField();
    widthText.setBounds(180, 40, 80, 20);
    calculator.add( widthText );

    JLabel areaLabel = new JLabel("Area of the 开发者_StackOverflow社区Office is:");
    areaLabel.setBounds(30, 70, 260, 20);
    calculator.add( areaLabel );
    areaText = new JTextField();
    areaText.setBounds(150, 70, 120, 20);
    areaText.setEditable(false);
    calculator.add(areaText);

    JButton calcarea = new JButton("Calculate");
    calcarea.setBounds(20,110,110,20);
    calcarea.addActionListener(this);
    calcarea.setBackground(Color.white);
    calculator.add(calcarea);

    JButton Close = new JButton("Close");
    Close.setBounds(150,110,80,20);
    Close.addActionListener(this);
    Close.setBackground(Color.white);
    calculator.add(Close);
  }


  public void actionPerformed(ActionEvent event){
    JButton button = (JButton)event.getSource();
    String buttonLabel = button.getText();
    if ("Close".equalsIgnoreCase(buttonLabel)){
      Exit_pressed(); return;
    }
    if ("Calculate".equalsIgnoreCase(buttonLabel)){
      Calculate_area(); return;
    }

  }

  private void Exit_pressed(){
    System.exit(0);
  }
  private void Calculate_area(){
    String lengthString, widthString;
    int length=0;
    int width=0;
    lengthString = lengthText.getText();
    widthString = widthText.getText();
    if (lengthString.length() < 1 || widthString.length() < 1 ){
      areaText.setText("Enter All Numbers"); return;
    }
    length = Integer.parseInt(lengthString);
    width = Integer.parseInt(widthString);
    if (length != 0 || width != 0){
      areaText.setText((length * width) + "");
    } else{
      areaText.setText("Enter All Numbers"); return;
    }
  }

  public static void main(String[] args){
    JFrame frame = new OfficeAreaCalculator();
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}


for each textbox you can create FocusListener like this :

JTextField textfield = new JTextField();
        textfield.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void focusGained(FocusEvent arg0) {
                // TODO Auto-generated method stub
                textfield.setText("");
            }
        });


You'll need FocusListener for that. Check out this example.


Use:

lengthText = new JTextField();
lengthText.setBounds(180, 15, 80, 20);
lengthText.addFocusListener(new FocusListener(){
    public void focusGained(FocusEvent e) {
        lengthText.setText("");
    }
    public void focusLost(FocusEvent e) {
    }
});


I think it is sexier to selectAll instead of setting text to "", because the user does not loose the previous value until she presses a new digit.

    lengthText = new JTextField();
    lengthText.setBounds(180, 15, 80, 20);
    lengthText.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void focusGained(FocusEvent arg0) {
            // TODO Auto-generated method stub
            lengthText.selectAll();
        }
    }); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜