其中应用到JFrame窗体,以及JButton组件、JPanel面板等。以及事件监听实现。这次用到了ActionEvent动作事件。
代码实现:用内部类实现时间监听:
public class JSQ {
public static void main(String[] args) {
SwingUtilities.invokeLater(JSQ::Caculater);
}
private static void Caculater() {
JFrame jf;
JButton[] jb=new JButton[10];
JPanel p1;
jf=new JFrame("整数加法计算器");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton add=new JButton("加");
JButton equal=new JButton("等于");
JButton clearshow =new JButton("清空");
p1=new JPanel();
p1.setLayout(new GridLayout(4,3,5,5));
String str="0123456789";
for (int i = 0; i < jb.length; i++) {
jb[i]=new JButton(str.charAt(i)+"");
int finalI = i;
jb[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String content=clearshow.getText();
if(content.equals("清空")) {
//这是第一个数得输入;
content=""+ finalI;
} else {
content+= finalI;
}
clearshow.setText(content);
}
});
p1.add(jb[i]);
}
//add得监听器
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String content=clearshow.getText();
content+="+";
clearshow.setText(content);
}
});
p1.add(add);
equal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String formula=clearshow.getText();
String[] nums=formula.split("\+");
int result=0;
for (String num : nums) {
result += Integer.parseInt(num);
}
formula+="="+result;
clearshow.setText(formula);
}
catch(Exception ex) {
jf.setTitle("输入错误,请按长按钮清空");
}
}
});
p1.add(equal);
clearshow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearshow.setText("清除");
}
});
jf.add(clearshow,BorderLayout.PAGE_START);
jf.add(p1);
jf.setBounds(100,100,350,250);
jf.setVisible(true);
}
}
用接口实现时间监听
public class hw_1 extends JFrame implements ActionListener{
JButton numButton[]=new JButton[10];
JButton add=new JButton("加");
JButton equal=new JButton("等于");
JButton clear_show =new JButton("清空");
public hw_1(){
JPanel center=new JPanel();
//网格布局;
center.setLayout(new GridLayout(4, 3));
for(int i=0;i<10;i++) {
numButton[i]=new JButton(""+i);
numButton[i].addActionListener(this);
center.add(numButton[i]);
}
center.add(add);
add.addActionListener(this);
center.add(equal);
//注册监听器l;
equal.addActionListener(this);
clear_show.addActionListener(this);
add(clear_show,BorderLayout.NORTH);
add(center,BorderLayout.CENTER);
setTitle("整数加法器");
setLocation(100, 100);
setSize(350, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new hw_1();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton curr=(JButton) e.getSource();
if(curr==clear_show) {
clear_show.setText("清空");
}
for(int i=0;i<10;i++) {
if(curr==numButton[i]) {
String content=clear_show.getText();
if(content.equals("清空")) {
//这是第一个数得输入;
content=""+i;
} else {
content+=i;
}
clear_show.setText(content);
}
}
if(curr==add) {
String content=clear_show.getText();
content+="+";
clear_show.setText(content);
}
if(curr==equal) {
try {
String formula=clear_show.getText();
String[] nums=formula.split("\+");
int result=0;
for (String num : nums) {
result += Integer.parseInt(num);
}
formula+="="+result;
clear_show.setText(formula);
}
catch(Exception ex) {
setTitle("输入错误,请按长按钮清空");
}
}
}
}
运行结果
总结
最近太忙,不过还是会抽空来更新的谢谢大家的支持!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)