字符统计工具:
package com.yufu.test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import java.awt.Color;
public class 字符统计工具 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 3233241143552074026L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
字符统计工具 frame = new 字符统计工具();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public 字符统计工具() {
setTitle("字符统计工具");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 452, 291);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(11, 8, 426, 189);
contentPane.add(scrollPane);
textArea = new bgm的JTextArea类(new String("src/img/bgm.png"));
textArea.setForeground(Color.MAGENTA);
textArea.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
doIt(e);
}
});
textArea.setFont(new Font("Dialog", Font.PLAIN, 15));
textArea.setLineWrap(true);
textArea.setColumns(10);
scrollPane.setViewportView(textArea);
textField = new JTextField();
textField.setBounds(124, 225, 61, 19);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(336, 225, 61, 19);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel = new JLabel("选中的字符数:");
lblNewLabel.setBounds(28, 227, 103, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("光标所在的字符位置:");
lblNewLabel_1.setBounds(203, 226, 144, 17);
contentPane.add(lblNewLabel_1);
}
protected void doIt(CaretEvent e) {
// TODO 自动生成的方法存根
int moue=e.getDot();
int moueSat=e.getMark();
textField.setText(Math.abs(moue-moueSat)+"");
textField_1.setText(moue+"");
}
}
///*******重写的方法
package com.yufu.test;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;
public class bgm的JTextArea类 extends JTextArea {
/**
*
*/
private static final long serialVersionUID = 1L;
private Image imae;
public bgm的JTextArea类(String path){
ImageIcon imaecn=new ImageIcon(path); ///***获得图片路径
imae=imaecn.getImage(); ///***获得图片
setOpaque(false);
}
@Override
public void paint(Graphics g) {
g.drawImage(imae, 0, 0, this);
super.paint(g);
}
}
效果图: