使用JCreator
执行出来和图片一模一样(为了凑字无关紧要帮帮忙回到家电脑百度低等级觉得就到家大家觉得简单记得记得觉得就到家大家都)
希望采纳一下
import javax.swing.*;
public class MyFrame extends JFrame {
MyFrame(String title) {
super(title);
//添加容器
JPanel panel = new JPanel();
setContentPane(panel);
panel.setLayout(null);
JLabel l1 = new JLabel("输入数值n:");
JLabel l2 = new JLabel("在此处显示1~n能被5整除的个数");
JTextField t1 = new JTextField(20);
JButton b1 = new JButton("确定");
JButton b2 = new JButton("取消");
l1.setBounds(20,5,80,30);
t1.setBounds(100,12,120,20);
b1.setBounds(110,37,60,25);
b2.setBounds(180,37,60,25);
l2.setBounds(20,60,300,25);
panel.add(l1);
panel.add(t1);
panel.add(b1);
panel.add(b2);
panel.add(l2);
//添加监听器
b1.addActionListener((e)->{
int cnt =0;
int a = Integer.parseInt(t1.getText());
for(int i=1;i<a;i++){
if(i%5==0){
cnt++;
}
}
l2.setText("1~" + a + "能被5整除的数有" + cnt + "个");
});
b2.addActionListener((e)->{
System.exit(0);
});
//设置窗口风格
setDefaultCloseOperation(EXIT_ON_CLOSE);
//设置窗口显示位置和大小
setBounds(400, 300, 400, 200);
//设置窗口可见
setVisible(true);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame("计算");
}
}