一個有趣Java小案例,練練手,目前還是初級版本,還在升級當中哦…(附實現代碼在下面)
老闆,這是你要撥打的電話嗎?
實現的代碼在這
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.CardLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLayeredPane;
import javax.swing.JTextField;
import java.awt.Font;
public class Dial extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Dial frame = new Dial();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Dial() {
setTitle("\u62E8\u53F7");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 496, 300);
getContentPane().setLayout(null);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(89, 117, 1, 1);
getContentPane().add(layeredPane);
JButton btnNewButton = new JButton("\u662F");
btnNewButton.setBounds(22, 142, 83, 48);
getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("\u8FD9\u662F\u4F60\u8981\u62E8\u6253\u7684\u7535\u8BDD\u5417\uFF1F");
lblNewLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 20));
lblNewLabel.setBounds(23, 69, 219, 63);
getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel(radomPhone());
lblNewLabel_1.setFont(new Font("微軟雅黑", Font.BOLD, 21));
lblNewLabel_1.setBounds(252, 85, 199, 28);
getContentPane().add(lblNewLabel_1);
JButton btnNewButton_1 = new JButton("\u4E0D\u662F");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//將隨機手機號碼給標籤
lblNewLabel_1.setText(radomPhone());
}
});
btnNewButton_1.setBounds(115, 142, 83, 48);
getContentPane().add(btnNewButton_1);
}
//抽取11位隨機數的手機號碼
public String radomPhone() {
Random r = new Random();
String text = "";
for(int i = 0;i<11;i++) {
//隨機數位數由循環決定,而隨機的數字大小由r.nextInt決定
text += r.nextInt(10);
}
return text;
}
}
我是一顆剽悍的種子(一顆偏愛前端的後端新司機)
最近打算做——“破公司上班的最後一天,老闆讓我給他開發一個撥號程序”這一個系列,想讓學Java路上也能好玩和有趣!
老闆,可不要忘了收藏、點贊和關注喲~