package 文本編輯器;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
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.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class TextEditBox extends JFrame {
// 添加屬性
private JComboBox combox_name, combox_size;// 字體、字號組合框
private JButton button_larger,button_smaller,button_color;//字體變大變小和顏色選擇器
private JCheckBox checkb_bold, checkb_italic;// 粗體、斜體複選框
private JPopupMenu popupmenu;
private JTextArea ta = new JTextArea();
private JScrollPane sp = new JScrollPane(ta);
//查找對話框屬性
private JTextField tf_search;
private JButton button_next;
//
private int key=0;
public TextEditBox(String str) {
super(str);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Dimension dim = getToolkit().getScreenSize(); // 獲得屏幕分辨率
this.setBounds(dim.width / 4, dim.height / 4, 700, 480);
JToolBar toolbar = new JToolBar(); // 創建工具欄
this.add(toolbar, BorderLayout.NORTH); // 工具欄添加到窗格北部
this.add(sp);
ta.setLineWrap(true);// 換行
//////////////////字體
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontsName = ge.getAvailableFontFamilyNames(); // 獲得系統字體
combox_name = new JComboBox(fontsName);
toolbar.add(combox_name);
combox_name.addActionListener(new ActionListener() {// 字號
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//獲得字體名
Font font = ta.getFont(); //獲得文本區的當前字體對象
int style = font.getStyle(); //獲得字形
int size = font.getSize();
ta.setFont(new Font(fontname, style, size));
}
});
/////////////////字號
String sizestr[] = { "20", "30", "40", "50", "60", "70" ,"80","90","100"};
combox_size = new JComboBox(sizestr);
combox_size.setEditable(true);
toolbar.add(combox_size);
combox_size.addActionListener(new ActionListener() {// 字號
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//獲得字體名
int size = Integer.parseInt((String)combox_size.getSelectedItem());
Font font = ta.getFont(); //獲得文本區的當前字體對象
int style = font.getStyle(); //獲得字形
ta.setFont(new Font(fontname, style, size));
}
});
////////////////////字號加減按鈕
button_larger=new JButton("A+");
toolbar.add(button_larger);
button_larger.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//獲得字體名
Font font = ta.getFont(); //獲得文本區的當前字體對象
int style = font.getStyle(); //獲得字形
int size = font.getSize()+5;
ta.setFont(new Font(fontname, style, size));
}
});
button_smaller=new JButton("A-");
toolbar.add(button_smaller);
button_smaller.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//獲得字體名
Font font = ta.getFont(); //獲得文本區的當前字體對象
int style = font.getStyle(); //獲得字形
int size = font.getSize()-5;
ta.setFont(new Font(fontname, style, size));
}
});
/////////////////J
/////////////////粗體和斜體
checkb_bold = new JCheckBox("粗體"); //字形複選框
toolbar.add(checkb_bold);
checkb_bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//獲得字體名
Font font = ta.getFont(); //獲得文本區的當前字體對象
int style = font.getStyle(); //獲得字形
int size = font.getSize();
style = style ^ 1;
ta.setFont(new Font(fontname, style, size));
}
});
checkb_italic = new JCheckBox("斜體");
toolbar.add(checkb_italic);
checkb_italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//獲得字體名
Font font = ta.getFont(); //獲得文本區的當前字體對象
int style = font.getStyle(); //獲得字形
int size = font.getSize();
style = style ^ 2;
ta.setFont(new Font(fontname, style, size));
}
});
////////////////
JRadioButton radiob_color[];
String colorstr[]={"紅","綠","藍"};
ButtonGroup bgroup_color = new ButtonGroup(); //按鈕組
radiob_color = new JRadioButton[colorstr.length]; //顏色單選按鈕數組
for (int i=0; i<radiob_color.length; i++){
radiob_color[i]=new JRadioButton(colorstr[i]);
bgroup_color.add(radiob_color[i]); //添加到按鈕組
toolbar.add(radiob_color[i]); //添加到工具欄
}
radiob_color[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.red);// 設置顏色
}
});
radiob_color[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.green);
}
});
radiob_color[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.blue);
}
});
///////////////顏色選擇器
button_color=new JButton("其他");
toolbar.add(button_color);
button_color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color color;
color=JColorChooser.showDialog(TextEditBox.this,"顏色選擇", Color.black);
ta.setForeground(color);// 設置顏色
}
});
////////////////鼠標事件
ta.addMouseListener(new MouseAdapter() {// 鼠標事件處理方法,右擊彈出菜單
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == MouseEvent.BUTTON3_MASK) // 單擊的是鼠標右鍵
popupmenu.show(ta, e.getX(), e.getY()); // 在鼠標單擊處顯示快捷菜單
}
});
////////////////
this.addmyMenu(); //調用自定義方法,添加菜單
this.setVisible(true);
}
private void addmyMenu() {// 添加主菜單、快捷菜單、對話框
JMenuBar menubar = new JMenuBar(); // 菜單欄
this.setJMenuBar(menubar); // 添加菜單欄
String menustr[] = { "文件", "編輯", "工具", "幫助" };
JMenu menu[] = new JMenu[menustr.length];
for (int i = 0; i < menustr.length; i++) {
menu[i] = new JMenu(menustr[i]); // 菜單
menubar.add(menu[i]); // 菜單欄中加入菜單
}
////////////////////////////////
JMenuItem menuitem_open = new JMenuItem("打開");
menu[0].add(menuitem_open);
menuitem_open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
int result = filechooser.showOpenDialog(TextEditBox.this);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File file = filechooser.getSelectedFile();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
ta.setText("");
String text;
while ((text = br.readLine()) != null) {
ta.append(text);
}
fr.close();
br.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(TextEditBox.this,"打開文檔出錯!");
}
}
}
});
JMenuItem menuitem_save = new JMenuItem("保存");
menu[0].add(menuitem_save);
menuitem_save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
int result = filechooser.showSaveDialog(TextEditBox.this);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File file = filechooser.getSelectedFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
String text=ta.getText();
fw.write(text);
fw.close();
bw.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(TextEditBox.this,"打開文檔出錯!");
}
}
}
});
menu[0].addSeparator(); // 加分隔線
JMenuItem menuitem_exit = new JMenuItem("退出");
menu[0].add(menuitem_exit);
menuitem_exit.addActionListener(new ActionListener() {// 退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
/////////////////////////////
JMenu menu_style = new JMenu("字形");
JCheckBoxMenuItem checkboxmenuitem_bold = new JCheckBoxMenuItem("粗體");
menu_style.add(checkboxmenuitem_bold);
JCheckBoxMenuItem checkboxmenuitem_italic = new JCheckBoxMenuItem("斜體");
menu_style.add(checkboxmenuitem_italic);
menu[1].add(menu_style); // 菜單加入到菜單中成爲二級菜單
checkboxmenuitem_bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//獲得字體名
Font font = ta.getFont(); //獲得文本區的當前字體對象
int style = font.getStyle(); //獲得字形
int size = font.getSize();
style = style ^ 1;
ta.setFont(new Font(fontname, style, size));
}
});
checkboxmenuitem_italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//獲得字體名
Font font = ta.getFont(); //獲得文本區的當前字體對象
int style = font.getStyle(); //獲得字形
int size = font.getSize();
style = style ^ 2;
ta.setFont(new Font(fontname, style, size));
}
});
////////////////////////////
JMenu menu_color = new JMenu("顏色");
menu[1].add(menu_color);
ButtonGroup buttongroup = new ButtonGroup();
String colorstr[] = { "紅", "綠", "藍" };
JRadioButtonMenuItem rbmi_color[] = new JRadioButtonMenuItem[colorstr.length];
for (int i = 0; i < rbmi_color.length; i++) {
rbmi_color[i] = new JRadioButtonMenuItem(colorstr[i]); // 單選菜單項
buttongroup.add(rbmi_color[i]);
menu_color.add(rbmi_color[i]);
}
rbmi_color[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.red);
}
});
rbmi_color[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.green);
}
});
rbmi_color[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setForeground(Color.blue);
}
});
/////////////////////////////////
JMenuItem menuitem_countwordsnum = new JMenuItem("字數統計");
menu[2].add(menuitem_countwordsnum);
menuitem_countwordsnum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int count=0;
for(int i=0;i<ta.getText().length();i++){
if(!ta.getText().substring(i,i+1).equals(" ")){
count++;
}
}
JOptionPane.showMessageDialog(TextEditBox.this, "文本框中一共有"+count+"個字符!");
}
});
menu[2].addSeparator(); // 加分隔線
JMenuItem menuitem_search = new JMenuItem("查找");
menu[2].add(menuitem_search);
menuitem_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new MessageJDialog();
button_next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str_search=tf_search.getText();
int len = str_search.length();
for (int i = key; i < ta.getText().length() - len + 1; i++) {
String str_record = ta.getText().substring(i, i + len);
if (str_record.equals(str_search)) {
key = i + 1;
ta.requestFocus();
ta.select(i, i + len);
return;
}
}
}
});
key=0;
}
});
JMenuItem menuitem_replace = new JMenuItem("替換");
menu[2].add(menuitem_replace);
menuitem_replace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str_replace=JOptionPane.showInputDialog(TextEditBox.this,
"請輸入你要替換的字符串:" );
String str_replacelater=JOptionPane.showInputDialog(TextEditBox.this,
"請輸入你要用來替換的內容:" );
int len=str_replace.length();
for(int i=0;i<ta.getText().length()-len+1;i++){
String str_record=ta.getText().substring(i, i+len);
if(str_record.equals(str_replace)){
ta.replaceRange(str_replacelater,i, i+len);
}
}
}
});
/////////////////////////////////
JMenuItem menuitem_about = new JMenuItem("關於");
menu[3].add(menuitem_about);
menuitem_about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(TextEditBox.this,"文本編輯器v1.0 開發者:hhtx");
}
});
////////////////////////////////////////////////// 快捷菜單對象
popupmenu = new JPopupMenu();
String menuitemstr[] = { "剪切", "複製", "粘貼" };
JMenuItem popmenuitem[] = new JMenuItem[menuitemstr.length];
for (int i = 0; i < popmenuitem.length; i++) {
popmenuitem[i] = new JMenuItem(menuitemstr[i]);// 菜單項
popupmenu.add(popmenuitem[i]);// 快捷菜單加入菜單項
}
popmenuitem[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));// 設置快捷鍵Ctrl+X
popmenuitem[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));// 設置快捷鍵Ctrl+C
popmenuitem[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK));// 設置快捷鍵Ctrl+V
popmenuitem[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.cut(); //將選中文本剪切送系統剪貼板
}
});
popmenuitem[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.copy(); //將選中文本複製送系統剪貼板
}
});
popmenuitem[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.paste();//剪貼板的文本粘貼在當前位置
}
});
ta.add(popupmenu); // 文本區添加快捷菜單
}
//
private class MessageJDialog extends JDialog {
private JLabel lable_tip;
private JPanel panel_next = new JPanel();
private JPanel panel_search = new JPanel();
private JPanel panel_tip = new JPanel();
public MessageJDialog() {
super(TextEditBox.this, "查找");
this.setSize(300, 170);
this.setLocation(TextEditBox.this.getX() + 200,
TextEditBox.this.getY() + 200);
this.setLayout(new GridLayout(3, 1));
//
ImageIcon imageIcon = new ImageIcon("img/search.png");
lable_tip = new JLabel("請輸入你要查找的字符串:", imageIcon, JLabel.LEFT);
panel_tip.add(lable_tip);
this.add(panel_tip);
tf_search = new JTextField(20);
panel_search.add(tf_search);
this.add(panel_search);
button_next = new JButton("查找下一個");
panel_next.add(button_next);
this.add(panel_next);
this.setVisible(true);
}
}
public static void main(String args[]) {
new TextEditBox("文本編輯器v1.0");
}
}
java文本編輯器v2.0 圖形用戶界面
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.