教你用JAVA寫文本編輯器(三)
大家好,接下來的部分可能有點亂,但是并不復雜,我希望我能盡量給大家捋清楚思路。
老套路,這是我的前兩篇系列,需要的同學了解一下: JAVA寫文本編輯器(二) JAVA寫文本編輯器(一)
下面我們要實現(xiàn)的是一個點擊選擇文本格式的窗口,這里同樣是要畫一個窗口,跟(二)差不多。要實現(xiàn)的功能呢,主要是有幾個ComboBox彈出list出來,選中的屬性改變下面的文本。最下面兩個按鈕,確定則主窗口文本改變,取消則不改變。
在這里我建議大家可以先重新開一個工程,然后一步一步的測試完成后,將代碼拷回來,把main刪掉就可以了。
剛剛提到的界面最上方有幾個下拉框,這個需要用JComboBox實現(xiàn),而內(nèi)容的填充呢,需要相對應的數(shù)組。
public class about_Format extends JFrame{ private JComboBox choose_word_style; private JComboBox choose_word_big; private JComboBox choose_word_pattern; private JComboBox choose_word_color; private String[] styles = {"宋體","黑體","楷體","微軟雅黑","隸書"}; private String[] colors = {"紅色","藍色","綠色","黑色","白色","黃色"}; private String[] word_big = {"2","4","8","16","24","32","64","72"}; private String[] pattern = {"常規(guī)","傾斜","粗體"}; private JPanel paneNorth;//用于裝四個ComboBox public about_Format() { initBox(); initLocation(); this.setSize(550,200); this.setTitle("文字格式"); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } /** * 初始化布局 * 將每個控件按照一定得布局排在this窗口中 */ public void initLocation() { paneNorth = new JPanel(); paneNorth.add(new JLabel("字體:")); paneNorth.add(choose_word_style); paneNorth.add(new JLabel("字號:")); paneNorth.add(choose_word_big); paneNorth.add(new JLabel("字形:")); paneNorth.add(choose_word_pattern); paneNorth.add(new JLabel("顏色:")); paneNorth.add(choose_word_color); this.add(paneNorth,BorderLayout.NORTH); } /** * 初始化幾個comboBox * 把相應的選項加入 */ public void initBox() { choose_word_style = new JComboBox(styles); choose_word_big = new JComboBox(word_big); choose_word_pattern = new JComboBox(pattern); choose_word_color = new JComboBox(colors); } public static void main(String[] args) { about_Format a = new about_Format(); } }
首先我們在類內(nèi)聲明了變量,然后通過initBox()方法,進行初始化,這樣顯得結(jié)構(gòu)比較整齊。方法內(nèi)將comboBox實例化,并且將相應的字符串數(shù)組放入,這樣list效果就會出來了。
接下來,我們需要一個容器panel來把四個JComboBox裝進去,所以在initLocation()方法里實例化一個panel,把一大堆JLabel,JComboBox裝進去了。然后再將panel裝進父窗體,設置屬性north。
下來是一個文本顯示區(qū)域,可以有很多種,我這里選了JTextField,這里需要提前講一下,這里我還提前定義了一堆的字體屬性,是為了最終子父窗體屬性的影響而存在的,這里不需要在意:
public class about_Format extends JFrame{ ... private JPanel paneNorth;//用于裝四個ComboBox private JPanel paneCenter; private JTextField showText ; // 用一個font來裝選中的的屬性,每選中一次,對對應的屬性修改 //然后集成一個font里進行修改 //對selectedFont 設置默認屬性 private Font selectedFont = new Font("黑體",Font.PLAIN, 32); private String selectedStyle = "宋體"; private int selectedBig = 32; private int selectedPattern = Font.PLAIN; private Color selectedColor = Color.BLACK; public about_Format() { initBox(); initText(); // 這里要放在Location前因為initText里有元素被Location訪問 否則會出現(xiàn)空指針異常 initLocation(); this.setSize(550,200); this.setTitle("文字格式"); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } /** * 初始化布局 * 將每個控件按照一定得布局排在this窗口中 */ public void initLocation() { paneNorth = new JPanel(); ... paneCenter = new JPanel(); paneCenter.add(showText); this.add(paneCenter, BorderLayout.CENTER); } /** * 初始化展示字體區(qū)域 */ public void initText() { showText = new JTextField("字體展示"); showText.setFont(selectedFont); showText.setEditable(false); showText.setSize(100,160); //showText.setForeground(Color.red); } /** * 初始化幾個comboBox * 把相應的選項加入 */ public void initBox() { ... } public static void main(String[] args) { about_Format a = new about_Format(); } }
相信到這里大家的思路都清晰了吧,其實沒有那么復雜。我們只需要把每一個部件拆開來看,一個個的去實現(xiàn)就可以了。
接下來我們只需要添加兩個按鈕,然后統(tǒng)一響應JComboBox和Button的事件就好了,照例在面板下部添加button,然后繼承接口,處理回調(diào)事件。
添加兩個button的代碼:
public class about_Format extends JFrame{ ... private JPanel paneNorth;//用于裝四個ComboBox private JPanel paneCenter; private JPanel paneSouth; private JButton btn_ok; private JButton btn_cancel; private JTextField showText ; ... public about_Format() { initBox(); initText(); initButton(); initLocation(); this.setSize(550,200); this.setTitle("文字格式"); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } /** * 初始化ok 和cancel 兩個按鈕 */ private void initButton() { btn_ok = new JButton("OK"); btn_cancel = new JButton("CANCEL"); } /** * 初始化布局 * 將每個控件按照一定得布局排在this窗口中 */ public void initLocation() { ... this.add(paneCenter, BorderLayout.CENTER); paneSouth = new JPanel(); paneSouth.add(btn_ok); paneSouth.add(btn_cancel); this.add(paneSouth, BorderLayout.SOUTH); } /** * 初始化展示字體區(qū)域 */ public void initText() { showText = new JTextField("字體展示"); showText.setFont(selectedFont); showText.setEditable(false); showText.setSize(100,160); //showText.setForeground(Color.red); } /** * 初始化幾個comboBox * 把相應的選項加入 */ public void initBox() { ... } public static void main(String[] args) { about_Format a = new about_Format(); } }
下面統(tǒng)一添加監(jiān)聽器,這回我們用ItemListener,ActionListener兩個接口,我們可以把button跟item分開來。如果想要統(tǒng)一監(jiān)聽也可以自己改。
添加監(jiān)聽器后代碼:
public class about_Format extends JFrame implements ItemListener,ActionListener{ ... ... private JButton btn_ok; private JButton btn_cancel; private JTextField showText ; // 用一個font來裝選中的的屬性,每選中一次,對對應的屬性修改 //然后集成一個font里進行修改 //對selectedFont 設置默認屬性 private Font selectedFont = new Font("黑體",Font.PLAIN, 32); private String selectedStyle = "宋體"; private int selectedBig = 32; private int selectedPattern = Font.PLAIN; private Color selectedColor = Color.BLACK; public about_Format() { initBox(); initText(); initButton(); initLocation(); initListener(); addBtnListener(); this.setSize(550,200); this.setTitle("文字格式"); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } /** * 時間監(jiān)聽回調(diào)函數(shù) * 對每個item做出事件響應 */ @Override public void itemStateChanged(ItemEvent e) { if (e.getItem() == "宋體") { selectedStyle = "宋體"; renewFont(); }else if (e.getItem() == "黑體") { selectedStyle = "黑體"; renewFont(); }else if (e.getItem() == "楷體") { selectedStyle = "楷體"; renewFont(); }else if (e.getItem() == "微軟雅黑") { selectedStyle = "微軟雅黑"; renewFont(); }else if (e.getItem() == "隸書") { selectedStyle = "隸書"; renewFont(); }else if (e.getItem() == "常規(guī)") { selectedPattern = Font.PLAIN; renewFont(); }else if (e.getItem() == "傾斜") { selectedPattern = Font.ITALIC; renewFont(); }else if (e.getItem() == "粗體") { selectedPattern = Font.BOLD; renewFont(); }else if (e.getItem() == "2") { selectedBig = 2; renewFont(); }else if (e.getItem() == "4") { selectedBig = 4; renewFont(); }else if (e.getItem() == "8") { selectedBig = 8; renewFont(); }else if (e.getItem() == "16") { selectedBig = 16; renewFont(); }else if (e.getItem() == "24") { selectedBig = 24; renewFont(); }else if (e.getItem() == "32") { selectedBig = 32; renewFont(); }else if (e.getItem() == "64") { selectedBig = 64; renewFont(); }else if (e.getItem() == "72") { selectedBig = 72; renewFont(); }else if (e.getItem() == "紅色") { selectedColor = Color.red; renewFont(); }else if (e.getItem() == "黑色") { selectedColor = Color.black; renewFont(); }else if (e.getItem() == "藍色") { selectedColor = Color.blue; renewFont(); }else if (e.getItem() == "黃色") { selectedColor = Color.yellow; renewFont(); }else if (e.getItem() == "綠色") { selectedColor = Color.green; renewFont(); }else if (e.getItem() == "白色") { selectedColor = Color.WHITE; renewFont(); } } /** * 兩個btn的監(jiān)聽事件回調(diào) * @param arg0 */ @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btn_cancel) { this.dispose();//銷毀當前窗口 }else if (e.getSource() == btn_ok) { // 調(diào)用父窗體的實例,拿到textarea并對其setFont //fileManagement.getEdit_text_area().setFont(selectedFont); // 這里的Edit_text_area設置為靜態(tài)變量static 函數(shù)也一樣 這樣才能調(diào)用 //fileManagement.getEdit_text_area().setForeground(selectedColor); // 設置顏色 // 在父窗口內(nèi)必須將Edit_text_area設置為static變量(靜態(tài)變量) // 靜態(tài)變量的特點是,已經(jīng)對象一經(jīng)實例化(test1被new出來) 其中的靜態(tài)變量也會在內(nèi)存中存在,一直持續(xù)到實例被銷毀 // 這時我們我們可以對其進行訪問 /*test1 t1 = new test1(); t1.getEdit_text_area().setFont(selectedFont);*/ /** * 以上這個方法是不行的,因為會通過實例化test1 窗口來設置一個新的Font的窗口,與我們想要的效果不相符 * 我們想要的是在原父窗口內(nèi)將其字體改變格式 */ this.dispose(); } } public void renewFont() { selectedFont = new Font(selectedStyle,selectedPattern,selectedBig); showText.setFont(selectedFont); showText.setForeground(selectedColor); } /** * 對ComboBox添加監(jiān)聽器 */ private void initListener() { choose_word_style.addItemListener(this); choose_word_big.addItemListener(this); choose_word_pattern.addItemListener(this); choose_word_color.addItemListener(this); } /** * 給兩個btn添加監(jiān)聽器 */ public void addBtnListener() { btn_ok.addActionListener(this); btn_cancel.addActionListener(this); } /** * 初始化ok 和cancel 兩個按鈕 */ private void initButton() { btn_ok = new JButton("OK"); btn_cancel = new JButton("CANCEL"); } /** * 初始化布局 * 將每個控件按照一定得布局排在this窗口中 */ public void initLocation() { ... } /** * 初始化展示字體區(qū)域 */ public void initText() { ... } /** * 初始化幾個comboBox * 把相應的選項加入 */ public void initBox() { ... } public static void main(String[] args) { about_Format a = new about_Format(); } }
大家可能被嚇到了,一下子跳出來那么多代碼。莫慌,這里一個個分析還是很簡單的。剛才也說過了,我們現(xiàn)在需要對button和item添加監(jiān)聽器,然后在回調(diào)函數(shù)里處理就好了,至于處理的內(nèi)容。無非就是兩個button點擊然后關閉窗口。item選擇對showText改變Font,至于怎么變,前面我們聲明的selected屬性就很有用了,可以通過他們減少很多代碼量。這里不需要多說,大家看一眼就明白了。
OK,到這里,一個完整的可以選擇文本格式的小窗口已經(jīng)獨立的出來了。那我們接下來就是把除了main之外的其他內(nèi)容拷到原來的工程里,當然要新建一個.java。完成之后,我們就可以考慮btn_ok點擊的時候主窗體文本改變的問題。
不多說,首先在父窗體的actionPerformed里處理事件,把about_Format窗口彈出來再說。
在主窗體.java內(nèi),將JTextArea 設置為static,然后給一個getter 方法:
private static JTextArea edit_text_area; //private JTextArea edit_text_area; // 原來 public static JTextArea getEdit_text_area() { //public JTextArea getEdit_text_area() { return edit_text_area; }
在about_Format.java里給btn_ok添加事件:
else if (e.getSource() == btn_ok) { // 調(diào)用父窗體的實例,拿到textarea并對其setFont test5.getEdit_text_area().setFont(selectedFont); // 這里的Edit_text_area設置為靜態(tài)變量static 函數(shù)也一樣 這樣才能調(diào)用 //fileManagement.getEdit_text_area().setForeground(selectedColor); // 設置顏色 // 在父窗口內(nèi)必須將Edit_text_area設置為static變量(靜態(tài)變量) // 靜態(tài)變量的特點是,已經(jīng)對象一經(jīng)實例化(test1被new出來) 其中的靜態(tài)變量也會在內(nèi)存中存在,一直持續(xù)到實例被銷毀 // 這時我們我們可以對其進行訪問 /*test1 t1 = new test1(); t1.getEdit_text_area().setFont(selectedFont);*/ /** * 以上這個方法是不行的,因為會通過實例化test1 窗口來設置一個新的Font的窗口,與我們想要的效果不相符 * 我們想要的是在原父窗口內(nèi)將其字體改變格式 */ this.dispose(); }
這里給大家留一個小bug,給大家自己去發(fā)現(xiàn),看看是否能實現(xiàn)。
到此為止,第二個窗口就畫完了。
總結(jié)一下本章完成的工作:一、在主窗口點擊item,彈出窗口。二、給窗口畫出三層結(jié)構(gòu),第一層幾個單選列,第二層一個顯示文本,第三層兩個按鈕。 三、給JComboBox添加監(jiān)聽事件,并對事件做處理,給兩個按鈕添加事件,對事件做處理。
到此這篇關于用JAVA寫文本編輯器的文章就介紹到這了,更多相關JAVA寫文本編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot 項目讀取Resources目錄下的文件(推薦)
這篇文章主要介紹了Springboot 項目讀取Resources目錄下的文件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11SpringBoot利用@Retryable注解實現(xiàn)接口重試
本文主要介紹了springboot如何利用@Retryable注解實現(xiàn)接口重試功能,文中示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06java實現(xiàn)酷狗音樂臨時緩存文件轉(zhuǎn)換為MP3文件的方法
這篇文章主要介紹了java實現(xiàn)酷狗音樂臨時緩存文件轉(zhuǎn)換為MP3文件的方法,涉及java針對文件操作的相關技巧,需要的朋友可以參考下2016-08-08