抽奖
java版本
读入txt文件和学生照片,随机选择学生。
效果图
application.java
package lottery2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Application {
public static void main(String[] args) {
int index = 0;
ArrayList<Person> stuInfo = new ArrayList<Person>();
String lineTxt = null;
String[] tempString = null;
Person tempPerson = null;
// 读取学生信息
try {
File myFile = new File("src/information/stu2.txt");
if (myFile.isFile() && myFile.exists()) {
InputStreamReader Reader = new InputStreamReader(new FileInputStream(myFile), "UTF-8");
// 考虑到编码格式,new FileInputStream(myFile)文件字节输入流,以字节为单位对文件中的数据进行读取
// new InputStreamReader(FileInputStream a, "编码类型")
// 将文件字节输入流转换为文件字符输入流并给定编码格式
BufferedReader bufferedReader = new BufferedReader(Reader);
// BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
// 通过BuffereReader包装实现高效读取
while ((lineTxt = bufferedReader.readLine()) != null) {
// buffereReader.readLine()按行读取写成字符串
stuInfo.add(new Person());
tempString = lineTxt.split("\t");
tempPerson = stuInfo.get(index);
tempPerson.setName(tempString[0]);
tempPerson.setBirthday(tempString[1]);
index++;
}
Reader.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
// TODO: handle exception
}
// 读取学生照片
File file = new File("src/images/");
File[] files = file.listFiles();
ImageIcon[] imgs = new ImageIcon[files.length];
for (int n = 0; n < files.length; n++) {
String str = files[n].getPath();
imgs[n] = new ImageIcon(str);
}
// 创建GUI,并传入学生信息的ArrayList
new LotteryFrame(stuInfo, imgs);
}
}
Person.java
package lottery2;
public class Person {
private String birthday;
private String name;
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
LotteryFrame.java
package lottery2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class LotteryFrame extends JFrame {
int randomId = 0;
int flag = 0;
JPanel otherPanel;
JLabel myImage;
JButton randomButton;
JLabel birthLabel;
JLabel nameLabel;
JLabel formerBirthLabel;
JLabel formerNameLabel;
Timer timer;
ArrayList<Person> stuInfo = new ArrayList<Person>();
ImageIcon[] imgs;
//构造方法
LotteryFrame(ArrayList<Person> stuInfo, ImageIcon[] imgs) {
this.imgs = imgs;
this.stuInfo = stuInfo;
myImage = new JLabel(imgs[0]);
otherPanel = new JPanel();
//用于调整布局的两个虚无button
JButton c = new JButton(" ");
JButton d = new JButton(" ");
c.setContentAreaFilled(false);
c.setBorderPainted(false);
c.setEnabled(false);
d.setContentAreaFilled(false);
d.setBorderPainted(false);
d.setEnabled(false);
//设置label和button样式
formerBirthLabel = new JLabel("学号:");
birthLabel = new JLabel("王嘉尔");
nameLabel = new JLabel("19940328");
formerNameLabel = new JLabel("姓名:");
birthLabel.setFont(new Font("隶书", Font.PLAIN, 36));
birthLabel.setForeground(Color.RED);
nameLabel.setFont(new Font("隶书", Font.PLAIN, 36));
nameLabel.setForeground(Color.RED);
randomButton = new JButton("随机选择");
randomButton.setFont(new Font("隶书", Font.PLAIN, 36));
formerBirthLabel.setFont(new Font("隶书", Font.PLAIN, 36));
formerNameLabel.setFont(new Font("隶书", Font.PLAIN, 36));
//添加组件
otherPanel.add(formerNameLabel);
otherPanel.add(nameLabel);
otherPanel.add(d);
otherPanel.add(randomButton);
otherPanel.add(c);
otherPanel.add(formerBirthLabel);
otherPanel.add(birthLabel);
//关于Frame的一些设置
this.getContentPane().add(myImage, BorderLayout.CENTER);
this.getContentPane().add(otherPanel, BorderLayout.SOUTH);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//设置 timer
timer = new Timer(150, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
randomId = (int) (Math.random() * stuInfo.size());
myImage.setIcon(imgs[randomId]);
Person temPerson = stuInfo.get(randomId);
birthLabel.setText(temPerson.getBirthday());
nameLabel.setText(temPerson.getName());
}
});
//按钮点击事件
randomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (flag == 0) {
timer.start();
flag = 1;
} else {
timer.stop();
// Person temPerson = stuInfo.get(randomId);
// birthLabel.setText(temPerson.getBirthday());
// nameLabel.setText(temPerson.getName());
flag = 0;
}
}
});
}
}
网页版
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>抽奖作业</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<aside class="profile-card">
<header>
<!-- here’s the avatar -->
<a target="_blank" href="#">
<img id= "changeImg" src="1.jpg" class="hoverZoomLink">
</a>
<!-- the username -->
<h1 id= "changeWord">
王嘉尔
</h1>
<!-- and role or location -->
<h2>
</h2>
</header>
<!-- bit of a bio; who are you? -->
<div class="profile-bio">
<br>
<button class="button" onclick="control()" >Click Here</button>
</div>
<!-- some social links to show off -->
<ul class="profile-social-links">
<li>
<a target="_blank" href="">
<i class="fa fa-facebook"></i>
</a>
</li>
<li>
<a target="_blank" href="">
<i class="fa fa-twitter"></i>
</a>
</li>
<li>
<a target="_blank" href="">
<i class="fa fa-github"></i>
</a>
</li>
<li>
<a target="_blank" href="">
<i class="fa fa-behance"></i>
</a>
</li>
</ul>
</aside>
</body>
<script>
var flag = 0;
var int;
function control(){
if(flag == 0){
int=self.setInterval("change()",150);
flag = 1;
}else{
int=window.clearInterval(int);
flag = 0;
}
}
// 首先,定义一个数组,用于存放所有的背景图片
var imgArr = new Array();
// 将所有背景图放入arr内
imgArr.push('1.jpg', '2.jpg', '3.jpg',"4.jpg","5.jpg","6.jpg");
var inform=[];
var inform=new Array("王嘉尔", "梅艳芳", "刘德华","古天乐","张国荣","郭富城");
function change() {
var index =parseInt(Math.random()*(imgArr.length-1));
var currentImage=imgArr[index];
var currentWord=inform[index];
document.getElementById("changeImg").src=currentImage;
document.getElementById("changeWord").innerText=currentWord;
flag = 1;
};
</script>
</html>
css
html {
height: 100%;
}
body {
overflow: hidden;
background: #bcdee7 url("../img/bg.jpg") no-repeat center center fixed;
background-size: cover;
position: fixed;
padding: 0px;
margin: 0px;
width: 100%;
height: 100%;
font: normal 14px/1.618em "Roboto", sans-serif;
-webkit-font-smoothing: antialiased;
}
body:before {
content: "";
height: 0px;
padding: 0px;
border: 130em solid #313440;
position: absolute;
left: 50%;
top: 100%;
z-index: 2;
display: block;
-webkit-border-radius: 50%;
border-radius: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
-webkit-animation: puff 0.5s 1.8s cubic-bezier(0.55, 0.055, 0.675, 0.19) forwards, borderRadius 0.2s 2.3s linear forwards;
animation: puff 0.5s 1.8s cubic-bezier(0.55, 0.055, 0.675, 0.19) forwards, borderRadius 0.2s 2.3s linear forwards;
}
h1,
h2 {
font-weight: 500;
margin: 0px 0px 5px 0px;
}
h1 {
font-size: 24px;
}
h2 {
font-size: 16px;
}
p {
margin: 0px;
}
.profile-card {
background: #FFB300;
width: 56px;
height: 56px;
position: absolute;
left: 50%;
top: 50%;
z-index: 2;
overflow: hidden;
opacity: 0;
margin-top: 70px;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
-webkit-border-radius: 50%;
border-radius: 50%;
-webkit-box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16), 0px 3px 6px rgba(0, 0, 0, 0.23);
box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16), 0px 3px 6px rgba(0, 0, 0, 0.23);
-webkit-animation: init 0.5s 0.2s cubic-bezier(0.55, 0.055, 0.675, 0.19) forwards, moveDown 1s 0.8s cubic-bezier(0.6, -0.28, 0.735, 0.045) forwards, moveUp 1s 1.8s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards, materia 0.5s 2.7s cubic-bezier(0.86, 0, 0.07, 1) forwards;
animation: init 0.5s 0.2s cubic-bezier(0.55, 0.055, 0.675, 0.19) forwards, moveDown 1s 0.8s cubic-bezier(0.6, -0.28, 0.735, 0.045) forwards, moveUp 1s 1.8s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards, materia 0.5s 2.7s cubic-bezier(0.86, 0, 0.07, 1) forwards;
}
.profile-card header {
width: 179px;
height: 280px;
padding: 40px 20px 30px 20px;
display: inline-block;
float: left;
border-right: 2px dashed #EEEEEE;
background: #FFFFFF;
color: #000000;
margin-top: 50px;
opacity: 0;
text-align: center;
-webkit-animation: moveIn 1s 3.1s ease forwards;
animation: moveIn 1s 3.1s ease forwards;
}
.profile-card header h1 {
color: #FF5722;
}
.profile-card header a {
display: inline-block;
text-align: center;
position: relative;
margin: 25px 30px;
}
.profile-card header a:after {
position: absolute;
content: "";
bottom: 3px;
right: 3px;
width: 20px;
height: 20px;
border: 4px solid #FFFFFF;
-webkit-transform: scale(0);
transform: scale(0);
background: -webkit-linear-gradient(top, #2196F3 0%, #2196F3 50%, #FFC107 50%, #FFC107 100%);
background: linear-gradient(#2196F3 0%, #2196F3 50%, #FFC107 50%, #FFC107 100%);
-webkit-border-radius: 50%;
border-radius: 50%;
-webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
-webkit-animation: scaleIn 0.3s 3.5s ease forwards;
animation: scaleIn 0.3s 3.5s ease forwards;
}
.profile-card header a > img {
width: 170px;
max-width: 100%;
-webkit-border-radius: 50%;
border-radius: 50%;
-webkit-transition: -webkit-box-shadow 0.3s ease;
transition: box-shadow 0.3s ease;
-webkit-box-shadow: 0px 0px 0px 8px rgba(0, 0, 0, 0.06);
box-shadow: 0px 0px 0px 8px rgba(0, 0, 0, 0.06);
}
.profile-card header a:hover > img {
-webkit-box-shadow: 0px 0px 0px 12px rgba(0, 0, 0, 0.1);
box-shadow: 0px 0px 0px 12px rgba(0, 0, 0, 0.1);
}
.profile-card .profile-bio {
width: 175px;
height: 180px;
display: inline-block;
float: right;
padding: 50px 20px 30px 20px;
background: #FFFFFF;
color: #333333;
margin-top: 50px;
text-align: center;
opacity: 0;
-webkit-animation: moveIn 1s 3.1s ease forwards;
animation: moveIn 1s 3.1s ease forwards;
}
.profile-social-links {
width: 218px;
display: inline-block;
float: right;
margin: 0px;
padding: 15px 20px;
background: #FFFFFF;
margin-top: 50px;
text-align: center;
opacity: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-webkit-animation: moveIn 1s 3.1s ease forwards;
animation: moveIn 1s 3.1s ease forwards;
}
.profile-social-links li {
list-style: none;
margin: -5px 0px 0px 0px;
padding: 0px;
float: left;
width: 25%;
text-align: center;
}
.profile-social-links li a {
display: inline-block;
color: red;
width: 24px;
height: 24px;
padding: 6px;
position: relative;
overflow: hidden!important;
-webkit-border-radius: 50%;
border-radius: 50%;
}
.profile-social-links li a i {
position: relative;
z-index: 1;
}
.profile-social-links li a img,
.profile-social-links li a svg {
width: 24px;
}
.button{
background: linear-gradient(135deg, #6e8efb, #a2d7f0);
border: none;
font-size: 28px;
color: #FFFFFF;
padding: 0;
width: 150px;
height: 50px;
text-align: center;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
text-decoration: none;
overflow: hidden;
cursor: pointer;
font-size: 1.5rem;
box-shadow: 0 2px 5px rgba(0, 0, 0, .2);
border-radius: 7px;
}
button:active{
opacity: 0.5;
}
@-webkit-keyframes init {
0% {
width: 0px;
height: 0px;
}
100% {
width: 56px;
height: 56px;
margin-top: 0px;
opacity: 1;
}
}
@keyframes init {
0% {
width: 0px;
height: 0px;
}
100% {
width: 56px;
height: 56px;
margin-top: 0px;
opacity: 1;
}
}
@-webkit-keyframes puff {
0% {
top: 100%;
height: 0px;
padding: 0px;
}
100% {
top: 50%;
height: 100%;
padding: 0px 100%;
}
}
@keyframes puff {
0% {
top: 100%;
height: 0px;
padding: 0px;
}
100% {
top: 50%;
height: 100%;
padding: 0px 100%;
}
}
@-webkit-keyframes borderRadius {
0% {
-webkit-border-radius: 50%;
}
100% {
-webkit-border-radius: 0px;
}
}
@keyframes borderRadius {
0% {
-webkit-border-radius: 50%;
}
100% {
border-radius: 0px;
}
}
@-webkit-keyframes moveDown {
0% {
top: 50%;
}
50% {
top: 40%;
}
100% {
top: 100%;
}
}
@keyframes moveDown {
0% {
top: 50%;
}
50% {
top: 40%;
}
100% {
top: 100%;
}
}
@-webkit-keyframes moveUp {
0% {
background: #FFB300;
top: 100%;
}
50% {
top: 40%;
}
100% {
top: 50%;
background: #E0E0E0;
}
}
@keyframes moveUp {
0% {
background: #FFB300;
top: 100%;
}
50% {
top: 40%;
}
100% {
top: 50%;
background: #E0E0E0;
}
}
@-webkit-keyframes materia {
0% {
background: #E0E0E0;
}
50% {
-webkit-border-radius: 4px;
}
100% {
width: 440px;
height: 280px;
background: #FFFFFF;
-webkit-border-radius: 4px;
}
}
@keyframes materia {
0% {
background: #E0E0E0;
}
50% {
border-radius: 4px;
}
100% {
width: 440px;
height: 280px;
background: #FFFFFF;
border-radius: 4px;
}
}
@-webkit-keyframes moveIn {
0% {
margin-top: 50px;
opacity: 0;
}
100% {
opacity: 1;
margin-top: -20px;
}
}
@keyframes moveIn {
0% {
margin-top: 50px;
opacity: 0;
}
100% {
opacity: 1;
margin-top: -20px;
}
}
@-webkit-keyframes scaleIn {
0% {
-webkit-transform: scale(0);
}
100% {
-webkit-transform: scale(1);
}
}
@keyframes scaleIn {
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
@-webkit-keyframes ripple {
0% {
transform: scale3d(0, 0, 0);
}
50%,
100% {
-webkit-transform: scale3d(1, 1, 1);
}
100% {
opacity: 0;
}
}
@keyframes ripple {
0% {
transform: scale3d(0, 0, 0);
}
50%,
100% {
transform: scale3d(1, 1, 1);
}
100% {
opacity: 0;
}
}
@media screen and (min-aspect-ratio: 4/3) {
body {
background-size: cover;
}
body:before {
width: 0px;
}
@-webkit-keyframes puff {
0% {
top: 100%;
width: 0px;
padding-bottom: 0px;
}
100% {
top: 50%;
width: 100%;
padding-bottom: 100%;
}
}
@keyframes puff {
0% {
top: 100%;
width: 0px;
padding-bottom: 0px;
}
100% {
top: 50%;
width: 100%;
padding-bottom: 100%;
}
}
}
@media screen and (min-height: 480px) {
.profile-card header {
width: auto;
height: auto;
padding: 30px 20px;
display: block;
float: none;
border-right: none;
}
.profile-card .profile-bio {
width: auto;
height: auto;
padding: 15px 20px 30px 20px;
display: block;
float: none;
}
.profile-social-links {
width: 100%;
display: block;
float: none;
}
@-webkit-keyframes materia {
0% {
background: #E0E0E0;
}
50% {
-webkit-border-radius: 4px;
}
100% {
width: 280px;
height: 440px;
background: #FFFFFF;
-webkit-border-radius: 4px;
}
}
@keyframes materia {
0% {
background: #E0E0E0;
}
50% {
border-radius: 4px;
}
100% {
width: 280px;
height: 440px;
background: #FFFFFF;
border-radius: 4px;
}
}
}
使用poi读入ppt文件
效果图
用到的jar包
package ppt;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.poi.hslf.usermodel.SlideShow;
public class Frame extends JFrame {
// 定义图片存储的文件夹位置
public static File imgFile = new File("D:\\eclipse\\work-space\\PowerPoint\\imgs");
File ppt;
static int index = 0;
JButton inputButton;
JButton firstButton;
JButton lastButton;
JButton nextButton;
JButton finalButton;
JPanel myJPanel;
JPanel otherPanel;
JLabel myImage;
ImageIcon[] imgs;
//构造方法
Frame() {
myImage = new JLabel();
otherPanel = new JPanel();
myJPanel = new JPanel();
//设置button样式
inputButton = new JButton("导入文件");
inputButton.setFont(new Font("隶书", Font.PLAIN, 36));
firstButton = new JButton("首页");
firstButton.setFont(new Font("隶书", Font.PLAIN, 36));
lastButton = new JButton("上一页");
lastButton.setFont(new Font("隶书", Font.PLAIN, 36));
nextButton = new JButton("下一页");
nextButton.setFont(new Font("隶书", Font.PLAIN, 36));
finalButton = new JButton("末页");
finalButton.setFont(new Font("隶书", Font.PLAIN, 36));
//添加组件
myJPanel.add(myImage);
otherPanel.add(inputButton);
otherPanel.add(firstButton);
otherPanel.add(lastButton);
otherPanel.add(nextButton);
otherPanel.add(finalButton);
//关于Frame的一些设置
this.getContentPane().add(myJPanel, BorderLayout.CENTER);
myImage.setSize(800, 600);
otherPanel.setSize(800, 200);
this.getContentPane().add(otherPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800, 600);
setVisible(true);
//按钮点击事件
inputButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SlideShow myppt = null;
JFileChooser myChooser = new JFileChooser();
if (myChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
ppt = myChooser.getSelectedFile();
try {
pptTojpg(ppt);
} catch (IOException e1) {
e1.printStackTrace();
}
// 将照片存入imgs
File[] files = imgFile.listFiles();
imgs = new ImageIcon[files.length];
for (int n = 0; n < files.length; n++) {
String str = files[n].getPath();
imgs[n] = new ImageIcon(str);
}
myImage.setIcon(imgs[0]);
index = 0;
} else {
System.out.println("No file selected!");
}
}
});
firstButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myImage.setIcon(imgs[0]);
index = 0;
}
});
lastButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (index == 0) {
index = 0;
} else {
index--;
myImage.setIcon(imgs[index]);
}
}
});
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (index == imgs.length - 1) {
index = imgs.length - 1;
} else {
index++;
myImage.setIcon(imgs[index]);
}
}
});
finalButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myImage.setIcon(imgs[imgs.length - 1]);
index = imgs.length - 1;
}
});
}
public static void pptTojpg(File PPT) throws IOException {
FileInputStream in = new FileInputStream(PPT);
SlideShow myppt = new SlideShow(in);
Dimension pgsize = myppt.getPageSize();
org.apache.poi.hslf.model.Slide[] slide = myppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
String absolutePath = imgFile.getAbsolutePath() + "/" + (i + 1) + ".jpeg";
File jpegFile = new File(absolutePath);
if (jpegFile.exists()) {
continue;
}
FileOutputStream out = new FileOutputStream(jpegFile);
ImageIO.write(img, "jpeg", out);
out.close();
}
}
public static void main(String[] args) {
JFrame myFrame = new Frame();
}
}
模拟时钟
代码(还需要用到一张表盘的底图,需要的话私信或者发邮箱)
package clock;
import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Clock1 extends JComponent {
private static int hour = 0;
private static int min = 0;
private static int sec = 0;
private static int total = 0;
private final double PI = Math.PI;
private Graphics2D g;
private static Date current = new Date();
private JButton currentButton = new JButton("当前时间");
private JButton customizeButton = new JButton("自定义时间");
private JLabel display = new JLabel("00:00:00");
private String timeString = "00:00:00";
private JLabel hourJLabel = new JLabel("时");
private JLabel minJLabel = new JLabel("分");
private JLabel secJLabel = new JLabel("秒");
private JTextField hourField = new JTextField(0);
private JTextField minField = new JTextField(0);
private JTextField secField = new JTextField(0);
public Clock1() {
hour = current.getHours();
min = current.getMinutes();
sec = current.getSeconds();
total = hour * 3600 + min * 60 + sec;
add(currentButton);
currentButton.setBounds(100, 510, 120, 30);
currentButton.setFont(new Font("微软雅黑", Font.PLAIN, 18));
add(display);
display.setBounds(250, 510, 120, 30);
display.setFont(new Font("微软雅黑", Font.BOLD, 18));
add(customizeButton);
customizeButton.setBounds(50, 560, 150, 30);
customizeButton.setFont(new Font("微软雅黑", Font.PLAIN, 18));
add(hourField);
hourField.setBounds(220, 560, 30, 30);
add(hourJLabel);
hourJLabel.setBounds(260, 560, 30, 30);
add(minField);
minField.setBounds(280, 560, 30, 30);
add(minJLabel);
minJLabel.setBounds(320, 560, 30, 30);
add(secField);
secField.setBounds(340, 560, 30, 30);
add(secJLabel);
secJLabel.setBounds(380, 560, 30, 30);
currentButton.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e) {
hour = current.getHours();
min = current.getMinutes();
sec = current.getSeconds();
total = hour * 3600 + min * 60 + sec;
if (myThread.isAlive()) {
} else {
myThread.start();
}
}
});
customizeButton.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e) {
hour = Integer.parseInt(hourField.getText());
min = Integer.parseInt(minField.getText());
sec = Integer.parseInt(secField.getText());
total = hour * 3600 + min * 60 + sec;
if (myThread.isAlive()) {
} else {
myThread.start();
}
}
});
}
public void hourPointer(int second, Graphics2D g) {
double x, y, speed;
speed = second * PI / 21600;
x = 250 + 100 * Math.sin(speed);
y = 233 - 100 * Math.cos(speed);
g.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
g.drawLine(250, 233, (int) x, (int) y);
}
public void minutePointer(int second, Graphics2D g) {
double x, y, speed;
speed = second * PI / 1800;
x = 250 + 130 * Math.sin(speed);
y = 233 - 130 * Math.cos(speed);
g.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
g.drawLine(250, 233, (int) x, (int) y);
}
public void secondPointer(int second, Graphics2D g) {
double x, y, x1, y1, speed;
speed = second * PI / 30;
x = 250 + 170 * Math.sin(speed);
y = 233 - 170 * Math.cos(speed);
g.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
g.drawLine(250, 233, (int) x, (int) y);
}
@SuppressWarnings("deprecation")
public void paintComponent(Graphics g1) {
super.paintComponent(g1);
g = (Graphics2D) g1;
int x = 0, y = 0;
ImageIcon icon = new ImageIcon("E:\\sophomore\\Java\\Java上机\\第四次作业\\clock\\clock01.jpg");
// g.drawImage(icon.getImage(), x, y, getSize().width, getSize().height, this);// 图片会自动缩放
g.drawImage(icon.getImage(), x, y, this);// 图片不会自动缩放
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
hourPointer(total, g);
minutePointer(total % 3600, g);
secondPointer(total % 60, g);
}
@SuppressWarnings("deprecation")
private Thread myThread = new Thread() {
public void run() {
while (true) {
total++;
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
repaint();
showTime();
}
}
};
@SuppressWarnings("deprecation")
private void showTime() {
sec += 1;
if (sec >= 60) {
sec = 0;
min += 1;
}
if (min >= 60) {
min = 0;
hour += 1;
}
if (hour < 10) {
timeString = "0" + hour + ":";
} else {
timeString = "" + hour + ":";
}
if (min < 10) {
timeString = timeString + "0" + min + ":";
} else {
timeString = timeString + "" + min + ":";
}
if (sec < 10) {
timeString = timeString + "0" + sec;
} else {
timeString = timeString + "" + sec;
}
timeString = " " + timeString;
display.setText(timeString);
}
public static void main(String[] args) {
JFrame myFrame = new JFrame("myClock");
Clock1 myClock1 = new Clock1();
myFrame.add(myClock1);
myClock1.setBounds(0, 0, 500, 500);
myFrame.setBounds(500, 200, 500, 650);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}