Teach you to write text editor of 3 with JAVA

  • 2021-12-12 04:23:56
  • OfStack

Hello, everyone. The next part may be a little messy, but it is not complicated. I hope I can try my best to clear your mind.

Old routine, this is my first two series, students need to know 1: JAVA writing text editor (2) JAVA writing text editor (1)

Below we want to achieve a click to select text format window, here is also to draw a window, similar to (2). To achieve the function is mainly a few ComboBox pop-up list out, selected attributes change the text below. The bottom two buttons, OK will change the text of the main window, and cancel will not change.

Here I suggest that you can re-open a project first, and then after the 1-step test is completed, copy the code back and delete main.

There are several drop-down boxes at the top of the interface just mentioned, which need to be realized with JComboBox, and the content filling needs the corresponding array.


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 = {" Song Style "," Blackbody "," Regular script "," Microsoft Yahei "," Official script "};
	private String[] colors = {" Red "," Blue "," Green "," Black "," White "," Yellow "};
	private String[] word_big = {"2","4","8","16","24","32","64","72"};
	private String[] pattern = {" Routine "," Tilt "," Bold font "};
	
	private JPanel paneNorth;// Used for loading 4 A ComboBox
	
	public about_Format() {
		  initBox();
		  initLocation();
		  
		  this.setSize(550,200);
		  this.setTitle(" Text format ");
		  this.setVisible(true);
		  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	  }
	
	/**
	   *  Initialize the layout 
	   *  Sets each control according to the 1 Set the layout in this In a window 
	   */
	  public void initLocation() {
		  paneNorth = new JPanel();
		  paneNorth.add(new JLabel(" Font :"));
		  paneNorth.add(choose_word_style);
		  paneNorth.add(new JLabel(" Size :"));
		  paneNorth.add(choose_word_big);
		  paneNorth.add(new JLabel(" Glyph :"));
		  paneNorth.add(choose_word_pattern);
		  paneNorth.add(new JLabel(" Color :"));
		  paneNorth.add(choose_word_color);
		  this.add(paneNorth,BorderLayout.NORTH);
		  
		  
		  
	  }
	
	/**
  	 *  Initialize several comboBox 
  	 *  Add the corresponding options 
  	 */
  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();
	}
}

First we declare the variables inside the class, and then initialize them with the initBox () method, which makes the structure neat. Instantiate comboBox in the method, and put the corresponding string array in, so that the list effect will come out.

Next, we need a container panel to fit four JComboBox, so we instantiate one panel in the initLocation () method and fit a lot of JLabel and JComboBox. Then load panel into the parent form and set the property north.

Next is a text display area, which can have many kinds. I chose JTextField here, which needs to be talked about in advance. Here I also defined a pile of font attributes in advance, which exist for the influence of the final parent form attributes. Don't care here:


public class about_Format extends JFrame{
 
	...
	
	private JPanel paneNorth;// Used for loading 4 A ComboBox
	private JPanel paneCenter;
	
	private JTextField showText ;
	//  Use 1 A font To load the selected attributes, and every time you select 1 Second, modify the corresponding attribute 
		// Then integrate 1 A font Make changes in 
		// Right selectedFont  Set default properties 
		private Font selectedFont = new Font(" Blackbody ",Font.PLAIN, 32);
		private String selectedStyle = " Song Style ";
		private int selectedBig = 32;
		private int selectedPattern = Font.PLAIN;
		private Color selectedColor = Color.BLACK;
	
	public about_Format() {
		  initBox();
		  initText(); //  It should be placed here Location Antecedent cause initText There are elements in it Location Visit   Otherwise, a null pointer exception will occur 
		  initLocation();
		  
		  
		  this.setSize(550,200);
		  this.setTitle(" Text format ");
		  this.setVisible(true);
		  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	  }
	
	/**
	   *  Initialize the layout 
	   *  Sets each control according to the 1 Set the layout in this In a window 
	   */
	  public void initLocation() {
		  paneNorth = new JPanel();
		  ...
		  
		  paneCenter = new JPanel();
		  paneCenter.add(showText);
		  this.add(paneCenter, BorderLayout.CENTER);
		  
	  }
	  
	  /**
	  	 *  Initialize the display font area 
	  	 */
	  	public void initText() {
		  showText = new JTextField(" Font display ");
		  showText.setFont(selectedFont);
		  showText.setEditable(false);
		  showText.setSize(100,160);
		  //showText.setForeground(Color.red);
	  }
	
	/**
  	 *  Initialize several comboBox 
  	 *  Add the corresponding options 
  	 */
  public void initBox() {
	  ...
  }
  
  public static void main(String[] args) {
		about_Format a = new about_Format();
	}
}

I believe everyone's thinking is clear here, but it is not so complicated. We only need to disassemble every 1 component and realize it one by one.

Next, we only need to add two buttons, and then we will respond to the events of JComboBox and Button. As usual, we will add button at the bottom of the panel, and then inherit the interface to handle the callback event.

Add two codes for button:


public class about_Format extends JFrame{
 
	...
	
	private JPanel paneNorth;// Used for loading 4 A 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(" Text format ");
		  this.setVisible(true);
		  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	  }
	
	/**
	   *  Initialization ok  And cancel  Two buttons 
	   */
	  private void initButton() {
		  btn_ok = new JButton("OK");
		  btn_cancel = new JButton("CANCEL");
		}
	
	/**
	   *  Initialize the layout 
	   *  Sets each control according to the 1 Set the layout in this In a window 
	   */
	  public void initLocation() {
		  ...
		  this.add(paneCenter, BorderLayout.CENTER);
		  
		  paneSouth = new JPanel();
		  paneSouth.add(btn_ok);
		  paneSouth.add(btn_cancel);
		  this.add(paneSouth, BorderLayout.SOUTH);
	  }
	  
	  /**
	  	 *  Initialize the display font area 
	  	 */
	  	public void initText() {
		  showText = new JTextField(" Font display ");
		  showText.setFont(selectedFont);
		  showText.setEditable(false);
		  showText.setSize(100,160);
		  //showText.setForeground(Color.red);
	  }
	
	/**
  	 *  Initialize several comboBox 
  	 *  Add the corresponding options 
  	 */
  public void initBox() {
	  ...
  }
  
  public static void main(String[] args) {
		about_Format a = new about_Format();
	}
}

The following system 1 adds a listener. This time, we use ItemListener and ActionListener interfaces. We can separate button from item. If you want to monitor the system 1, you can change it yourself.

Code after adding listener:


public class about_Format extends JFrame implements ItemListener,ActionListener{
 
	...
	
	...
	
	private JButton btn_ok;
	private JButton btn_cancel;
	
	private JTextField showText ;
	// 用1个font来装选中的的属性,每选中1次,对对应的属性修改
		//然后集成1个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);
	  }
	
	/**
	   * 时间监听回调函数
	   * 对每个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() == "常规") {
			  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的监听事件回调
	   * @param arg0
	   */
	  @Override
	  public void actionPerformed(ActionEvent e) {
		  if (e.getSource() == btn_cancel) {
			  this.dispose();//销毁当前窗口
		  }else if (e.getSource() == btn_ok) { // 调用父窗体的实例,拿到textarea并对其setFont
			  //fileManagement.getEdit_text_area().setFont(selectedFont); // 这里的Edit_text_area设置为静态变量static 函数也1样  这样才能调用
			  //fileManagement.getEdit_text_area().setForeground(selectedColor); // 设置颜色
			  // 在父窗口内必须将Edit_text_area设置为static变量(静态变量)
			  // 静态变量的特点是,已经对象1经实例化(test1被new出来) 其中的静态变量也会在内存中存在,1直持续到实例被销毁
			  // 这时我们我们可以对其进行访问 
			  
			  /*test1 t1 = new test1();
			  t1.getEdit_text_area().setFont(selectedFont);*/
			  /**
			   * 以上这个方法是不行的,因为会通过实例化test1 窗口来设置1个新的Font的窗口,与我们想要的效果不相符
			   * 我们想要的是在原父窗口内将其字体改变格式
			   */
			  this.dispose();
		  }
	  }
	  
	  public void renewFont() {
		  selectedFont = new Font(selectedStyle,selectedPattern,selectedBig);
		  showText.setFont(selectedFont);
		  showText.setForeground(selectedColor);
	  }
	  
	/**
	   * 对ComboBox添加监听器
	   */
	  private void initListener() {
		  choose_word_style.addItemListener(this);
		  choose_word_big.addItemListener(this);
		  choose_word_pattern.addItemListener(this);
		  choose_word_color.addItemListener(this);
	  }
	
	/**
	   * 给两个btn添加监听器
	   */
	  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");
		}
	
	/**
	   * 初始化布局
	   * 将每个控件按照1定得布局排在this窗口中
	   */
	  public void initLocation() {
		  ...
	  }
	  
	  /**
	  	 * 初始化展示字体区域
	  	 */
	  	public void initText() {
		  ...
	  }
	
	/**
  	 * 初始化几个comboBox 
  	 * 把相应的选项加入
  	 */
  public void initBox() {
	  ...
  }
  
  public static void main(String[] args) {
		about_Format a = new about_Format();
	}
}

Everyone may be scared, and so many codes jump out in one fell swoop. Don't panic, it's very simple to analyze each one here. As I said just now, we need to add listeners to button and item, and then deal with them in the callback function. As for the content of processing. Nothing more than two button clicks and closes the window. The item option changes the Font to the showText, and as to how, the selected attributes we declared earlier are very useful, and you can reduce a lot of code through them. There is no need to say more here. You can understand it after one look.

OK, here, a complete small window for selecting text formats has come out independently. Then we will copy other contents except main into the original project, and of course we will build a new one. java. Once that's done, we can consider how the main form text changes when btn_ok is clicked.

Needless to say, first handle the event in the actionPerformed of the parent form and pop up the about_Format window.

In the main form. java, set JTextArea to static, and then give an getter method:


private static JTextArea edit_text_area;
 //private JTextArea edit_text_area; //  Original 
public static JTextArea getEdit_text_area() {
 //public JTextArea getEdit_text_area() { 
  return edit_text_area;
 }

Add an event to btn_ok in about_Format. java:


else if (e.getSource() == btn_ok) { //  Call the instance of the parent form and get the textarea And to its setFont
     test5.getEdit_text_area().setFont(selectedFont); //  Here's Edit_text_area Set to a static variable static  Function also 1 Sample    So that you can call 
     //fileManagement.getEdit_text_area().setForeground(selectedColor); //  Set color 
     //  Within the parent window, you must set the Edit_text_area Set to static Variable (static variable) 
     //  Static variables are characterized by having objects 1 Instantiated ( test1 Be new Come out)   Static variables will also exist in memory, 1 Until the instance is destroyed 
     //  At this time, we can access it  
     
     /*test1 t1 = new test1();
     t1.getEdit_text_area().setFont(selectedFont);*/
     /**
      *  The above method is not feasible, because it will be implemented by instantiating test1  Window to set the 1 A new one Font The window does not match the effect we want 
      *  What we want is to change the font format in the original parent window 
      */
     this.dispose();
    }

Here, leave a small bug for everyone to discover and see if it can be realized.

So far, the second window is finished.

Summarize the work done in this chapter: 1. Click item in the main window to pop up a window. 2. Draw a three-layer structure for the window, with several radio columns on the first layer, one display text on the second layer and two buttons on the third layer. 3. Add listening events to JComboBox and deal with them. Add events to two buttons and deal with them.


Related articles: