標題:
文字編輯器 (九)
[打印本頁]
作者:
tonyh
時間:
2017-2-11 16:45
標題:
文字編輯器 (九)
本帖最後由 tonyh 於 2017-2-11 17:09 編輯
使用 JRadioButtonMenuItem 類別產生帶有 radio button 的 menu item,再將同一區塊的選項加入由 ButtonGroup 類別所產生的群組物件中,使於同一區塊只有一個選項可被選取。
[attach]1931[/attach]
[attach]1932[/attach]
[attach]1933[/attach]
import java.awt.Font;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JOptionPane;
//import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class Ch09 implements ActionListener{
String title="My Editor";
JFrame fm,fm2;
JScrollPane sp;
JTextArea ta;
ImageIcon ic,ic_cut,ic_exit,ic_new,ic_open,ic_paste,ic_save,ic_copy,ic_selectall,ic_about,ic_developer,ic_size,ic_style,ic_type;
JMenuBar mb;
JMenu mn_file,mn_edit,mn_option,mn_help,mn_size,mn_type,mn_style;
JMenuItem mi_new,mi_open,mi_save,mi_exit,mi_copy,mi_paste,mi_cut,mi_selectall,mi_about;
JRadioButtonMenuItem rbmi_1[]=new JRadioButtonMenuItem[3];
JRadioButtonMenuItem rbmi_2[]=new JRadioButtonMenuItem[4];
JRadioButtonMenuItem rbmi_3[]=new JRadioButtonMenuItem[2];
ButtonGroup bg_1,bg_2,bg_3;
JFileChooser fc;
FileNameExtensionFilter filter1;
void initialize()
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e){}
filter1 = new FileNameExtensionFilter("文字文件(*.txt)", "txt");
fc=new JFileChooser();
fc.setFileFilter(filter1);
ic=new ImageIcon(Ch09.class.getResource("pic/editor.png"));
ic_cut=new ImageIcon(Ch09.class.getResource("pic/cut.png"));
ic_exit=new ImageIcon(Ch09.class.getResource("pic/exit.png"));
ic_new=new ImageIcon(Ch09.class.getResource("pic/new.png"));
ic_open=new ImageIcon(Ch09.class.getResource("pic/open.png"));
ic_paste=new ImageIcon(Ch09.class.getResource("pic/paste.png"));
ic_save=new ImageIcon(Ch09.class.getResource("pic/save.png"));
ic_copy=new ImageIcon(Ch09.class.getResource("pic/copy.png"));
ic_selectall=new ImageIcon(Ch09.class.getResource("pic/selectall.png"));
ic_about=new ImageIcon(Ch09.class.getResource("pic/about.png"));
ic_developer=new ImageIcon(Ch09.class.getResource("pic/developer.png"));
ic_size=new ImageIcon(Ch09.class.getResource("pic/size.png"));
ic_type=new ImageIcon(Ch09.class.getResource("pic/type.png"));
ic_style=new ImageIcon(Ch09.class.getResource("pic/style.png"));
mi_new=new JMenuItem("開新檔案",ic_new);
mi_new.addActionListener(this);
mi_open=new JMenuItem("開啟舊檔",ic_open);
mi_open.addActionListener(this);
mi_save=new JMenuItem("儲存檔案",ic_save);
mi_save.addActionListener(this);
mi_exit=new JMenuItem("結束",ic_exit);
mi_exit.addActionListener(this);
mi_copy=new JMenuItem("複製",ic_copy);
mi_copy.addActionListener(this);
mi_paste=new JMenuItem("貼上",ic_paste);
mi_paste.addActionListener(this);
mi_cut=new JMenuItem("剪下",ic_cut);
mi_cut.addActionListener(this);
mi_selectall=new JMenuItem("全選",ic_selectall);
mi_selectall.addActionListener(this);
mi_about=new JMenuItem("關於 My Editor",ic_about);
mi_about.addActionListener(this);
rbmi_1[0]=new JRadioButtonMenuItem("大 (22)");
rbmi_1[1]=new JRadioButtonMenuItem("中 (18)",true);
rbmi_1[2]=new JRadioButtonMenuItem("小 (14)");
bg_1=new ButtonGroup();
for(int i=0; i<rbmi_1.length; i++)
bg_1.add(rbmi_1[i]);
rbmi_2[0]=new JRadioButtonMenuItem("一般",true);
rbmi_2[1]=new JRadioButtonMenuItem("粗體");
rbmi_2[2]=new JRadioButtonMenuItem("斜體");
rbmi_2[3]=new JRadioButtonMenuItem("粗體+斜體");
bg_2=new ButtonGroup();
for(int i=0; i<rbmi_2.length; i++)
bg_2.add(rbmi_2[i]);
rbmi_3[0]=new JRadioButtonMenuItem("白底黑字",true);
rbmi_3[1]=new JRadioButtonMenuItem("黑底白字");
bg_3=new ButtonGroup();
for(int i=0; i<rbmi_3.length; i++)
bg_3.add(rbmi_3[i]);
mn_file=new JMenu(" 檔案(F) ");
mn_file.setMnemonic('F'); //設定快速鍵
mn_file.add(mi_new);
mn_file.add(mi_open);
mn_file.add(mi_save);
mn_file.addSeparator(); //分隔線
mn_file.add(mi_exit);
mn_edit=new JMenu(" 編輯(E) ");
mn_edit.setMnemonic('E'); //設定快速鍵
mn_edit.add(mi_copy);
mn_edit.add(mi_paste);
mn_edit.add(mi_cut);
mn_edit.addSeparator();
mn_edit.add(mi_selectall);
mn_size=new JMenu("文字大小");
mn_size.setIcon(ic_size);
for(int i=0; i<rbmi_1.length; i++)
mn_size.add(rbmi_1[i]);
mn_type=new JMenu("文字樣式");
mn_type.setIcon(ic_type);
for(int i=0; i<rbmi_2.length; i++)
mn_type.add(rbmi_2[i]);
mn_style=new JMenu("風格");
mn_style.setIcon(ic_style);
for(int i=0; i<rbmi_3.length; i++)
mn_style.add(rbmi_3[i]);
mn_option=new JMenu(" 設定(O) ");
mn_option.setMnemonic('O');
mn_option.add(mn_size);
mn_option.add(mn_type);
mn_option.addSeparator();
mn_option.add(mn_style);
mn_help=new JMenu(" 說明(H) ");
mn_help.setMnemonic('H'); //設定快速鍵
mn_help.add(mi_about);
mb=new JMenuBar();
mb.setBorder(new BevelBorder(BevelBorder.RAISED)); //設定具陰影效果的邊框
mb.add(mn_file);
mb.add(mn_edit);
mb.add(mn_option);
mb.add(mn_help);
ta=new JTextArea();
ta.setFont(new Font("新細明體", Font.PLAIN, 18));
ta.setLineWrap(true); //自動換行
sp=new JScrollPane(ta);
fm=new JFrame(title+" - 未命名");
fm.setBounds(100, 100, 500, 350);
fm.setIconImage(ic.getImage());
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(sp);
fm.setJMenuBar(mb);
}
Ch09()
{
initialize();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mi_new)
{
ta.setText("");
fm.setTitle(title+" - 未命名");
}
else if(e.getSource()==mi_open)
{
int ret=fc.showOpenDialog(null);
if(ret==JFileChooser.APPROVE_OPTION)
{
try
{
String str;
File fi=fc.getSelectedFile();
BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
ta.setText(br.readLine());
do
{
str=br.readLine();
if(str==null)
break;
ta.append("\n"+str);
}while(true);
br.close();
fm.setTitle(title+" - "+fc.getName(fi));
}catch(Exception ex){}
}
}
else if(e.getSource()==mi_save)
{
int ret=fc.showSaveDialog(null);
if(ret==JFileChooser.APPROVE_OPTION)
{
try
{
File fi=fc.getSelectedFile();
BufferedWriter bw;
String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4);
System.out.println(ext);
String fiPath="";
if(fc.getFileFilter()==filter1)
{
if(ext.equals(".txt"))
fiPath=fi.getAbsolutePath();
else
fiPath=fi.getAbsolutePath()+".txt";
}
else
fiPath=fi.getAbsolutePath();
bw=new BufferedWriter(new FileWriter(fiPath));
bw.write(ta.getText().replaceAll("\n", "\r\n"));
//windows下的文字檔分行符號:\r\n linux/unix下的文字檔分行符號:\r Mac下的文字檔分行符號:\n
bw.flush();
bw.close();
fm.setTitle(title+" - "+fc.getName(fi));
}catch(Exception ex){}
}
}
else if(e.getSource()==mi_exit)
System.exit(0);
else if(e.getSource()==mi_copy)
ta.copy();
else if(e.getSource()==mi_paste)
ta.paste();
else if(e.getSource()==mi_cut)
ta.cut();
else if(e.getSource()==mi_selectall)
ta.selectAll();
else if(e.getSource()==mi_about)
{
String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n林宇翔所開發,感謝您的使用!";
JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ic_developer);
}
}
public static void main(String[] args) {
new Ch09();
}
}
複製代碼
作者:
劉得恩
時間:
2017-2-11 17:40
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Ch09 implements ActionListener{
JFrame f;
JTextArea t;
JScrollPane s;
JMenuItem mi[]=new JMenuItem[9];
String name[]=new String[]{"new","open","save","exit","copy","paste","cut","selectall","about"};
ImageIcon ic,d;
JMenuBar mb;
JMenu fl,ed,h,size,font,style,settings;
JRadioButtonMenuItem rbmi[][]=new JRadioButtonMenuItem[3][];
ButtonGroup bg[]=new ButtonGroup[3];
JFileChooser fc;
FileNameExtensionFilter filter1,filter2;
String title="MyEditor";
Ch09()
{
d=new ImageIcon(Ch09.class.getResource("pic/developer.png"));
ic=new ImageIcon(Ch09.class.getResource("pic/editor.png"));
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
fc=new JFileChooser();
rbmi[0]=new JRadioButtonMenuItem[3];
rbmi[1]=new JRadioButtonMenuItem[4];
rbmi[2]=new JRadioButtonMenuItem[2];
rbmi[0][0]=new JRadioButtonMenuItem("大(22)");
rbmi[0][1]=new JRadioButtonMenuItem("中(18)",true);
rbmi[0][2]=new JRadioButtonMenuItem("小(14)");
rbmi[1][0]=new JRadioButtonMenuItem("Plain",true);
rbmi[1][1]=new JRadioButtonMenuItem("Bold");
rbmi[1][2]=new JRadioButtonMenuItem("Italic");
rbmi[1][3]=new JRadioButtonMenuItem("Bold + Italic");
rbmi[2][0]=new JRadioButtonMenuItem("白底黑字",true);
rbmi[2][1]=new JRadioButtonMenuItem("黑底白字");
for(int i=0;i<3;i++)
bg[i]=new ButtonGroup();
size=new JMenu("size");
size.setIcon(new ImageIcon(Ch09.class.getResource("pic/size.png")));
font=new JMenu("font");
font.setIcon(new ImageIcon(Ch09.class.getResource("pic/type.png")));
style=new JMenu("style");
style.setIcon(new ImageIcon(Ch09.class.getResource("pic/style.png")));
for(int i=0;i<3;i++)
for(int j=0;j<rbmi[i].length;j++)
bg[i].add(rbmi[i][j]);
for(int i=0;i<rbmi[0].length;i++)
size.add(rbmi[0][i]);
for(int i=0;i<rbmi[1].length;i++)
font.add(rbmi[1][i]);
for(int i=0;i<rbmi[2].length;i++)
style.add(rbmi[2][i]);
for(int i=0;i<9;i++)
{
mi[i]=new JMenuItem(name[i],new ImageIcon(Ch09.class.getResource("pic/"+name[i]+".png")));
}
fl=new JMenu("檔案(F)");
ed=new JMenu("編輯(E)");
settings=new JMenu("設定(O)");
h=new JMenu("說明(H)");
fl.setMnemonic('F');
ed.setMnemonic('E');
settings.setMnemonic('O');
h.setMnemonic('H');
settings.add(size);
settings.add(font);
settings.add(style);
filter1=new FileNameExtensionFilter("純文字", "txt");
filter2=new FileNameExtensionFilter("圖片檔", "jpg","png","bmp");
fc.addChoosableFileFilter(filter1);
fc.addChoosableFileFilter(filter2);
fc.setFileFilter(filter1);
for(int i=0;i<3;i++)
{
mi[i].addActionListener(this);
fl.add(mi[i]);
}
fl.addSeparator();
mi[3].addActionListener(this);
fl.add(mi[3]);
for(int i=4;i<7;i++)
{
mi[i].addActionListener(this);
ed.add(mi[i]);
}
mi[8].addActionListener(this);
h.add(mi[8]);
ed.addSeparator();
ed.add(mi[7]);
mi[7].addActionListener(this);
mb=new JMenuBar();
mb.setBorder(new BevelBorder(BevelBorder.RAISED));
mb.add(fl);
mb.add(ed);
mb.add(settings);
mb.add(h);
t=new JTextArea();
t.setLineWrap(true);
t.setFont(new Font("新細明體",Font.PLAIN,15));
s=new JScrollPane(t);
f=new JFrame(title+" 未命名");
f.setBounds(100,100,400,300);
f.setIconImage(ic.getImage());
f.setResizable(true);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(s);
f.setJMenuBar(mb);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mi[0])
{
t.setText("");
f.setTitle(title+" 未命名");
}
else if(e.getSource()==mi[3])
System.exit(0);
else if(e.getSource()==mi[4])
{
t.copy();
}
else if(e.getSource()==mi[5])
{
t.paste();
}
else if(e.getSource()==mi[6])
{
t.cut();
}
else if(e.getSource()==mi[7])
{
t.selectAll();
}
else if(e.getSource()==mi[2])
{
int r=fc.showSaveDialog(null);
File fi=fc.getSelectedFile();
String fPath=fi.getAbsolutePath();
String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4);
if(r==JFileChooser.APPROVE_OPTION)
{
System.out.println(ext);
if(fc.getFileFilter()==filter1&&!ext.equals(".txt"))
fPath+=".txt";
else if(fc.getFileFilter()==filter2&&!ext.equals(".jpg"))
fPath+=".jpg";
try
{
BufferedWriter bw=new BufferedWriter(new FileWriter(fPath));
bw.write(t.getText().replaceAll("\n","\r\n"));
bw.flush();
bw.close();
f.setTitle(title+" - "+fPath);
}
catch(Exception ex){}
}
}
else if(e.getSource()==mi[1])
{
int r=fc.showOpenDialog(null);
File fi=fc.getSelectedFile();
if(r==JFileChooser.APPROVE_OPTION)
{
try
{
BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
t.setText(br.readLine());
while(true)
{
String s=br.readLine();
if(s==null)
break;
t.append("\n"+s);
}
br.close();
}
catch(Exception ex){}
}
}
else if(e.getSource()==mi[8])
{
String msg="A";
JOptionPane.showMessageDialog(f,msg,"About",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] arg)
{
new Ch09();
}
}
複製代碼
作者:
林宇翔
時間:
2017-2-11 17:42
(上半)
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
public class Ch07 implements ActionListener{
String title="My Editor";
JFrame fm,fm2;
JScrollPane sp;
JTextArea ta;
ImageIcon ic,ic_cut,ic_exit,ic_new,ic_open,ic_paste,ic_save,ic_copy,ic_selectall,ic_about,ic_developer,ic_size,ic_style,ic_type;
JMenuBar mb;
JMenu mn_file,mn_edit,mn_option,mn_help,mn_size,mn_type,mn_style;
JMenuItem mi_new,mi_open,mi_save,mi_exit,mi_copy,mi_paste,mi_cut,mi_selectall,mi_about;
JRadioButtonMenuItem rbmi_1[]=new JRadioButtonMenuItem[3];
JRadioButtonMenuItem rbmi_2[]=new JRadioButtonMenuItem[4];
JRadioButtonMenuItem rbmi_3[]=new JRadioButtonMenuItem[2];
ButtonGroup bg_1,bg_2,bg_3;
JFileChooser fc;
FileNameExtensionFilter filter1;
void initialize()
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e){}
filter1 = new FileNameExtensionFilter("文字文件(*.txt)", "txt");
fc=new JFileChooser();
fc.setFileFilter(filter1);
ic=new ImageIcon(Ch07.class.getResource("pic/editor.png"));
ic_cut=new ImageIcon(Ch07.class.getResource("pic/cut.png"));
ic_exit=new ImageIcon(Ch07.class.getResource("pic/exit.png"));
ic_new=new ImageIcon(Ch07.class.getResource("pic/new.png"));
ic_open=new ImageIcon(Ch07.class.getResource("pic/open.png"));
ic_paste=new ImageIcon(Ch07.class.getResource("pic/paste.png"));
ic_save=new ImageIcon(Ch07.class.getResource("pic/save.png"));
ic_copy=new ImageIcon(Ch07.class.getResource("pic/copy.png"));
ic_selectall=new ImageIcon(Ch07.class.getResource("pic/selectall.png"));
ic_about=new ImageIcon(Ch07.class.getResource("pic/about.png"));
ic_developer=new ImageIcon(Ch07.class.getResource("pic/developer.png"));
ic_size=new ImageIcon(Ch07.class.getResource("pic/size.png"));
ic_type=new ImageIcon(Ch07.class.getResource("pic/type.png"));
ic_style=new ImageIcon(Ch07.class.getResource("pic/style.png"));
mi_new=new JMenuItem("開新檔案",ic_new);
mi_new.addActionListener(this);
mi_open=new JMenuItem("開啟舊檔",ic_open);
mi_open.addActionListener(this);
mi_save=new JMenuItem("儲存檔案",ic_save);
mi_save.addActionListener(this);
mi_exit=new JMenuItem("結束",ic_exit);
mi_exit.addActionListener(this);
mi_copy=new JMenuItem("複製",ic_copy);
mi_copy.addActionListener(this);
mi_paste=new JMenuItem("貼上",ic_paste);
mi_paste.addActionListener(this);
mi_cut=new JMenuItem("剪下",ic_cut);
mi_cut.addActionListener(this);
mi_selectall=new JMenuItem("全選",ic_selectall);
mi_selectall.addActionListener(this);
mi_about=new JMenuItem("關於 My Editor",ic_about);
mi_about.addActionListener(this);
rbmi_1[0]=new JRadioButtonMenuItem("大 (22)");
rbmi_1[1]=new JRadioButtonMenuItem("中 (18)",true);
rbmi_1[2]=new JRadioButtonMenuItem("小 (14)");
bg_1=new ButtonGroup();
for(int i=0; i<rbmi_1.length; i++)
bg_1.add(rbmi_1[i]);
rbmi_2[0]=new JRadioButtonMenuItem("一般",true);
rbmi_2[1]=new JRadioButtonMenuItem("粗體");
rbmi_2[2]=new JRadioButtonMenuItem("斜體");
rbmi_2[3]=new JRadioButtonMenuItem("粗體+斜體");
bg_2=new ButtonGroup();
for(int i=0; i<rbmi_2.length; i++)
bg_2.add(rbmi_2[i]);
rbmi_3[0]=new JRadioButtonMenuItem("白底黑字",true);
rbmi_3[1]=new JRadioButtonMenuItem("黑底白字");
bg_3=new ButtonGroup();
for(int i=0; i<rbmi_3.length; i++)
bg_3.add(rbmi_3[i]);
mn_file=new JMenu(" 檔案(F) ");
mn_file.setMnemonic('F');
mn_file.add(mi_new);
mn_file.add(mi_open);
mn_file.add(mi_save);
mn_file.addSeparator();
mn_file.add(mi_exit);
mn_edit=new JMenu(" 編輯(E) ");
mn_edit.setMnemonic('E');
mn_edit.add(mi_copy);
mn_edit.add(mi_paste);
mn_edit.add(mi_cut);
mn_edit.addSeparator();
mn_edit.add(mi_selectall);
mn_size=new JMenu("文字大小");
mn_size.setIcon(ic_size);
for(int i=0; i<rbmi_1.length; i++)
mn_size.add(rbmi_1[i]);
mn_type=new JMenu("文字樣式");
mn_type.setIcon(ic_type);
for(int i=0; i<rbmi_2.length; i++)
mn_type.add(rbmi_2[i]);
mn_style=new JMenu("風格");
mn_style.setIcon(ic_style);
for(int i=0; i<rbmi_3.length; i++)
mn_style.add(rbmi_3[i]);
mn_option=new JMenu(" 設定(O) ");
mn_option.setMnemonic('O');
mn_option.add(mn_size);
mn_option.add(mn_type);
mn_option.addSeparator();
mn_option.add(mn_style);
mn_help=new JMenu(" 說明(H) ");
mn_help.setMnemonic('H');
mn_help.add(mi_about);
mb=new JMenuBar();
mb.setBorder(new BevelBorder(BevelBorder.RAISED));
mb.add(mn_file);
mb.add(mn_edit);
mb.add(mn_option);
mb.add(mn_help);
ta=new JTextArea();
ta.setFont(new Font("新細明體", Font.PLAIN, 18));
ta.setLineWrap(true);
sp=new JScrollPane(ta);
fm=new JFrame(title+" - 未命名");
fm.setBounds(100, 100, 500, 350);
fm.setIconImage(ic.getImage());
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(sp);
fm.setJMenuBar(mb);
}
Ch07()
{
initialize();
}
複製代碼
作者:
林宇翔
時間:
2017-2-11 17:43
(下半)
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mi_new)
{
ta.setText("");
fm.setTitle(title+" - 未命名");
}
else if(e.getSource()==mi_open)
{
int ret=fc.showOpenDialog(null);
if(ret==JFileChooser.APPROVE_OPTION)
{
try
{
String str;
File fi=fc.getSelectedFile();
BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
ta.setText(br.readLine());
do
{
str=br.readLine();
if(str==null)
break;
ta.append("\n"+str);
}while(true);
br.close();
fm.setTitle(title+" - "+fc.getName(fi));
}catch(Exception ex){}
}
}
else if(e.getSource()==mi_save)
{
int ret=fc.showSaveDialog(null);
if(ret==JFileChooser.APPROVE_OPTION)
{
try
{
File fi=fc.getSelectedFile();
BufferedWriter bw;
String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4);
System.out.println(ext);
String fiPath="";
if(fc.getFileFilter()==filter1)
{
if(ext.equals(".txt"))
fiPath=fi.getAbsolutePath();
else
fiPath=fi.getAbsolutePath()+".txt";
}
else
fiPath=fi.getAbsolutePath();
bw=new BufferedWriter(new FileWriter(fiPath));
bw.write(ta.getText().replaceAll("\n", "\r\n"));
bw.flush();
bw.close();
fm.setTitle(title+" - "+fc.getName(fi));
}catch(Exception ex){}
}
}
else if(e.getSource()==mi_exit)
System.exit(0);
else if(e.getSource()==mi_copy)
ta.copy();
else if(e.getSource()==mi_paste)
ta.paste();
else if(e.getSource()==mi_cut)
ta.cut();
else if(e.getSource()==mi_selectall)
ta.selectAll();
else if(e.getSource()==mi_about)
{
String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n林宇翔所開發,感謝您的使用!";
JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ic_developer);
}
}
public static void main(String[] args) {
new Ch07();
}
}
複製代碼
作者:
李允軒
時間:
2017-3-4 16:56
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.*;
import java.io.*;
public class Ch01 implements ActionListener{
String title="My Editor";
JFrame fm;
JScrollPane sp;
JTextArea ta;
ImageIcon ic,ic_cut,ic_exit,ic_new,ic_open,ic_size,ic_type,ic_style,ic_about,ic_developer,ic_paste,ic_save,ic_copy,ic_selectall;
JMenuBar mb;
JMenu mn_file,mn_edit,mn_help,mn_size,mn_type,mn_style,mn_option;
JMenuItem mi_new,mi_open,mi_save,mi_exit,mi_copy,mi_paste,mi_cut,mi_selectall,mi_about;
JFileChooser fc;
JRadioButtonMenuItem rbmi_1[]=new JRadioButtonMenuItem[3];
JRadioButtonMenuItem rbmi_2[]=new JRadioButtonMenuItem[4];
JRadioButtonMenuItem rbmi_3[]=new JRadioButtonMenuItem[2];
ButtonGroup bg_1,bg_2,bg_3;
FileNameExtensionFilter filter1,filter2;
void initialize(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e){}
filter1=new FileNameExtensionFilter("純文字文件","txt");
fc=new JFileChooser();
fc.addChoosableFileFilter(filter1);
fc.setFileFilter(filter1);
ic=new ImageIcon(Ch01.class.getResource("pic/editor.png"));
ic_cut=new ImageIcon(Ch01.class.getResource("pic/cut.png"));
ic_exit=new ImageIcon(Ch01.class.getResource("pic/exit.png"));
ic_new=new ImageIcon(Ch01.class.getResource("pic/new.png"));
ic_open=new ImageIcon(Ch01.class.getResource("pic/open.png"));
ic_paste=new ImageIcon(Ch01.class.getResource("pic/paste.png"));
ic_save=new ImageIcon(Ch01.class.getResource("pic/save.png"));
ic_copy=new ImageIcon(Ch01.class.getResource("pic/copy.png"));
ic_selectall=new ImageIcon(Ch01.class.getResource("pic/selectall.png"));
ic_about=new ImageIcon(Ch01.class.getResource("pic/about.png"));
ic_developer=new ImageIcon(Ch01.class.getResource("pic/developer.png"));
ic_size=new ImageIcon(Ch01.class.getResource("pic/size.png"));
ic_style=new ImageIcon(Ch01.class.getResource("pic/style.png"));
ic_type=new ImageIcon(Ch01.class.getResource("pic/type.png"));
mi_new=new JMenuItem("開新檔案",ic_new);
mi_new.addActionListener(this);
mi_open=new JMenuItem("開啟舊檔",ic_open);
mi_open.addActionListener(this);
mi_save=new JMenuItem("儲存檔案",ic_save);
mi_save.addActionListener(this);
mi_exit=new JMenuItem("結束",ic_exit);
mi_exit.addActionListener(this);
mi_copy=new JMenuItem("複製",ic_copy);
mi_copy.addActionListener(this);
mi_paste=new JMenuItem("貼上",ic_paste);
mi_paste.addActionListener(this);
mi_cut=new JMenuItem("剪下",ic_cut);
mi_cut.addActionListener(this);
mi_selectall=new JMenuItem("全選",ic_selectall);
mi_selectall.addActionListener(this);
mi_about=new JMenuItem("關於 My Editor",ic_about);
mi_about.addActionListener(this);
mn_help=new JMenu(" 關於(H) ");
mn_help.setMnemonic('H');
mn_help.add(mi_about);
rbmi_1[0]=new JRadioButtonMenuItem("大 (22)");
rbmi_1[1]=new JRadioButtonMenuItem("中 (18)",true);
rbmi_1[2]=new JRadioButtonMenuItem("小 (14)");
bg_1=new ButtonGroup();
for(int i=0; i<rbmi_1.length; i++)
bg_1.add(rbmi_1[i]);
rbmi_2[0]=new JRadioButtonMenuItem("一般",true);
rbmi_2[1]=new JRadioButtonMenuItem("粗體");
rbmi_2[2]=new JRadioButtonMenuItem("斜體");
rbmi_2[3]=new JRadioButtonMenuItem("粗體+斜體");
bg_2=new ButtonGroup();
for(int i=0; i<rbmi_2.length; i++)
bg_2.add(rbmi_2[i]);
rbmi_3[0]=new JRadioButtonMenuItem("白底黑字",true);
rbmi_3[1]=new JRadioButtonMenuItem("黑底白字");
bg_3=new ButtonGroup();
for(int i=0; i<rbmi_3.length; i++)
bg_3.add(rbmi_3[i]);
mn_size=new JMenu("文字大小");
mn_size.setIcon(ic_size);
for(int i=0; i<rbmi_1.length; i++)
mn_size.add(rbmi_1[i]);
mn_type=new JMenu("文字樣式");
mn_type.setIcon(ic_type);
for(int i=0; i<rbmi_2.length; i++)
mn_type.add(rbmi_2[i]);
mn_style=new JMenu("風格");
mn_style.setIcon(ic_style);
for(int i=0; i<rbmi_3.length; i++)
mn_style.add(rbmi_3[i]);
mn_option=new JMenu(" 設定(O) ");
mn_option.setMnemonic('O');
mn_option.add(mn_size);
mn_option.add(mn_type);
mn_option.addSeparator();
mn_option.add(mn_style);
mn_file=new JMenu(" 檔案(F) ");
mn_file.setMnemonic('F');
mn_file.add(mi_new);
mn_file.add(mi_open);
mn_file.add(mi_save);
mn_file.addSeparator();
mn_file.add(mi_exit);
mn_edit=new JMenu(" 編輯(E) ");
mn_edit.setMnemonic('E');
mn_edit.add(mi_copy);
mn_edit.add(mi_paste);
mn_edit.add(mi_cut);
mn_edit.addSeparator();
mn_edit.add(mi_selectall);
mb=new JMenuBar();
mb.setBorder(new BevelBorder(BevelBorder.RAISED));
mb.add(mn_file);
mb.add(mn_edit);
mb.add(mn_option);
mb.add(mn_help);
ta=new JTextArea();
ta.setFont(new Font("新細明體", Font.PLAIN, 18));
ta.setLineWrap(true);
sp=new JScrollPane(ta);
fm=new JFrame(title+" - 未命名");
fm.setBounds(100, 100, 500, 350);
fm.setIconImage(ic.getImage());
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(sp);
fm.setJMenuBar(mb);
}
複製代碼
作者:
李允軒
時間:
2017-3-4 16:57
Ch01(){
initialize();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==mi_new){
ta.setText("");
fm.setTitle(title+" - 未命名");
}
else if(e.getSource()==mi_open){
int ret=fc.showOpenDialog(null);
if(ret==JFileChooser.APPROVE_OPTION){
try{
String str;
File fi=fc.getSelectedFile();
BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
ta.setText(br.readLine());
do{
str=br.readLine();
if(str==null)
break;
ta.append("\n"+str);
}while(true);
br.close();
fm.setTitle(title+" - "+fc.getName(fi));
}catch(Exception ex){}
}
}
else if(e.getSource()==mi_save){
int ret=fc.showSaveDialog(null);
if(ret==JFileChooser.APPROVE_OPTION){
try{
File fi=fc.getSelectedFile();
BufferedWriter bw;
String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4); //file extension 副檔名
System.out.println(ext);
String fiPath="";
if(fc.getFileFilter()==filter1){
if(ext.equals(".txt"))
fiPath=fi.getAbsolutePath();
else
fiPath=fi.getAbsolutePath()+".txt";
}
else
fiPath=fi.getAbsolutePath();
bw=new BufferedWriter(new FileWriter(fiPath));
bw.write(ta.getText().replaceAll("\n", "\r\n"));
bw.flush();
bw.close();
fm.setTitle(title+" - "+fc.getName(fi));
}catch(Exception ex){}
}
}
else if(e.getSource()==mi_exit)
System.exit(0);
else if(e.getSource()==mi_copy)
ta.copy();
else if(e.getSource()==mi_paste)
ta.paste();
else if(e.getSource()==mi_cut)
ta.cut();
else if(e.getSource()==mi_selectall)
ta.selectAll();
else if(e.getSource() == mi_about){
String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n李允軒所開發,感謝您的使用!";
JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ic_developer);
}
}
public static void main(String[] args){
new Ch01();
}
}
複製代碼
回復
5#
李允軒
作者:
張彥承
時間:
2017-3-11 17:26
import java.awt.Font;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class Ch100 implements ActionListener{
String title="My Editor";
JFrame fm,fm2;
JScrollPane sp;
JTextArea ta;
ImageIcon ic,ic_cut,ic_exit,ic_new,ic_open,ic_paste,ic_save,ic_copy,ic_selectall,ic_about,ic_developer,ic_size,ic_style,ic_type;
JMenuBar mb;
JMenu mn_file,mn_edit,mn_option,mn_help,mn_size,mn_type,mn_style;
JMenuItem mi_new,mi_open,mi_save,mi_exit,mi_copy,mi_paste,mi_cut,mi_selectall,mi_about;
JRadioButtonMenuItem rbmi_1[]=new JRadioButtonMenuItem[3];
JRadioButtonMenuItem rbmi_2[]=new JRadioButtonMenuItem[4];
JRadioButtonMenuItem rbmi_3[]=new JRadioButtonMenuItem[2];
ButtonGroup bg_1,bg_2,bg_3;
JFileChooser fc;
FileNameExtensionFilter filter1;
void initialize()
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e){}
filter1 = new FileNameExtensionFilter("文字文件(*.txt)", "txt");
fc=new JFileChooser();
fc.setFileFilter(filter1);
ic=new ImageIcon(Ch100.class.getResource("pic/editor.png"));
ic_cut=new ImageIcon(Ch100.class.getResource("pic/cut.png"));
ic_exit=new ImageIcon(Ch100.class.getResource("pic/exit.png"));
ic_new=new ImageIcon(Ch100.class.getResource("pic/new.png"));
ic_open=new ImageIcon(Ch100.class.getResource("pic/open.png"));
ic_paste=new ImageIcon(Ch100.class.getResource("pic/paste.png"));
ic_save=new ImageIcon(Ch100.class.getResource("pic/save.png"));
ic_copy=new ImageIcon(Ch100.class.getResource("pic/copy.png"));
ic_selectall=new ImageIcon(Ch100.class.getResource("pic/selectall.png"));
ic_about=new ImageIcon(Ch100.class.getResource("pic/about.png"));
ic_developer=new ImageIcon(Ch100.class.getResource("pic/developer.png"));
ic_size=new ImageIcon(Ch100.class.getResource("pic/size.png"));
ic_type=new ImageIcon(Ch100.class.getResource("pic/type.png"));
ic_style=new ImageIcon(Ch100.class.getResource("pic/style.png"));
mi_new=new JMenuItem("開新檔案",ic_new);
mi_new.addActionListener(this);
mi_open=new JMenuItem("開啟舊檔",ic_open);
mi_open.addActionListener(this);
mi_save=new JMenuItem("儲存檔案",ic_save);
mi_save.addActionListener(this);
mi_exit=new JMenuItem("結束",ic_exit);
mi_exit.addActionListener(this);
mi_copy=new JMenuItem("複製",ic_copy);
mi_copy.addActionListener(this);
mi_paste=new JMenuItem("貼上",ic_paste);
mi_paste.addActionListener(this);
mi_cut=new JMenuItem("剪下",ic_cut);
mi_cut.addActionListener(this);
mi_selectall=new JMenuItem("全選",ic_selectall);
mi_selectall.addActionListener(this);
mi_about=new JMenuItem("關於 My Editor",ic_about);
mi_about.addActionListener(this);
rbmi_1[0]=new JRadioButtonMenuItem("大 (22)");
rbmi_1[1]=new JRadioButtonMenuItem("中 (18)",true);
rbmi_1[2]=new JRadioButtonMenuItem("小 (14)");
bg_1=new ButtonGroup();
for(int i=0; i<rbmi_1.length; i++)
bg_1.add(rbmi_1[i]);
rbmi_2[0]=new JRadioButtonMenuItem("一般",true);
rbmi_2[1]=new JRadioButtonMenuItem("粗體");
rbmi_2[2]=new JRadioButtonMenuItem("斜體");
rbmi_2[3]=new JRadioButtonMenuItem("粗體+斜體");
bg_2=new ButtonGroup();
for(int i=0; i<rbmi_2.length; i++)
bg_2.add(rbmi_2[i]);
rbmi_3[0]=new JRadioButtonMenuItem("白底黑字",true);
rbmi_3[1]=new JRadioButtonMenuItem("黑底白字");
bg_3=new ButtonGroup();
for(int i=0; i<rbmi_3.length; i++)
bg_3.add(rbmi_3[i]);
複製代碼
作者:
張彥承
時間:
2017-3-11 17:33
mn_file=new JMenu(" 檔案(F) ");
mn_file.setMnemonic('F');
mn_file.add(mi_new);
mn_file.add(mi_open);
mn_file.add(mi_save);
mn_file.addSeparator();
mn_file.add(mi_exit);
mn_edit=new JMenu(" 編輯(E) ");
mn_edit.setMnemonic('E');
mn_edit.add(mi_copy);
mn_edit.add(mi_paste);
mn_edit.add(mi_cut);
mn_edit.addSeparator();
mn_edit.add(mi_selectall);
mn_size=new JMenu("文字大小");
mn_size.setIcon(ic_size);
for(int i=0; i<rbmi_1.length; i++)
mn_size.add(rbmi_1[i]);
mn_type=new JMenu("文字樣式");
mn_type.setIcon(ic_type);
for(int i=0; i<rbmi_2.length; i++)
mn_type.add(rbmi_2[i]);
mn_style=new JMenu("風格");
mn_style.setIcon(ic_style);
for(int i=0; i<rbmi_3.length; i++)
mn_style.add(rbmi_3[i]);
mn_option=new JMenu(" 設定(O) ");
mn_option.setMnemonic('O');
mn_option.add(mn_size);
mn_option.add(mn_type);
mn_option.addSeparator();
mn_option.add(mn_style);
mn_help=new JMenu(" 說明(H) ");
mn_help.setMnemonic('H');
mn_help.add(mi_about);
mb=new JMenuBar();
mb.setBorder(new BevelBorder(BevelBorder.RAISED)); //設定具陰影效果的邊框
mb.add(mn_file);
mb.add(mn_edit);
mb.add(mn_option);
mb.add(mn_help);
ta=new JTextArea();
ta.setFont(new Font("新細明體", Font.PLAIN, 18));
ta.setLineWrap(true);
sp=new JScrollPane(ta);
fm=new JFrame(title+" - 未命名");
fm.setBounds(100, 100, 500, 350);
fm.setIconImage(ic.getImage());
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(sp);
fm.setJMenuBar(mb);
}
Ch100()
{
initialize();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mi_new)
{
ta.setText("");
fm.setTitle(title+" - 未命名");
}
else if(e.getSource()==mi_open)
{
int ret=fc.showOpenDialog(null);
if(ret==JFileChooser.APPROVE_OPTION)
{
try
{
String str;
File fi=fc.getSelectedFile();
BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
ta.setText(br.readLine());
do
{
str=br.readLine();
if(str==null)
break;
ta.append("\n"+str);
}while(true);
br.close();
fm.setTitle(title+" - "+fc.getName(fi));
}catch(Exception ex){}
}
}
else if(e.getSource()==mi_save)
{
int ret=fc.showSaveDialog(null);
if(ret==JFileChooser.APPROVE_OPTION)
{
try
{
File fi=fc.getSelectedFile();
BufferedWriter bw;
String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4);
System.out.println(ext);
String fiPath="";
if(fc.getFileFilter()==filter1)
{
if(ext.equals(".txt"))
fiPath=fi.getAbsolutePath();
else
fiPath=fi.getAbsolutePath()+".txt";
}
else
fiPath=fi.getAbsolutePath();
bw=new BufferedWriter(new FileWriter(fiPath));
bw.write(ta.getText().replaceAll("\n", "\r\n"));
bw.flush();
bw.close();
fm.setTitle(title+" - "+fc.getName(fi));
}catch(Exception ex){}
}
}
else if(e.getSource()==mi_exit)
System.exit(0);
else if(e.getSource()==mi_copy)
ta.copy();
else if(e.getSource()==mi_paste)
ta.paste();
else if(e.getSource()==mi_cut)
ta.cut();
else if(e.getSource()==mi_selectall)
ta.selectAll();
else if(e.getSource()==mi_about)
{
String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n林宇翔所開發,感謝您的使用!";
JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ic_developer);
}
}
public static void main(String[] args)
{
new Ch100();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2