color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JColorChooser jColor = new JColorChooser(); // 调用颜色面板,设置前景就可更改字体颜色
Color fcolor = ja.getForeground(); //得到前景色
ja.setForeground(jColor.showDialog(ja, "选择字体颜色", fcolor)); //设置前景色
}
});
cancel.addActionListener(new ActionListener() { //为取消按钮添加临听器
fontMain.setVisible(false); //当事件触发,把fontMain设为隐藏
ok.addActionListener(new ActionListener() { //为确定按钮添加临听器
fontMain.setVisible(false); //当事件触发,把fontMain设为隐藏
font.addActionListener(new ActionListener() { //为font按钮添加临听器
fontJd.setLocationRelativeTo(NewProgram.this); //设定fontJd的位置
fontJd.setVisible(true); //使fontJd为可见
int style = 0;
if (fontShape.getSelectedItem().equals("常规") == true) { //以下为设置字体的代码
style = Font.PLAIN; // 将style设置为常规字形保存
if (fontShape.getSelectedItem().equals("斜体") == true) {
style = Font.ITALIC;
if (fontShape.getSelectedItem().equals("粗体") == true) {
style = Font.BOLD;
if (fontShape.getSelectedItem().equals("粗斜体") == true) {
style = Font.BOLD ◆ Font.ITALIC;
// 设置字体大小的 //字号
"10" };
for (int i = 0; i size.length; i◆◆) {
if (sizeList.getSelectedItem().equals(size[i]) == true) { //设置字体字号
ja.setFont(new Font(String.valueOf(fontList
.getSelectedItem()), style, (10 - i) * 10));
fontJd.dispose();
}}};
请参考
详细代码
//CalendarWindow类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class CalendarWindow extends JFrame implements ActionListener,
MouseListener,FocusListener{
int year,month,day;
CalendarMessage calendarMessage;
CalendarPad calendarPad;
NotePad notePad;
JTextField showYear,showMonth;
JTextField [] showDay;
CalendarImage calendarImage;
Clock clock;
JButton nextYear,previousYear,nextMonth,previousMonth;
JButton saveDailyRecord,deleteDailyRecord,readDailyRecord;
File dir;
Color backColor=Color.gray;
public CalendarWindow(){
dir=new File("./dailyRecord");
dir.mkdir();
for(int i=0;ishowDay.length;i◆◆){
showDay[i]=new JTextField();
showDay[i].setBackground(backColor);
showDay[i].addMouseListener(this);
showDay[i].addFocusListener(this);
calendarMessage=new CalendarMessage();
calendarPad=new CalendarPad();
notePad=new NotePad();
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH)◆1;
day=calendar.get(Calendar.DAY_OF_MONTH);
calendarMessage.setYear(year);
calendarMessage.setMonth(month);
calendarMessage.setDay(day);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.setShowDayTextField(showDay);
notePad.setShowMessage(year,month,day);
calendarPad.showMonthCalendar();
doMark(); //给有日志的号码做标记,见后面的doMark()方法
calendarImage=new CalendarImage();
calendarImage.setImageFile(new File("sea.jpg"));
clock=new Clock();
JSplitPane splitV1=
new JSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);
new JSplitPane(JSplitPane.VERTICAL_SPLIT,notePad,clock);
add(splitH,BorderLayout.CENTER);
showYear.setHorizontalAlignment(JTextField.CENTER);
showMonth.setHorizontalAlignment(JTextField.CENTER);
nextYear=new JButton("下年");
previousYear=new JButton("上年");
nextMonth=new JButton("下月");
previousMonth=new JButton("上月");
nextYear.addActionListener(this);
previousYear.addActionListener(this);
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
showYear.addActionListener(this);
JPanel north=new JPanel();
north.add(previousYear);
north.add(showYear);
north.add(nextYear);
north.add(previousMonth);
north.add(showMonth);
north.add(nextMonth);
add(north,BorderLayout.NORTH);
saveDailyRecord=new JButton("保存日志") ;
deleteDailyRecord=new JButton("删除日志");
readDailyRecord=new JButton("读取日志");
saveDailyRecord.addActionListener(this);
deleteDailyRecord.addActionListener(this);
readDailyRecord.addActionListener(this);
JPanel pSouth=new JPanel();
pSouth.add(saveDailyRecord);
pSouth.add(deleteDailyRecord);
pSouth.add(readDailyRecord);
add(pSouth,BorderLayout.SOUTH);
setVisible(true);//根据参数 b 的值显示或隐藏此 Window
validate();//验证此容器及其所有子组件
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的操作
public void actionPerformed(ActionEvent e){
if(e.getSource()==nextYear){
year◆◆;
showYear.setText(""◆year);
doMark();
else if(e.getSource()==previousYear){
year--;
else if(e.getSource()==nextMonth){
month◆◆;
showMonth.setText(" "◆month);
else if(e.getSource()==previousMonth){
month--;
else if(e.getSource()==showYear){
String s=showYear.getText().trim();
char a[]=s.toCharArray();
boolean boo=false;
for(int i=0;ia.length;i◆◆)
if(!(Character.isDigit(a[i])))
boo=true;
if(boo==true) //弹出"警告"消息对话框
JOptionPane.showMessageDialog(this,"您输入了非法年份","警告", JOptionPane.WARNING_MESSAGE);
else if(boo==false)
year=Integer.parseInt(s);
else if(e.getSource()==saveDailyRecord){
notePad.save(dir,year,month,day);
else if(e.getSource()==deleteDailyRecord){
notePad.delete(dir,year,month,day);
else if(e.getSource()==readDailyRecord)
notePad.read(dir,year,month,day);
public void mousePressed(MouseEvent e){
JTextField text=(JTextField)e.getSource();
String str=text.getText().trim();
try{ day=Integer.parseInt(str);
catch(NumberFormatException exp){
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void focusGained(FocusEvent e){
Component com=(Component)e.getSource();
com.setBackground(Color.red);
public void focusLost(FocusEvent e){
com.setBackground(backColor);
public void doMark(){
showDay[i].removeAll();
String str=showDay[i].getText().trim();
try{
int n=Integer.parseInt(str);
if(isHaveDailyRecord(n)==true){ //见后面的isHaveDailyRecord()方法
JLabel mess=new JLabel("存");
mess.setFont(new Font("TimesRoman",Font.PLAIN,11));
mess.setForeground(Color.black) ;
showDay[i].add(mess);
} }
catch(Exception exp){}
calendarPad.repaint();
calendarPad.validate();
public boolean isHaveDailyRecord(int n){
String key=""◆year◆""◆month◆""◆n;
String [] dayFile=dir.list();
for(int k=0;kdayFile.length;k◆◆){
if(dayFile[k].equals(key◆".txt")){
break;
} }
return boo;
public static void main(String args[]){
new CalendarWindow();
//CalendarPad类:
public class CalendarPad extends JPanel{
JLabel title[];
String [] 星期={"SUN/日","MON/一","TUE/二","WED/三","THU/四","FRI/五","SAT/六"};
JPanel north,center;
public CalendarPad(){
setLayout(new BorderLayout());
north=new JPanel();
center=new JPanel();
add(center,BorderLayout.CENTER);
title[j]=new JLabel();
title[j].setText(星期[j]);
title[j].setHorizontalAlignment(JLabel.CENTER);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());
north.add(title[j]);
title[0].setForeground(Color.red);
public void setShowDayTextField(JTextField [] text){
showDay=text;
showDay[i].setHorizontalAlignment(JTextField.CENTER);
showDay[i].setEditable(false);
center.add(showDay[i]);
public void setCalendarMessage(CalendarMessage calendarMessage){
this.calendarMessage=calendarMessage;
public void showMonthCalendar(){
String [] a=calendarMessage.getMonthCalendar();
showDay[i].setText(a[i]);
validate();
刚才我这登的两号 回答问题出现了碰撞,上面那阿辉的代码接这里!!!import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;public class JNotePadUI extends JFrame {
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuClose; private JMenu editMenu;
private JMenuItem menuCut;
private JMenuItem menuCopy;
private JMenuItem menuPaste; private JMenuItem menuAbout;
private JTextArea textArea;
private JLabel stateBar;
private JFileChooser fileChooser;
private JPopupMenu popUpMenu; public JNotePadUI() {
super("新建文本文件");
setUpUIComponent();
setUpEventListener();
setVisible(true);
private void setUpUIComponent() {
// 菜单栏
JMenuBar menuBar = new JMenuBar();
// 设置「文件」菜单
JMenu fileMenu = new JMenu("文件");
menuOpen = new JMenuItem("打开");
// 快捷键设置
menuOpen.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_O,
InputEvent.CTRL_MASK));
menuSave = new JMenuItem("保存");
menuSave.setAccelerator(
KeyEvent.VK_S,
menuSaveAs = new JMenuItem("另存为"); menuClose = new JMenuItem("关闭");
menuClose.setAccelerator(
KeyEvent.VK_Q,
fileMenu.add(menuOpen);
fileMenu.addSeparator(); // 分隔线
fileMenu.add(menuSave);
fileMenu.add(menuSaveAs);
fileMenu.add(menuClose);
以上就是土嘎嘎小编为大家整理的java写的记事本代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!