MyBaseコード

37205 ワード

私は自分で関数たちを管理するために使って、もともと自分のライブラリを建てたいと思っていましたが、仕事をしてからこれらのものはあまりよく使われていないと思いました.
 

  
// 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
+ '
' );
str
= ff.readLine();
}
break ;
}
if (str.contains(tf.getText())){
while (ff.ready() &&! str.contains( " **** " )){
ta.append(str
+ '
' );
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(
'
' + " // " + tf.getText());
String str
= ta.getText();
ff.write(
'
' + str);
ff.write(
'
' + " //***************************************************************************** " );
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"