MyBase代码

我自己用来管理函数们的,本来是想建自己的库,可后来工作后觉得这些东西不是很常用了。

//MyTools.java


//MyBase
package gangZi;

import javax.swing.JFrame;

public class MyTools extends JFrame{
Tools p
= new Tools();

public MyTools(){
super("MyTools");
this.add(p);
this.setSize(600, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
*
@param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
new MyTools();
}
}


//Tools.java


package gangZi;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Tools extends JPanel {

private static final long serialVersionUID = 1L;
private JLabel lb_type = null;
private JLabel lb_name = null;
private JLabel lb_content = null;
private JComboBox cbx = null;
private JTextField tf = null;
private JTextArea ta = null;
private JButton bt_search = null;
private JButton bt_insert = null;
private JScrollPane jScrollPane = null;
/**
* This method initializes cbx
*
*
@return javax.swing.JComboBox
*/
private JComboBox getCbx() {
if (cbx == null) {
String[] items
={"概念","公式","方法"};
cbx
= new JComboBox(items);
cbx.setBounds(
new Rectangle(148, 1, 150, 30));
cbx.setSize(
200, cbx.getHeight());
cbx.setSelectedItem(
"方法");
cbx.setEditable(
false);
}
return cbx;
}

/**
* This method initializes tf
*
*
@return javax.swing.JTextField
*/
private JTextField getTf() {
if (tf == null) {
tf
= new JTextField();
tf.setBounds(
new Rectangle(148, 31, 200, 30));
}
return tf;
}

/**
* This method initializes ta
*
*
@return javax.swing.JTextArea
*/
private JTextArea getTa() {
if (ta == null) {
ta
= new JTextArea();
ta.setBounds(
new Rectangle(62, 92, 419, 250));
ta.setBackground(Color.LIGHT_GRAY);
}
return ta;
}

/**
* This method initializes bt_search
*
*
@return javax.swing.JButton
*/
private JButton getBt_search() {
if (bt_search == null) {
bt_search
= new JButton();
bt_search.setBounds(
new Rectangle(111, 376, 101, 29));
bt_search.setText(
"查找");
bt_search.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
actionPerformed_search(e);
}
});
}
return bt_search;
}

/**
* This method initializes bt_insert
*
*
@return javax.swing.JButton
*/
private JButton getBt_insert() {
if (bt_insert == null) {
bt_insert
= new JButton();
bt_insert.setBounds(
new Rectangle(256, 376, 91, 30));
bt_insert.setText(
"增加");
bt_insert.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
actionPerformed_insert(e);
}
});
}
return bt_insert;
}

/**
*
@param args
*/
// public static void main(String[] args) {
// // TODO 自动生成方法存根
//
// }

/**
* This is the default constructor
*/
public Tools() {
super();
initialize();
}

/**
* This method initializes this
*
*
@return void
*/
private void initialize() {
lb_content
= new JLabel();
lb_content.setText(
"内容:");
lb_content.setBounds(
new Rectangle(44, 61, 80, 30));
lb_name
= new JLabel();
lb_name.setText(
"名称:");
lb_name.setBounds(
new Rectangle(44, 31, 80, 30));
lb_type
= new JLabel();
lb_type.setText(
"类型:");
lb_type.setBounds(
new Rectangle(44, 1, 79, 30));
this.setLayout(null);
this.setBounds(new Rectangle(0, 0, 550, 450));
this.add(lb_type, null);
this.add(lb_name, null);
this.add(lb_content, null);
this.add(getCbx(), null);
this.add(getTf(), null);
this.add(getTa(), null);
ta.setText(
null);
this.add(getBt_search(), null);
this.add(getBt_insert(), null);
this.add(getJScrollPane(), null);
}

public void actionPerformed_search(ActionEvent e){
String fileName
=null;
if(cbx.getSelectedItem().equals("概念"))fileName="concepts.txt";
if(cbx.getSelectedItem().equals("公式"))fileName="formulas.txt";
if(cbx.getSelectedItem().equals("方法"))fileName="functions.txt";
readFromFile(fileName);
}

public void actionPerformed_insert(ActionEvent e){
String fileName
=null;
if(cbx.getSelectedItem().equals("概念"))fileName="concepts.txt";
if(cbx.getSelectedItem().equals("公式"))fileName="formulas.txt";
if(cbx.getSelectedItem().equals("方法"))fileName="functions.txt";
writeToFile(fileName);
}

public void readFromFile(String fileName){
try{
FileReader f
=new FileReader(fileName);
BufferedReader ff
=new BufferedReader(f);
while(ff.ready()){
String str
=ff.readLine();
if(tf.getText().isEmpty()){
while(ff.ready()){
if(str.contains("//"))
ta.append(str
+'\n');
str
=ff.readLine();
}
break;
}
if(str.contains(tf.getText())){
while(ff.ready()&&!str.contains("****")){
ta.append(str
+'\n');
str
=ff.readLine();
}
break;
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}

public void writeToFile(String fileName){
try{
FileWriter f
=new FileWriter(fileName,true);
BufferedWriter ff
=new BufferedWriter(f);
ff.write(
'\n'+"//"+tf.getText());
String str
=ta.getText();
ff.write(
'\n'+str);
ff.write(
'\n'+"//*****************************************************************************");
ff.flush();
ff.close();
}
catch(IOException e){
e.printStackTrace();
}
ta.setText(
null);
tf.setText(
null);
}

public void println(){
System.out.println(
"到这里了");
}

/**
* This method initializes jScrollPane
*
*
@return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane
= new JScrollPane();
jScrollPane.setBounds(
new Rectangle(46, 90, 453, 249));
jScrollPane.setViewportView(ta);
}
return jScrollPane;
}

}
// @jve:decl-index=0:visual-constraint="10,10"
原文地址:https://www.cnblogs.com/frostbelt/p/1767747.html