標題:
文字編輯器 (九)
[打印本頁]
作者:
tonyh
時間:
2020-3-21 14:02
標題:
文字編輯器 (九)
使用 JRadioButtonMenuItem 類別產生帶有 radio button 的 menu item,再將同一區塊的選項加入由 ButtonGroup 類別所產生的群組物件中,使於同一區塊只有一個選項可被選取。
本帖隱藏的內容需要回復才可以瀏覽
素材:
作者:
蔡幸融
時間:
2020-3-21 15:26
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Ch04 implements ActionListener{
JFrame fm;
JScrollPane sp;
JTextArea ta;
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_size[]=new JRadioButtonMenuItem[3];
JRadioButtonMenuItem rbmi_type[]=new JRadioButtonMenuItem[4];
JRadioButtonMenuItem rbmi_style[]=new JRadioButtonMenuItem[2];
ButtonGroup btg_1, btg_2, btg_3;
ImageIcon ic, ii_new, ii_open, ii_save, ii_exit, ii_copy, ii_paste, ii_cut, ii_selectall, ii_about, ii_developer, ii_size, ii_type, ii_style;
JFileChooser fc;
FileNameExtensionFilter filter;
Ch04()
{
initialize();
}
void initialize()
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {}
ic=new ImageIcon(Ch04.class.getResource("pic/editor.png"));
ii_new=new ImageIcon(Ch04.class.getResource("pic/new.png"));
ii_open=new ImageIcon(Ch04.class.getResource("pic/open.png"));
ii_save=new ImageIcon(Ch04.class.getResource("pic/save.png"));
ii_exit=new ImageIcon(Ch04.class.getResource("pic/exit.png"));
ii_copy=new ImageIcon(Ch04.class.getResource("pic/copy.png"));
ii_paste=new ImageIcon(Ch04.class.getResource("pic/paste.png"));
ii_cut=new ImageIcon(Ch04.class.getResource("pic/cut.png"));
ii_selectall=new ImageIcon(Ch04.class.getResource("pic/selectall.png"));
ii_about=new ImageIcon(Ch04.class.getResource("pic/about.png"));
ii_developer=new ImageIcon(Ch04.class.getResource("pic/developer.png"));
ii_size=new ImageIcon(Ch04.class.getResource("pic/size.png"));
ii_type=new ImageIcon(Ch04.class.getResource("pic/type.png"));
ii_style=new ImageIcon(Ch04.class.getResource("pic/style.png"));
filter=new FileNameExtensionFilter("純文字檔案", "txt");
fc=new JFileChooser();
fc.addChoosableFileFilter(filter);
fc.setFileFilter(filter);
mi_new=new JMenuItem("開新檔案",ii_new);
mi_new.addActionListener(this);
mi_open=new JMenuItem("開啟舊檔",ii_open);
mi_open.addActionListener(this);
mi_save=new JMenuItem("儲存檔案",ii_save);
mi_save.addActionListener(this);
mi_exit=new JMenuItem("結束",ii_exit);
mi_exit.addActionListener(this);
mi_copy=new JMenuItem("複製",ii_copy);
mi_copy.addActionListener(this);
mi_paste=new JMenuItem("貼上",ii_paste);
mi_paste.addActionListener(this);
mi_cut=new JMenuItem("剪下",ii_cut);
mi_cut.addActionListener(this);
mi_selectall=new JMenuItem("全選",ii_selectall);
mi_selectall.addActionListener(this);
mi_about=new JMenuItem("關於 My Editor",ii_about);
mi_about.addActionListener(this);
rbmi_size[0]=new JRadioButtonMenuItem("大(22)");
rbmi_size[1]=new JRadioButtonMenuItem("中(18)", true);
rbmi_size[2]=new JRadioButtonMenuItem("小(14)");
btg_1=new ButtonGroup();
for(int i=0; i<rbmi_size.length; i++)
btg_1.add(rbmi_size[i]);
rbmi_type[0]=new JRadioButtonMenuItem("一般", true);
rbmi_type[1]=new JRadioButtonMenuItem("粗體");
rbmi_type[2]=new JRadioButtonMenuItem("斜體");
rbmi_type[3]=new JRadioButtonMenuItem("粗體+斜體");
btg_2=new ButtonGroup();
for(int i=0; i<rbmi_type.length; i++)
btg_2.add(rbmi_type[i]);
rbmi_style[0]=new JRadioButtonMenuItem("白底黑字", true);
rbmi_style[1]=new JRadioButtonMenuItem("黑底白字");
btg_3=new ButtonGroup();
for(int i=0; i<rbmi_style.length; i++)
btg_3.add(rbmi_style[i]);
mn_size=new JMenu("文字大小");
mn_size.setIcon(ii_size);
for(int i=0; i<rbmi_size.length; i++)
mn_size.add(rbmi_size[i]);
mn_style=new JMenu("文字樣式");
mn_style.setIcon(ii_style);
for(int i=0; i<rbmi_style.length; i++)
mn_style.add(rbmi_style[i]);
mn_type=new JMenu("風格");
mn_type.setIcon(ii_type);
for(int i=0; i<rbmi_type.length; i++)
mn_type.add(rbmi_type[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_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, 20));
ta.setLineWrap(true);
sp=new JScrollPane(ta);
fm=new JFrame("My Editor");
fm.setIconImage(ic.getImage());
fm.setBounds(100, 100, 500, 350);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(sp);
fm.setJMenuBar(mb);
}
public static void main(String[] args) {
new Ch04();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==mi_open)
{
int ret=fc.showOpenDialog(null);
if(ret==JFileChooser.APPROVE_OPTION)
{
try {
File fl=fc.getSelectedFile();
BufferedReader br=new BufferedReader(new FileReader(fl.getAbsolutePath()));
String str=br.readLine();
String tmp=br.readLine();
while(tmp!=null)
{
str+="\r\n";
str+=tmp;
tmp=br.readLine();
}
ta.setText(str);
br.close();
fm.setTitle("My Editor - "+fl.getName());
} catch (Exception ex) {}
}
}
if(e.getSource()==mi_save)
{
int ret=fc.showSaveDialog(null);
if(ret==JFileChooser.APPROVE_OPTION)
{
try {
File fl=fc.getSelectedFile();
String path="";
if(fl.getAbsolutePath().substring(fl.getAbsolutePath().length()-4).equals(".txt"))
{
path=fl.getAbsolutePath();
fm.setTitle("My Editor - "+fl.getName());
}else
{
path=fl.getAbsolutePath()+".txt";
fm.setTitle("My Editor - "+fl.getName()+".txt");
}
BufferedWriter bw=new BufferedWriter(new FileWriter(path));
bw.write(ta.getText().replaceAll("\n", "\r\n"));
bw.flush();
bw.close();
} catch (Exception ex) {}
}
}
if(e.getSource()==mi_new)
{
ta.setText("");
fm.setTitle("My Editor - 未命名");
}
if(e.getSource()==mi_exit)
System.exit(0);
if(e.getSource()==mi_copy)
ta.copy();
if(e.getSource()==mi_paste)
ta.paste();
if(e.getSource()==mi_cut)
ta.cut();
if(e.getSource()==mi_selectall)
ta.selectAll();
if(e.getSource()==mi_about)
{
String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n林宇翔所開發,感謝您的使用!";
JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ii_developer);
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2