我有两个文本框,我不确定如何或在何处插入一行代码,以便在单击时清除文本框。
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);
}
}
答案 0 :(得分:2)
对于每个文本框,您可以像这样创建FocusListener:
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("");
}
});
答案 1 :(得分:1)
你需要FocusListener。查看this example。
答案 2 :(得分:1)
使用:
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) {
}
});
答案 3 :(得分:1)
我认为selectAll更为性感,而不是将文本设置为“”,因为用户在按下新数字之前不会丢失之前的值。
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();
}
});