📄 dogyears.java
字号:
package myPackage;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class DogYears extends JFrame {
private static final long serialVersionUID = 1L;
private static final int DOG_YEARS_PER_HUMANYEARS=10;
private JTextField humanYearsTF = new JTextField(3);
private JTextField dogYearsTF = new JTextField(3);
DogYears(){
JButton convertButton = new JButton("Convert");
convertButton.addActionListener(new ConverBtnlistener());
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(new JLabel("Dog Years"));
content.add(dogYearsTF);
content.add(convertButton);
content.add(new JLabel("Human Years"));
content.add(humanYearsTF);
setContentPane(content);
pack();
setTitle("Convertor--Dog years to Human years");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
class ConverBtnlistener implements ActionListener{
public void actionPerformed(ActionEvent e){
String dyString = dogYearsTF.getText();
int dogYears = Integer.parseInt(dyString);
int humanYears = dogYears*DOG_YEARS_PER_HUMANYEARS;
humanYearsTF.setText("" +humanYears);
}
}
/* private class GUIactionListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String actionCmd = event.getActionCommand();
if(actionCmd.equals("Convert")){
System.out.println("Converted...");
return;
}
if(actionCmd.equals("add")){
System.out.println("add...");
return;
}
This is same old way for the listener not practical
}
}*/
public static void main(String[] args) {
DogYears window = new DogYears();
window.setSize(150, 200);
window.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -