一个有趣Java小案例,练练手,初级版本链接放下面,目前已经是升级版本,还有终极版本哦…所以要点赞和关注哟(附实现代码在下面)
在破公司上班的最后一天,老板让我给他开发一个拨号程序——初级版(Java实现)
完整代码在这(特别提醒:代码在浏览器看没问题,但是在CSDN里看格式可能会错)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLayeredPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionEvent;
public class Dial_Upgrade extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JLabel lblNewLabel_2;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
private static int i = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Dial_Upgrade frame = new Dial_Upgrade();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Dial_Upgrade() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLayeredPane layeredPane = new JLayeredPane();
contentPane.add(layeredPane, BorderLayout.CENTER);
textField = new JTextField();
textField.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField.setBounds(25, 94, 42, 54);
layeredPane.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("\u91CD\u65B0\u542F\u52A8");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField_1.setText("");
textField_2.setText("");
textField_3.setText("");
textField_4.setText("");
textField_5.setText("");
textField_6.setText("");
textField_7.setText("");
textField_8.setText("");
textField_9.setText("");
textField_10.setText("");
i=0;
}
});
btnNewButton.setBounds(25, 183, 165, 35);
layeredPane.add(btnNewButton);
JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u4F60\u7684\u624B\u673A\u53F7\u7801\uFF1A");
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
lblNewLabel.setBounds(25, 35, 225, 35);
layeredPane.add(lblNewLabel);
JButton btnNewButton_1 = new JButton("\u62E8\u53F7");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i++;
Timer timer = new Timer();
timer.schedule(new Dial_Number(),100);
}
});
btnNewButton_1.setBounds(208, 183, 165, 35);
layeredPane.add(btnNewButton_1);
textField_1 = new JTextField();
textField_1.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_1.setColumns(10);
textField_1.setBounds(71, 94, 42, 54);
layeredPane.add(textField_1);
textField_2 = new JTextField();
textField_2.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_2.setColumns(10);
textField_2.setBounds(117, 94, 42, 54);
layeredPane.add(textField_2);
textField_3 = new JTextField();
textField_3.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_3.setColumns(10);
textField_3.setBounds(164, 94, 42, 54);
layeredPane.add(textField_3);
textField_4 = new JTextField();
textField_4.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_4.setColumns(10);
textField_4.setBounds(210, 94, 42, 54);
layeredPane.add(textField_4);
textField_5 = new JTextField();
textField_5.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_5.setColumns(10);
textField_5.setBounds(256, 94, 42, 54);
layeredPane.add(textField_5);
JLabel lblNewLabel_1 = new JLabel("-");
lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_1.setBounds(157, 112, 18, 16);
layeredPane.add(lblNewLabel_1);
lblNewLabel_2 = new JLabel("-");
lblNewLabel_2.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_2.setBounds(299, 112, 18, 16);
layeredPane.add(lblNewLabel_2);
textField_6 = new JTextField();
textField_6.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_6.setColumns(10);
textField_6.setBounds(306, 94, 42, 54);
layeredPane.add(textField_6);
textField_7 = new JTextField();
textField_7.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_7.setColumns(10);
textField_7.setBounds(352, 94, 42, 54);
layeredPane.add(textField_7);
textField_8 = new JTextField();
textField_8.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_8.setColumns(10);
textField_8.setBounds(398, 94, 42, 54);
layeredPane.add(textField_8);
textField_9 = new JTextField();
textField_9.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_9.setColumns(10);
textField_9.setBounds(443, 94, 42, 54);
layeredPane.add(textField_9);
textField_10 = new JTextField();
textField_10.setFont(new Font("微软雅黑", Font.BOLD, 20));
textField_10.setColumns(10);
textField_10.setBounds(489, 94, 42, 54);
layeredPane.add(textField_10);
}
public class Dial_Number extends TimerTask{
Random r = new Random();
String text = "";
@Override
public void run() {
if(i == 1) {
while(2>i) {
text = " " + r.nextInt(10);
textField.setText(text);
}
}
if(i == 2) {
while(3>i) {
text = " " + r.nextInt(10);
textField_1.setText(text);
}
}
if(i == 3) {
while(4>i) {
text = " " + r.nextInt(10);
textField_2.setText(text);
}
}
if(i == 4) {
while(5>i) {
text = " " + r.nextInt(10);
textField_3.setText(text);
}
}
if(i == 5) {
while(6>i) {
text = " " + r.nextInt(10);
textField_4.setText(text);
}
}
if(i == 6) {
while(7>i) {
text = " " + r.nextInt(10);
textField_5.setText(text);
}
}
if(i == 7) {
while(8>i) {
text = " " + r.nextInt(10);
textField_6.setText(text);
}
}
if(i == 8) {
while(9>i) {
text = " " + r.nextInt(10);
textField_7.setText(text);
}
}
if(i == 9) {
while(10>i) {
text = " " + r.nextInt(10);
textField_8.setText(text);
}
}
if(i == 10) {
while(11>i) {
text = " " + r.nextInt(10);
textField_9.setText(text);
}
}
if(i == 11) {
while(12>i) {
text = " " + r.nextInt(10);
textField_10.setText(text);
}
}
}
}
}
我是一颗剽悍的种子(一颗偏爱前端的后端新司机)
最近打算做——“破公司上班的最后一天,老板让我给他开发一个拨号程序”这一个系列,已经从初级版本到现在的升级版,还有一个终极版本,嘻嘻,想让学Java路上也能好玩和有趣!
老板,可不要忘了收藏、点赞和关注哟~