導入Java . awt . event . *;
導入javax . swing . *;
公共類CaseConverter擴展JFrame {
私有JTextField inputField
私有JTextField outputField
私有JButton capitalButton
private JButton lowercaseButton
public CaseConverter() {
//設置窗口標題
setTitle(“大小寫轉換器”);
//將窗口布局設置為FlowLayout。
set layout(new flow layout());
//創建壹個組件
input field = new JTextField(20);
output field = new JTextField(20);
Capital button = new JButton(" Capital ");
lowercaseButton = new JButton("小寫");
//將組件添加到窗口
add(input field);
add(output field);
add(capital button);
add(lower casebutton);
//向按鈕添加壹個press事件偵聽器
capital button . addactionlistener(new capital listener());
lowercasebutton . addactionlistener(新LowercaseListener());
}
//為大寫按鈕定義press事件偵聽器。
私有類CapitalListener實現ActionListener {
public void action performed(action event e){
//從輸入文本框中獲取輸入字符串
string input = input field . gettext();
//將字符串中的大寫字母轉換為小寫字母,其他字符保持不變。
string output = input . tolowercase();