Java實(shí)現(xiàn)超級(jí)實(shí)用的日記本
演示圖

演示圖

CalendarApp.java
package calenda;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class CalendarApp extends JFrame{
private CalendarPanel calendarPanel=new CalendarPanel();
private static JLabel jlblLearn=new JLabel("開始準(zhǔn)備記單詞");
private String versionID="歡迎使用版本1.0.20150427";
private JLabel jlblVersionID=new JLabel(versionID);
private JButton jbtPrior=new JButton("←");
private JButton jbtNext=new JButton("→");
private JButton jbtDiary=new JButton("寫日記");
private JButton jbtScanDiary=new JButton("看日記");
private static int year;
public void init()
{
year=calendarPanel.getYear();
calendarPanel.setBackground(Color.WHITE);
/**
* 添加功能性按鈕到日歷面板下方
*/
JPanel jpButtons=new JPanel(new FlowLayout());
//設(shè)置按鈕背景色為白色
jbtPrior.setBackground(Color.WHITE);
jbtNext.setBackground(Color.WHITE);
jbtDiary.setBackground(Color.WHITE);
jbtScanDiary.setBackground(Color.WHITE);
/*
* 給四個(gè)按鈕添加鼠標(biāo)事件,使其更加炫酷
*/
//一、后退按鈕
jbtPrior.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
jbtPrior.setForeground(Color.GREEN);
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
jbtPrior.setForeground(Color.BLACK);
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
//二、前進(jìn)按鈕
jbtNext.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
jbtNext.setForeground(Color.GREEN);
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
jbtNext.setForeground(Color.BLACK);
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
//三、寫日記按鈕
jbtDiary.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
jbtDiary.setForeground(Color.GREEN);
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
jbtDiary.setForeground(Color.BLACK);
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
//四、看日記按鈕
jbtScanDiary.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
jbtScanDiary.setForeground(Color.GREEN);
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
jbtScanDiary.setForeground(Color.BLACK);
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
jpButtons.add(jbtPrior);
jpButtons.add(jbtNext);
jpButtons.add(jbtDiary);
jpButtons.add(jbtScanDiary);
jpButtons.setBackground(Color.WHITE);
/**
* 添加日歷主要組件
*/
JPanel jpCalendar=new JPanel(new BorderLayout());
jpCalendar.add(calendarPanel,BorderLayout.CENTER);
jpCalendar.add(jpButtons,BorderLayout.SOUTH);
/**
* 添加背單詞模塊
*/
JPanel jpLearn=new JPanel(new FlowLayout());
jpLearn.setBorder(new TitledBorder("開心背單詞"));
jpLearn.add(jlblLearn);
jpLearn.setBackground(Color.WHITE);
/**
* 添加版本號(hào)信息
*/
JPanel jpVersionID=new JPanel(new FlowLayout());
Font font=new Font("宋體",Font.PLAIN,4);
jpVersionID.setFont(font);
jpVersionID.add(jlblVersionID);
jpVersionID.setBackground(Color.WHITE);
/**
* 容器面板,合并記單詞與版本號(hào)模塊
*/
JPanel jpBelow=new JPanel(new BorderLayout(2,1));
jpBelow.add(jpLearn,BorderLayout.CENTER);
jpBelow.add(jpVersionID,BorderLayout.SOUTH);
this.add(jpCalendar,BorderLayout.CENTER);
this.add(jpBelow,BorderLayout.SOUTH);
this.setBackground(Color.WHITE);
this.setSize(700, 500);
this.setLocationRelativeTo(null);
this.setTitle("多功能日歷");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
jbtScanDiary.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
new Thread(new thread_scanDiary()).start();
}
});
jbtDiary.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
//獲取本地系統(tǒng)時(shí)間
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");//設(shè)置日期格式
String time=df.format(new Date());
new Thread(new thread_keepDiary(time)).start();
}
});
jbtPrior.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int currentMonth=calendarPanel.getMonth();
if(currentMonth==0)
{
calendarPanel.setYear(year);
year--;
}
calendarPanel.setMonth((currentMonth-1)%12);
}
});
jbtNext.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int currentMonth=calendarPanel.getMonth();
if(currentMonth==11)
{
calendarPanel.setYear(++year);
}
calendarPanel.setMonth((currentMonth+1)%12);
}
});
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
new CalendarApp().init();
new Thread(new thread_showEnglish(jlblLearn)).start();
}
});
}
}
CalendarPanel.java
/**
* 本程序功能是進(jìn)行日歷主面板布局
*/
package calenda;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class CalendarPanel extends JPanel {
/**
* 聲明數(shù)據(jù)變量
*/
private static final long serialVersionUID = 1L;
private JLabel jlblHeader=new JLabel(" ",JLabel.CENTER);
private JLabel[] jlblDay=new JLabel[49];
private Calendar calendar=new GregorianCalendar();
private int year0=calendar.get(Calendar.YEAR);
private int month0=calendar.get(Calendar.MONTH);
private int day0=calendar.get(Calendar.DAY_OF_MONTH);
private int month;
private int year;
private int day;
private JPanel jpDays=new JPanel(new GridLayout(0,7));
Font font1=new Font("宋體",Font.ITALIC,20);
Font font2=new Font("宋體",Font.BOLD,26);
Font font3=new Font("宋體",Font.BOLD,30);
public CalendarPanel()
{
//設(shè)置日歷頭部件以及日期標(biāo)簽的背景色為白色
jlblHeader.setBackground(Color.WHITE);
jpDays.setBackground(Color.WHITE);
//聲明每個(gè)標(biāo)簽
for(int i=0;i<49;i++)
{
jlblDay[i]=new JLabel();
jlblDay[i].setBorder(new LineBorder(Color.LIGHT_GRAY,1));
jlblDay[i].setHorizontalAlignment(JLabel.RIGHT);
jlblDay[i].setVerticalAlignment(JLabel.TOP);
}
calendar=new GregorianCalendar();
month=calendar.get(Calendar.MONTH);
year=calendar.get(Calendar.YEAR);
day=calendar.get(Calendar.DATE);
//更新日歷
updateCalendar();
showHeader();
showDays();
//添加到主面板
this.setLayout(new BorderLayout());
this.add(jlblHeader, BorderLayout.NORTH);
this.add(jpDays, BorderLayout.CENTER);
}
private void showHeader()
{
SimpleDateFormat sdf=new SimpleDateFormat("MMMM yyyy",Locale.CHINA);
String header=sdf.format(calendar.getTime());
jlblHeader.setText(header);
jlblHeader.setForeground(Color.BLUE);
jlblHeader.setFont(font3);
}
private void showDayNames()
{
DateFormatSymbols dfs=new DateFormatSymbols(Locale.CHINA);
String dayNames[]=dfs.getWeekdays();
for(int i=0;i<7;i++)
{
jlblDay[i].setText(dayNames[i+1]);
jlblDay[i].setForeground(Color.BLUE);
jlblDay[i].setHorizontalAlignment(JLabel.CENTER);
jlblDay[i].setFont(font2);
jpDays.add(jlblDay[i]);
}
}
public void showDays()
{
jpDays.removeAll();
showDayNames();
int startingDayOfMonth=calendar.get(Calendar.DAY_OF_WEEK);
Calendar cloneCalendar=(Calendar)calendar.clone();
cloneCalendar.add(Calendar.DATE, -1);
int daysInPrecedingMonth=cloneCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for(int i=0;i<startingDayOfMonth-1;i++)
{
jlblDay[i+7].setForeground(Color.LIGHT_GRAY);
jlblDay[i+7].setHorizontalAlignment(JLabel.CENTER);
jlblDay[i+7].setText(daysInPrecedingMonth-startingDayOfMonth+2+i+"");
jlblDay[i+7].setFont(font1);
jpDays.add(jlblDay[i+7]);
}
int daysInCurrentMonth=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for(int i=1;i<=daysInCurrentMonth;i++)
{
if(i==day0&&year==year0&&month==month0)
{
jlblDay[i-2+startingDayOfMonth+7].setForeground(Color.red);
jlblDay[i-2+startingDayOfMonth+7].setHorizontalAlignment(JLabel.CENTER);
jlblDay[i-2+startingDayOfMonth+7].setText(i+"");
jlblDay[i-2+startingDayOfMonth+7].setFont(font2);
jpDays.add(jlblDay[i-2+startingDayOfMonth+7]);
}
else
{
jlblDay[i-2+startingDayOfMonth+7].setForeground(Color.darkGray);
jlblDay[i-2+startingDayOfMonth+7].setHorizontalAlignment(JLabel.CENTER);
jlblDay[i-2+startingDayOfMonth+7].setText(i+"");
jlblDay[i-2+startingDayOfMonth+7].setFont(font1);
jpDays.add(jlblDay[i-2+startingDayOfMonth+7]);
}
}
int j=1;
for(int i=daysInCurrentMonth-1+startingDayOfMonth+7;i%7!=0;i++)
{
jlblDay[i].setForeground(Color.LIGHT_GRAY);
jlblDay[i].setHorizontalAlignment(JLabel.CENTER);
jlblDay[i].setText(j++ +"");
jlblDay[i].setFont(font1);
jpDays.add(jlblDay[i]);
}
jpDays.repaint();
}
public void updateCalendar()
{
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, 1);
month=calendar.get(Calendar.MONTH);
year=calendar.get(Calendar.YEAR);
day=calendar.get(Calendar.DATE);
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public void setMonth(int month)
{
this.month=month;
updateCalendar();
showHeader();
showDays();
}
public void setYear(int year)
{
this.year=year;
updateCalendar();
showHeader();
showDays();
}
}
Diary.java
/**
* 本程序?yàn)槿沼涱?
* 最后修改日期為2015-4-27
*/
package calenda;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.JOptionPane;
public class Diary implements Serializable {
/**
* 數(shù)據(jù)域
*/
//私有變量
private String filename;
private String theme;
private String content;
//含參構(gòu)造方法
public Diary(String filename,String theme,String content)
{
if(theme.length()==0)
{
JOptionPane.showMessageDialog(null, "無論心情如何,總得有個(gè)主題吧!");
}
else if(content.length()==0)
{
JOptionPane.showMessageDialog(null, "把開心的不開心的都盡情寫下吧!");
}
else
{
File CalendarDiaryFile=new File("C:/Calendar/Diary");
if(!CalendarDiaryFile.exists())
{
CalendarDiaryFile.mkdirs();
}
this.filename="C:/Calendar/Diary/"+filename+".dat";
this.theme=theme;
this.content=content;
}
}
public void write() throws Exception
{
File file=new File(filename);
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this);
oos.close();
}
public Diary read() throws Exception
{
File file=new File(filename);
FileInputStream fis=new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Diary d=(Diary) ois.readObject();
ois.close();
return d;
}
/**
* 變量get()方法
* @return
*/
public String getTheme()
{
return this.theme;
}
public String getContent()
{
return this.content;
}
/**
* 變量set()方法
* @param comment
*/
}
thread_keepDiary.java
/**
* 備忘錄線程
* 用于單擊標(biāo)簽時(shí)可以存儲(chǔ)日記
* 目前只考慮年月日時(shí)間點(diǎn),不標(biāo)記具體時(shí)間點(diǎn)
*/
package calenda;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.nio.file.Files;
import javax.swing.*;
public class thread_keepDiary extends JFrame implements Runnable {
private JFrame jf=this;
private JTextArea jta=new JTextArea();
private JButton jbtSave=new JButton("保存");
private JLabel jlblTitle=new JLabel("主題");
private JTextField jtfTitle=new JTextField(16);
private String id;
public thread_keepDiary(String time)
{
JPanel jpTitle=new JPanel();
jpTitle.setLayout(new BorderLayout());
jpTitle.add(jlblTitle, BorderLayout.WEST);
jpTitle.add(jtfTitle,BorderLayout.CENTER);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JScrollPane jsp=new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.id=time;
jbtSave.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String theme=jtfTitle.getText().trim();
String content=jta.getText();
Diary d_today=new Diary(id,theme,content);
try
{
d_today.write();
JOptionPane.showMessageDialog(null, "保存成功!");
jf.dispose();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
this.setTitle("備忘錄"+id);
this.add(jsp,BorderLayout.CENTER);
this.add(jpTitle, BorderLayout.NORTH);
this.add(jbtSave, BorderLayout.SOUTH);
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
@Override
public void run() {
this.setVisible(true);
}
}
thread_mottoSparkle.java
package calenda;
import javax.swing.JLabel;
public class thread_mottoSparkle implements Runnable{
private JLabel jlbl;
public thread_mottoSparkle(JLabel jlbl)
{
this.jlbl=jlbl;
}
@Override
public void run() {
String content=jlbl.getText();
int L=content.length();
int index=0;
while(true)
{
jlbl.setText(content.substring(0, index));
try
{
Thread.sleep(250);
}
catch(Exception ex)
{
ex.printStackTrace();
}
index++;
if(index==(L+1))
index=0;
}
}
}
thread_scanDiary.java
/**
* 查看日記線程編寫
*/
package calenda;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class thread_scanDiary extends JFrame implements Runnable{
/**
* 聲明變量區(qū)
*/
private static final long serialVersionUID = 1L;
//日歷數(shù)據(jù)庫存放路徑
private String path="C:/Calendar/Diary";
//日歷總個(gè)數(shù)
private static int num;
//聲明日歷文件
private File file;
private File[] Diary;
//聲明JTable模型
private JTable jtable;
//聲明格言面板及標(biāo)簽及內(nèi)容等
private JPanel jpMotto=new JPanel();
private JLabel jlblMotto=new JLabel();
private Font font=new Font("宋體",Font.ITALIC,20);
private String wish="唯有專注,才能讓自己成功。";
//增加彈出式菜單2015-4-26
private JPopupMenu jPopupMenu1=new JPopupMenu();
//聲明菜單
private JMenuItem jmiScan=new JMenuItem("查看");
private JMenuItem jmiDelete=new JMenuItem("刪除");
private JMenuItem jmiComment=new JMenuItem("評(píng)論");
@Override
public void run() {
//嘗試彈出式菜單增加子菜單
jmiScan.setForeground(Color.RED);
jmiDelete.setForeground(Color.RED);
jmiComment.setForeground(Color.RED);
jPopupMenu1.add(jmiScan);
jPopupMenu1.addSeparator();
jPopupMenu1.add(jmiDelete);
jPopupMenu1.addSeparator();
jPopupMenu1.add(jmiComment);
/**
* 智能獲取文件列表
*/
file = new File(path);
Diary=file.listFiles();
num=Diary.length;
String[] head={" 時(shí)間 "," 主題 "};
Object[][] diary=new Object[num][2];
for(int i=0;i<num;i++)
{
try{
String time=Diary[i].getName().replaceFirst(".dat", "");
FileInputStream fis=new FileInputStream(Diary[i]);
ObjectInputStream ois = new ObjectInputStream(fis);
Diary d=(Diary) ois.readObject();
ois.close();
String theme=d.getTheme();
diary[i][0]=time;
diary[i][1]=theme;
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/**
* 格言面板取
*/
jlblMotto.setText(wish);
jlblMotto.setFont(font);
jlblMotto.setForeground(Color.RED);
jpMotto.add(jlblMotto);
jpMotto.setBackground(Color.WHITE);
/**
* 日歷列表面板區(qū)
*/
final DefaultTableModel tableModel=new DefaultTableModel(diary,head);
jtable=new JTable(tableModel);
jtable.setBackground(Color.WHITE);
jtable.setRowHeight(30);
jtable.setDoubleBuffered(false);
jtable.setComponentPopupMenu(jPopupMenu1);
jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 設(shè)置table內(nèi)容居中
tcr.setHorizontalAlignment(SwingConstants.CENTER);// 這句和上句作用一樣
jtable.setDefaultRenderer(Object.class, tcr);
JScrollPane jsp=new JScrollPane(jtable);
/**
* 彈出式菜單事件監(jiān)聽器編寫
*/
//查看菜單
jmiScan.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(jtable.getSelectedRow()>=0)
{
int index=jtable.getSelectedRow();
String filename="C:/Calendar/Diary/"+Diary[index].getName();
File file=new File(filename);
try
{
FileInputStream fis=new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Diary d=(Diary) ois.readObject();
ois.close();
JFrame jf=new JFrame();
JTextArea jta=new JTextArea();
JLabel jlblTitle=new JLabel("主題");
JTextField jtfTitle=new JTextField(16);
JPanel jpTitle=new JPanel();
jpTitle.setLayout(new BorderLayout());
jpTitle.add(jlblTitle, BorderLayout.WEST);
jpTitle.add(jtfTitle,BorderLayout.CENTER);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JScrollPane jsp=new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jtfTitle.setText(d.getTheme());
jta.setText(d.getContent());
jtfTitle.setEditable(false);
jta.setEditable(false);
jf.setTitle("日記 "+Diary[index].getName().replaceFirst(".dat", ""));
jf.add(jsp,BorderLayout.CENTER);
jf.add(jpTitle, BorderLayout.NORTH);
jf.setSize(400,400);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jf.setVisible(true);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
else
{
JOptionPane.showMessageDialog(null, "請(qǐng)先選中一個(gè)日記!");
}
}
});
//刪除菜單
jmiDelete.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(jtable.getSelectedRow()>=0)
{
int index=jtable.getSelectedRow();
String filename="C:/Calendar/Diary/"+Diary[index].getName();
File file=new File(filename);
int option=JOptionPane.showConfirmDialog(null, "你確定要?jiǎng)h除日記"+Diary[index].getName()+"?");
if(option==JOptionPane.YES_OPTION)
{
file.delete();
tableModel.removeRow(index);
SwingUtilities.updateComponentTreeUI(jtable);
JOptionPane.showMessageDialog(null, "刪除成功!");
}
else
{
}
}
else
{
JOptionPane.showMessageDialog(null, "請(qǐng)先選中一個(gè)日記!");
}
}
});
/**
* 主框架布局
*/
this.add(jsp,BorderLayout.CENTER);
this.add(jpMotto, BorderLayout.SOUTH);
this.setSize(600, 500);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle("日記列表");
this.setVisible(true);
new Thread(new thread_mottoSparkle(jlblMotto)).start();
}
}
thread_showEnglish.java
package calenda;
import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.security.SecureRandom;
import java.util.Random;
import javax.swing.JLabel;
public class thread_showEnglish implements Runnable {
private static JLabel jlbl=new JLabel();
Font font=new Font("���ו",Font.BOLD,18);
public thread_showEnglish(JLabel jlbl)
{
this.jlbl=jlbl;
jlbl.setForeground(Color.MAGENTA);
jlbl.setFont(font);
}
@Override
public void run() {
// TODO Auto-generated method stub
int count=0;
try
{
String file="C:/Calendar/Learning/english_word.txt";
BufferedReader input = new BufferedReader(new FileReader(file));
while(input.readLine()!=null)
{
count++;
}
input.close();
int[] word = new int[count];
SecureRandom random = new SecureRandom();
for(int i=0;i<count;i++)
{
word[i]=random.nextInt(count);
}
int index=0;
Thread.sleep(1000);
while(true)
{
BufferedReader input1 = new BufferedReader(new FileReader(file));
String content="";
int line=0;
while((content=input1.readLine())!=null)
{
if(line==word[index])
{
jlbl.setText(content);
}
line++;
}
if(index==count-1)
index=0;
else
index++;
Thread.sleep(3500);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- 關(guān)于JAVA經(jīng)典算法40題(超實(shí)用版)
- 簡(jiǎn)潔實(shí)用的Java Base64編碼加密異常處理類代碼
- 20個(gè)非常實(shí)用的Java程序代碼片段
- Java無限級(jí)樹(遞歸)超實(shí)用案例
- Java Web十條開發(fā)實(shí)用小知識(shí)
- Java用freemarker導(dǎo)出word實(shí)用示例
- java實(shí)用驗(yàn)證碼的實(shí)現(xiàn)代碼
- Java對(duì)象簡(jiǎn)單實(shí)用案例之計(jì)算器實(shí)現(xiàn)代碼
- 詳解JAVA常用的時(shí)間操作【實(shí)用】
- 總結(jié)十個(gè)實(shí)用但偏執(zhí)的Java編程技術(shù)
相關(guān)文章
Java基礎(chǔ) Servlet監(jiān)聽器詳解
這篇文章主要介紹了Java基礎(chǔ) Servlet監(jiān)聽器詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
Java String的intern方法使用場(chǎng)景示例
這篇文章主要介紹了Java String的intern方法使用場(chǎng)景示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11
使用Java和PostgreSQL存儲(chǔ)向量數(shù)據(jù)的實(shí)現(xiàn)指南
在當(dāng)今的數(shù)字化時(shí)代,數(shù)據(jù)存儲(chǔ)的方式和技術(shù)正變得越來越復(fù)雜和多樣化,隨著機(jī)器學(xué)習(xí)和數(shù)據(jù)科學(xué)的發(fā)展,向量數(shù)據(jù)的存儲(chǔ)和管理變得尤為重要,本文將詳細(xì)介紹如何使用 Java 和 PostgreSQL 數(shù)據(jù)庫來存儲(chǔ)向量數(shù)據(jù),需要的朋友可以參考下2024-09-09
java圖形化界面實(shí)現(xiàn)簡(jiǎn)單混合運(yùn)算計(jì)算器的示例代碼
這篇文章主要介紹了java圖形化界面實(shí)現(xiàn)簡(jiǎn)單混合運(yùn)算計(jì)算器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java實(shí)現(xiàn)二分查找樹及其相關(guān)操作
二分查找樹是一種有組織的二叉樹。我們可以通過鏈接節(jié)點(diǎn)表示這樣一棵樹,二分查找樹(Binary Search Tree)的基本操作有搜索、求最大值、求最小值、求前驅(qū)、求后繼、插入及刪除,對(duì)java二分查找樹相關(guān)知識(shí)感興趣的朋友一起看看吧2021-07-07
@PropertySource 無法讀取配置文件的屬性值解決方案
這篇文章主要介紹了@PropertySource 無法讀取配置文件的屬性值解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java冒泡排序(Bubble Sort)實(shí)例講解
冒泡排序的原理:假設(shè)要求的數(shù)組是正序,兩兩進(jìn)行比較,如果前一個(gè)書比后一個(gè)數(shù)小,位置不變。如果前一個(gè)數(shù)比后一個(gè)數(shù)大,位置互換,再跟后一個(gè)數(shù)進(jìn)行比較,直到最后。就是逐步把大數(shù)送到最后,下面來個(gè)實(shí)例給大家看看2013-11-11
Spring?Boot自定義?Starter并推送到遠(yuǎn)端公服的詳細(xì)代碼
這篇文章主要介紹了Spring?Boot自定義?Starter并推送到遠(yuǎn)端公服,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
java文件刪除不了File類的delete方法刪不掉文件的原因以及分析
這篇文章主要介紹了java文件刪除不了File類的delete方法刪不掉文件的原因以及分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

