我有兩個文本框和我不知道如何或在哪裏插入行代碼時,它的點擊在清除文本框。文本框時獲得焦點
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 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);
}
}