gpt4 book ai didi

java - 接收焦点时清除文本框

转载 作者:行者123 更新时间:2023-12-01 17:36:53 26 4
gpt4 key购买 nike

我有两个文本框,但我不确定如何或在哪里插入一行代码,以便在单击文本框时清除该文本框。

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);
}
}

最佳答案

对于每个文本框,您可以像这样创建 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("");
}
});

关于java - 接收焦点时清除文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5382436/

26 4 0
文章推荐: java正则表达式
文章推荐: java - 在对象上应用新函数
文章推荐: java - float 字
文章推荐: java 泛型 - 将 List 转换为 List