资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java文本处理器源代码 java源程序编译为字节码的命令

java必须用文本编辑器编写源文件对吗?

对的

成都创新互联于2013年创立,先为番禺等服务建站,番禺等地企业,进行企业商务咨询服务。为番禺企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

1、编写源文件

使用一个文本编辑器(如Edit或记事本)编写源文件,不可使用非文本编辑器(如我word编辑器)。

将编写好的源文件保存起来,源文件的扩展名必须是.java。

2、编译源文件

使用Java编译器(javac.exe)编译源文件,得到字节码文件。

3、运行程序

使用Java SE 平台中的Java解释器(java.exe)来解释执行字节码文件。

java语言写一个文本编辑器的源代码 要能测试通过的!用Eclipse能运行通过的!

一. 高亮的内容:

需要高亮的内容有:

1. 关键字, 如 public, int, true 等.

2. 运算符, 如 +, -, *, /等

3. 数字

4. 高亮字符串, 如 "example of string"

5. 高亮单行注释

6. 高亮多行注释

二. 实现高亮的核心方法:

StyledDocument.setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace)

三. 文本编辑器选择.

Java中提供的多行文本编辑器有: JTextComponent, JTextArea, JTextPane, JEditorPane等, 都可以使用. 但是因为语法着色中文本要使用多种风格的样式, 所以这些文本编辑器的document要使用StyledDocument.

JTextArea使用的是PlainDocument, 此document不能进行多种格式的着色.

JTextPane, JEditorPane使用的是StyledDocument, 默认就可以使用.

为了实现语法着色, 可以继承自DefaultStyledDocument, 设置其为这些文本编辑器的documet, 或者也可以直接使用JTextPane, JEditorPane来做. 为了方便, 这里就直接使用JTextPane了.

四. 何时进行着色.

当文本编辑器中有字符被插入或者删除时, 文本的内容就发生了变化, 这时检查, 进行着色.

为了监视到文本的内容发生了变化, 要给document添加一个DocumentListener监听器, 在他的removeUpdate和insertUpdate中进行着色处理.

而changedUpdate方法在文本的属性例如前景色, 背景色, 字体等风格改变时才会被调用.

@Override

public void changedUpdate(DocumentEvent e) {

}

@Override

public void insertUpdate(DocumentEvent e) {

try {

colouring((StyledDocument) e.getDocument(), e.getOffset(), e.getLength());

} catch (BadLocationException e1) {

e1.printStackTrace();

}

}

@Override

public void removeUpdate(DocumentEvent e) {

try {

// 因为删除后光标紧接着影响的单词两边, 所以长度就不需要了

colouring((StyledDocument) e.getDocument(), e.getOffset(), 0);

} catch (BadLocationException e1) {

e1.printStackTrace();

}

}

五. 着色范围:

pos: 指变化前光标的位置.

len: 指变化的字符数.

例如有关键字public, int

单词"publicint", 在"public"和"int"中插入一个空格后变成"public int", 一个单词变成了两个, 这时对"public" 和 "int"进行着色.

着色范围是public中p的位置和int中t的位置加1, 即是pos前面单词开始的下标和pos+len开始单词结束的下标. 所以上例中要着色的范围是"public int".

提供了方法indexOfWordStart来取得pos前单词开始的下标, 方法indexOfWordEnd来取得pos后单词结束的下标.

public int indexOfWordStart(Document doc, int pos) throws BadLocationException {

// 从pos开始向前找到第一个非单词字符.

for (; pos 0 isWordCharacter(doc, pos - 1); --pos);

return pos;

}

public int indexOfWordEnd(Document doc, int pos) throws BadLocationException {

// 从pos开始向前找到第一个非单词字符.

for (; isWordCharacter(doc, pos); ++pos);

return pos;

}

一个字符是单词的有效字符: 是字母, 数字, 下划线.

public boolean isWordCharacter(Document doc, int pos) throws BadLocationException {

char ch = getCharAt(doc, pos); // 取得在文档中pos位置处的字符

if (Character.isLetter(ch) || Character.isDigit(ch) || ch == '_')

return false;

}

所以着色的范围是[start, end] :

int start = indexOfWordStart(doc, pos);

int end = indexOfWordEnd(doc, pos + len);

六. 关键字着色.

从着色范围的开始下标起进行判断, 如果是以字母开或者下划线开头, 则说明是单词, 那么先取得这个单词, 如果这个单词是关键字, 就进行关键字着色, 如果不是, 就进行普通的着色. 着色完这个单词后, 继续后面的着色处理. 已经着色过的字符, 就不再进行着色了.

public void colouring(StyledDocument doc, int pos, int len) throws BadLocationException {

// 取得插入或者删除后影响到的单词.

// 例如"public"在b后插入一个空格, 就变成了:"pub lic", 这时就有两个单词要处理:"pub"和"lic"

// 这时要取得的范围是pub中p前面的位置和lic中c后面的位置

int start = indexOfWordStart(doc, pos);

int end = indexOfWordEnd(doc, pos + len);

char ch;

while (start end) {

ch = getCharAt(doc, start);

if (Character.isLetter(ch) || ch == '_') {

// 如果是以字母或者下划线开头, 说明是单词

// pos为处理后的最后一个下标

start = colouringWord(doc, start);

} else {

//SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, normalStyle));

++start;

}

}

}

public int colouringWord(StyledDocument doc, int pos) throws BadLocationException {

int wordEnd = indexOfWordEnd(doc, pos);

String word = doc.getText(pos, wordEnd - pos); // 要进行着色的单词

if (keywords.contains(word)) {

// 如果是关键字, 就进行关键字的着色, 否则使用普通的着色.

// 这里有一点要注意, 在insertUpdate和removeUpdate的方法调用的过程中, 不能修改doc的属性.

// 但我们又要达到能够修改doc的属性, 所以把此任务放到这个方法的外面去执行.

// 实现这一目的, 可以使用新线程, 但放到swing的事件队列里去处理更轻便一点.

SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, keywordStyle));

} else {

SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, normalStyle));

}

return wordEnd;

}

因为在insertUpdate和removeUpdate方法中不能修改document的属性, 所以着色的任务放到这两个方法外面, 所以使用了SwingUtilities.invokeLater来实现.

private class ColouringTask implements Runnable {

private StyledDocument doc;

private Style style;

private int pos;

private int len;

public ColouringTask(StyledDocument doc, int pos, int len, Style style) {

this.doc = doc;

this.pos = pos;

this.len = len;

this.style = style;

}

public void run() {

try {

// 这里就是对字符进行着色

doc.setCharacterAttributes(pos, len, style, true);

} catch (Exception e) {}

}

}

七: 源码

关键字着色的完成代码如下, 可以直接编译运行. 对于数字, 运算符, 字符串等的着色处理在以后的教程中会继续进行详解.

import java.awt.Color;

import java.util.HashSet;

import java.util.Set;

import javax.swing.JFrame;

import javax.swing.JTextPane;

import javax.swing.SwingUtilities;

import javax.swing.event.DocumentEvent;

import javax.swing.event.DocumentListener;

import javax.swing.text.BadLocationException;

import javax.swing.text.Document;

import javax.swing.text.Style;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyledDocument;

public class HighlightKeywordsDemo {

public static void main(String[] args) {

JFrame frame = new JFrame();

JTextPane editor = new JTextPane();

editor.getDocument().addDocumentListener(new SyntaxHighlighter(editor));

frame.getContentPane().add(editor);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 500);

frame.setVisible(true);

}

}

/**

* 当文本输入区的有字符插入或者删除时, 进行高亮.

*

* 要进行语法高亮, 文本输入组件的document要是styled document才行. 所以不要用JTextArea. 可以使用JTextPane.

*

* @author Biao

*

*/

class SyntaxHighlighter implements DocumentListener {

private SetString keywords;

private Style keywordStyle;

private Style normalStyle;

public SyntaxHighlighter(JTextPane editor) {

// 准备着色使用的样式

keywordStyle = ((StyledDocument) editor.getDocument()).addStyle("Keyword_Style", null);

normalStyle = ((StyledDocument) editor.getDocument()).addStyle("Keyword_Style", null);

StyleConstants.setForeground(keywordStyle, Color.RED);

StyleConstants.setForeground(normalStyle, Color.BLACK);

// 准备关键字

keywords = new HashSetString();

keywords.add("public");

keywords.add("protected");

keywords.add("private");

keywords.add("_int9");

keywords.add("float");

keywords.add("double");

}

public void colouring(StyledDocument doc, int pos, int len) throws BadLocationException {

// 取得插入或者删除后影响到的单词.

// 例如"public"在b后插入一个空格, 就变成了:"pub lic", 这时就有两个单词要处理:"pub"和"lic"

// 这时要取得的范围是pub中p前面的位置和lic中c后面的位置

int start = indexOfWordStart(doc, pos);

int end = indexOfWordEnd(doc, pos + len);

char ch;

while (start end) {

ch = getCharAt(doc, start);

if (Character.isLetter(ch) || ch == '_') {

// 如果是以字母或者下划线开头, 说明是单词

// pos为处理后的最后一个下标

start = colouringWord(doc, start);

} else {

SwingUtilities.invokeLater(new ColouringTask(doc, start, 1, normalStyle));

++start;

}

}

}

/**

* 对单词进行着色, 并返回单词结束的下标.

*

* @param doc

* @param pos

* @return

* @throws BadLocationException

*/

public int colouringWord(StyledDocument doc, int pos) throws BadLocationException {

int wordEnd = indexOfWordEnd(doc, pos);

String word = doc.getText(pos, wordEnd - pos);

if (keywords.contains(word)) {

// 如果是关键字, 就进行关键字的着色, 否则使用普通的着色.

// 这里有一点要注意, 在insertUpdate和removeUpdate的方法调用的过程中, 不能修改doc的属性.

// 但我们又要达到能够修改doc的属性, 所以把此任务放到这个方法的外面去执行.

// 实现这一目的, 可以使用新线程, 但放到swing的事件队列里去处理更轻便一点.

SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, keywordStyle));

} else {

SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, normalStyle));

}

return wordEnd;

}

/**

* 取得在文档中下标在pos处的字符.

*

* 如果pos为doc.getLength(), 返回的是一个文档的结束符, 不会抛出异常. 如果pos0, 则会抛出异常.

* 所以pos的有效值是[0, doc.getLength()]

*

* @param doc

* @param pos

* @return

* @throws BadLocationException

*/

public char getCharAt(Document doc, int pos) throws BadLocationException {

return doc.getText(pos, 1).charAt(0);

}

/**

* 取得下标为pos时, 它所在的单词开始的下标. ±wor^d± (^表示pos, ±表示开始或结束的下标)

*

* @param doc

* @param pos

* @return

* @throws BadLocationException

*/

public int indexOfWordStart(Document doc, int pos) throws BadLocationException {

// 从pos开始向前找到第一个非单词字符.

for (; pos 0 isWordCharacter(doc, pos - 1); --pos);

return pos;

}

/**

* 取得下标为pos时, 它所在的单词结束的下标. ±wor^d± (^表示pos, ±表示开始或结束的下标)

*

* @param doc

* @param pos

* @return

* @throws BadLocationException

*/

public int indexOfWordEnd(Document doc, int pos) throws BadLocationException {

// 从pos开始向前找到第一个非单词字符.

for (; isWordCharacter(doc, pos); ++pos);

return pos;

}

/**

* 如果一个字符是字母, 数字, 下划线, 则返回true.

*

* @param doc

* @param pos

* @return

* @throws BadLocationException

*/

public boolean isWordCharacter(Document doc, int pos) throws BadLocationException {

char ch = getCharAt(doc, pos);

if (Character.isLetter(ch) || Character.isDigit(ch) || ch == '_')

return false;

}

@Override

public void changedUpdate(DocumentEvent e) {

}

@Override

public void insertUpdate(DocumentEvent e) {

try {

colouring((StyledDocument) e.getDocument(), e.getOffset(), e.getLength());

} catch (BadLocationException e1) {

e1.printStackTrace();

}

}

@Override

public void removeUpdate(DocumentEvent e) {

try {

// 因为删除后光标紧接着影响的单词两边, 所以长度就不需要了

colouring((StyledDocument) e.getDocument(), e.getOffset(), 0);

} catch (BadLocationException e1) {

e1.printStackTrace();

}

}

/**

* 完成着色任务

*

* @author Biao

*

*/

private class ColouringTask implements Runnable {

private StyledDocument doc;

private Style style;

private int pos;

private int len;

public ColouringTask(StyledDocument doc, int pos, int len, Style style) {

this.doc = doc;

this.pos = pos;

this.len = len;

this.style = style;

}

public void run() {

try {

// 这里就是对字符进行着色

doc.setCharacterAttributes(pos, len, style, true);

} catch (Exception e) {}

}

}

}

java语言写一个文本编辑器的源代码

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

import java.util.*; //Date needed

import java.io.PrintWriter;

public class NotePad extends JFrame

{

JTextArea jta;

class newl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jta.setText("");

}

}

class openl implements ActionListener

{ public void actionPerformed(ActionEvent e)

{

JFileChooser jf=new JFileChooser();

jf.showOpenDialog(NotePad.this);

}

}

//保存文件的监听

class savel implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

JFileChooser jf = new JFileChooser();

jf.showSaveDialog(NotePad.this);

}

}

//打印的监听 ?

class printl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

// PrintWriter p = new PrintWriter(NotePad.this);

}

}

//退出记事本的监听

class exitl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);//退出

}

}

//拷贝的监听

class copyl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jta.copy();

}

}

//粘贴的监听

class pastel implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jta.paste();

}

}

//剪切的监听

class cutl implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

jta.cut();

}

}

//查找的监听

//添加日期的监听

class datel implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

Date d=new Date();

jta.append(d.toString());

}

}

//构造函数

public NotePad()

{

jta=new JTextArea("",24,40);

JScrollPane jsp=new JScrollPane(jta);

JMenuBar jmb=new JMenuBar();

JMenu mFile=new JMenu("File");

JMenu mEdit=new JMenu("Edit");

JMenuItem mNew=new JMenuItem("New",KeyEvent.VK_N);

mNew.addActionListener(new newl());

mFile.add(mNew);

JMenuItem mOpen=new JMenuItem("Open",KeyEvent.VK_O);

mOpen.addActionListener(new openl());

mFile.add(mOpen);

JMenuItem mSave=new JMenuItem("Save");

mSave.addActionListener(new savel());

mFile.add(mSave);

mFile.addSeparator(); //添加分割线

JMenuItem mPrint = new JMenuItem("Print");

mPrint.addActionListener(new printl());

mFile.add(mPrint);

mFile.addSeparator(); //添加分割线

JMenuItem mExit=new JMenuItem("Exit");

mExit.addActionListener(new exitl());

mFile.add(mExit);

mFile.setMnemonic(KeyEvent.VK_F);

//编辑菜单的子菜单的处理

JMenuItem jmi;

jmi=new JMenuItem("Copy");

jmi.addActionListener(new copyl());

mEdit.add(jmi);

jmi=new JMenuItem("Cut");

jmi.addActionListener(new cutl());

mEdit.add(jmi);

jmi=new JMenuItem("Paste");

jmi.addActionListener(new pastel());

mEdit.add(jmi);

mEdit.addSeparator(); //添加分割线

jmi=new JMenuItem("Find");

mEdit.add(jmi);

jmi=new JMenuItem("FindNext");

mEdit.add(jmi);

mEdit.addSeparator();

jmi=new JMenuItem("Select All");

mEdit.add(jmi);

jmi=new JMenuItem("Date/Time");

jmi.addActionListener(new datel());

mEdit.add(jmi);

jmb.add(mFile);

jmb.add(mEdit);

this.setJMenuBar(jmb);

this.getContentPane().add(jsp);

this.setSize(200,200);

this.setVisible(true);

}

//主函数,程序入口点

public static void main(String s[])

{

new NotePad();

}

}

求java编写的简单协同文本编辑器源码~~~急!

package jettang;

import java.lang.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

//////////////////////////////////////////////

//主类

public class STU {

public STU() {

try {

jbInit();

} catch (Exception ex) {

ex.printStackTrace();

}

}

public static void main(String args[]) {

new Frm_Main();

}

private void jbInit() throws Exception {

}

}

//主框架,数据库

class Frm_Main implements ActionListener { //throws IOException

public static final int EXIT_ON_CLOSE = 0;

public JFrame frame;

public Container c;

public JMenuBar menuBar;

public JMenu mainMenu1;

public JMenu mainMenu2;

public JMenu mainMenu3;

public JMenuItem subMenu1[] = new JMenuItem[5];

public JMenuItem subMenu2[] = new JMenuItem[7];

public JMenuItem subMenu3[] = new JMenuItem[2];

JButton toolBarButton[] = new JButton[8];

public JToolBar toolBar;

//////////////////////////////////////////////

String strTip[] = {"姓名查询...", "添加信息...",

"删除...", "修改...", "修改确认...",

"添加确认...", "删除确认...", "关于产品信息:)"};

//////////////////////////////////////////////

// String id = new String();

String name = new String();

String sex = new String();

String age = new String();

String classid = new String();

String department = new String();

String call = new String();

//////////////////////////////////////////////

Connection con;

ResultSet rs;

Statement st;

// ResultSetMetaData rsmd;

// Vector columnHeads=new Vector();

// Vector rows = new Vector();

// Vector currentRow=new Vector();

///////////////////////////////////////////////

public JLabel classidL = new JLabel("班级");

public JLabel nameL = new JLabel("姓名");

public JLabel sexL = new JLabel("性别");

public JLabel ageL = new JLabel("年龄");

public JLabel departmentL = new JLabel("单位");

public JLabel callL = new JLabel("电话");

public JLabel tittleLabel = new JLabel();

//////////////////////////////////////////////

public JTextField classidT = new JTextField();

public JTextField nameT = new JTextField();

public JTextField sexT = new JTextField();

public JTextField ageT = new JTextField();

public JTextField departmentT = new JTextField();

public JTextField callT = new JTextField();

public JTextArea textT = new JTextArea();

//////////////////////////////////////////////

//////////////////////////////////////////////

public Frm_Main() {

/////////////////////////////////////////////////

frame = new JFrame("学籍管理系统V1.0");

c = frame.getContentPane();

c.setLayout(null);

menuBar = new JMenuBar();

toolBar = new JToolBar();

toolBar.setFloatable(false);

frame.setJMenuBar(menuBar);

frame.setResizable(false);

////////////////////////////////////////////////

mainMenu1 = new JMenu("管理");

String str1[] = {"添加", "删除", "查询", " ", "关闭"};

for (int i = 0; i 5; i++) {

if (i == 3) {

mainMenu1.addSeparator();

} else {

subMenu1[i] = new JMenuItem(str1[i]);

subMenu1[i].addActionListener(this);

mainMenu1.add(subMenu1[i]);

}

}

menuBar.add(mainMenu1);

/////////////////////////////////////////////////

mainMenu2 = new JMenu("*作");

String str2[] = {"查询", "添加", "删除", "修改",

"提交修改", "提交添加", "提交删除"};

for (int i = 0; i 7; i++) {

subMenu2[i] = new JMenuItem(str2[i]);

subMenu2[i].addActionListener(this);

mainMenu2.add(subMenu2[i]);

}

menuBar.add(mainMenu2);

/////////////////////////////////////////////////

mainMenu3 = new JMenu("集体查询");

String str3[] = {"按班级...", "按单位..."};

for (int i = 0; i 2; i++) {

subMenu3[i] = new JMenuItem(str3[i]);

subMenu3[i].addActionListener(this);

mainMenu3.add(subMenu3[i]);

}

menuBar.add(mainMenu3);

////////////////////////////////////////////////

String strToolBar[] = {"查询", "添加", "删除", "修改", "提交修改",

"提交添加", "提交删除", "关于"};

for (int i = 0; i 8; i++) {

toolBarButton[i] = new JButton(strToolBar[i]);

toolBarButton[i].setToolTipText(strTip[i]);

toolBarButton[i].addActionListener(this);

toolBar.add(toolBarButton[i]);

}

toolBar.setLocation(0, 0);

toolBar.setSize(400, 30);

c.add(toolBar);

////////////////////////////////////////////////

classidL.setLocation(35, 40);

classidL.setSize(40, 20);

//classidL.setFont(new Font("",Font.BOLD,12));

c.add(classidL);

classidT.setLocation(90, 40);

classidT.setSize(200, 20);

//classidT.setEnabled(false);

c.add(classidT);

nameL.setLocation(35, 70);

nameL.setSize(40, 20);

c.add(nameL);

nameT.setLocation(90, 70);

nameT.setSize(200, 20);

c.add(nameT);

sexL.setLocation(35, 100);

sexL.setSize(40, 20);

c.add(sexL);

sexT.setLocation(90, 100);

sexT.setSize(200, 20);

c.add(sexT);

ageL.setLocation(35, 130);

ageL.setSize(40, 20);

c.add(ageL);

ageT.setLocation(90, 130);

ageT.setSize(200, 20);

c.add(ageT);

departmentL.setLocation(35, 160);

departmentL.setSize(40, 20);

c.add(departmentL);

departmentT.setLocation(90, 160);

departmentT.setSize(200, 20);

c.add(departmentT);

callL.setLocation(35, 190);

callL.setSize(40, 20);

c.add(callL);

callT.setLocation(90, 190);

callT.setSize(200, 20);

c.add(callT);

/////////////////////////////////////////////////

tittleLabel.setText("----------集体查询结果--------");

tittleLabel.setSize(300, 20);

tittleLabel.setLocation(10, 210);

c.add(tittleLabel);

////////////////////////////////////////////

textT.setSize(400, 300);

textT.setLocation(10, 240);

c.add(textT, 14);

///////////////////////////////////////////////

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 650);

frame.setLocation(200, 200);

frame.setVisible(true);

conDB();

}

//以上是框架设计

//JDBC

public void conDB() {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (ClassNotFoundException e) {

JOptionPane.showMessageDialog(null, "数据库错误");

}

try {

con = DriverManager.getConnection("jdbc:odbc:jettang", "sa", "");

st = con.createStatement();

} catch (SQLException e) {

JOptionPane.showMessageDialog(null, "数据库连接失败");

}

}

//关闭连接

public void closeDB() {

try {

st.close();

con.close();

} catch (SQLException e) {

JOptionPane.showMessageDialog(null, "数据库关闭失败");

}

}

//事件响应

public void actionPerformed(ActionEvent e) {

//////////////////////////////////////////////

if (e.getSource() == toolBarButton[7]) {

JOptionPane.showMessageDialog(null, "当前版本1.0,*作帮助请看使用说明书");

}

/////////////////////////////////////////////查询(按姓名)

if (e.getSource() == subMenu2[0] || e.getSource() == toolBarButton[0] ||

e.getSource() == subMenu1[2]) {

String idid = JOptionPane.showInputDialog("请输入学生姓名");

if (idid.trim() != "") {

String strSQL = "select * from stuinfo where name = + idid +

";

try {

rs = st.executeQuery(strSQL);

int count = 0;

while (rs.next()) {

classid = rs.getString("classid");

name = rs.getString("name");

department = rs.getString("department");

sex = rs.getString("sex");

age = rs.getString("age");

call = rs.getString("call");

++count;

}

if (count == 0) {

JOptionPane.showMessageDialog(null, "无此信息");

} else {

classidT.setText(classid);

nameT.setText(name);

sexT.setText(sex);

ageT.setText(age);

callT.setText(call);

departmentT.setText(department);

}

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败");

}

}

}

/////////////////////////////////////////////关闭

if (e.getSource() == subMenu1[4]) {

frame.dispose();

closeDB();

}

/////////////////////////////////////////////添加

if (e.getSource() == subMenu1[0] || e.getSource() == subMenu2[1] ||

e.getSource() == toolBarButton[1]) {

JOptionPane.showMessageDialog(null, "请输入你添加的信息再点击提交添加键");

classidT.setEnabled(true);

classidT.setText("");

nameT.setText("");

sexT.setText("");

ageT.setText("");

callT.setText("");

departmentT.setText("");

}

/////////////////////////////////////////////提交添加

if (e.getSource() == toolBarButton[5] || e.getSource() == subMenu2[5]) {

if ((classidT.getText().trim()).equals("") ||

(nameT.getText().trim()).equals("") ||

(sexT.getText().trim()).equals("") ||

(ageT.getText().trim()).equals("") ||

(callT.getText().trim()).equals("") ||

(departmentT.getText().trim()).equals("")) {

JOptionPane.showMessageDialog(null, "请先点击添加键");

} else {

classid = classidT.getText();

name = nameT.getText();

sex = sexT.getText();

age = ageT.getText();

call = callT.getText();

department = departmentT.getText();

String strSQL =

"insert into stuinfo(classid,name,sex,age,call,department) values( +

classid + , +

name + , + sex + , + age + , + call + , +

department + )";

try {

st.executeUpdate(strSQL);

} catch (Exception exx) {

JOptionPane.showMessageDialog(null, "添加失败");

return;

}

JOptionPane.showMessageDialog(null, "添加成功");

classidT.setText("");

nameT.setText("");

sexT.setText("");

ageT.setText("");

callT.setText("");

departmentT.setText("");

}

}

////////////////////////////////////////////提交修改

if (e.getSource() == subMenu2[4] || e.getSource() == toolBarButton[4]) {

if ((classidT.getText().trim()).equals("") ||

(nameT.getText().trim()).equals("") ||

(sexT.getText().trim()).equals("")

|| (ageT.getText().trim()).equals("") ||

(callT.getText().trim()).equals("") ||

(departmentT.getText().trim()).equals("")) {

JOptionPane.showMessageDialog(null, "请先点击修改键");

return;

} else {

classid = classidT.getText();

name = nameT.getText();

sex = sexT.getText();

age = ageT.getText();

call = callT.getText();

department = departmentT.getText();

String strSQL = "update stuinfo set classid= + classid +

,sex= + sex + ,age= + age + ,call= +

call + ,department= + department + " +

"where name= + name + ";

try {

st.executeUpdate(strSQL);

} catch (Exception exx) {

JOptionPane.showMessageDialog(null, "修改失败");

return;

}

JOptionPane.showMessageDialog(null, "修改成功");

classidT.setText("");

nameT.setText("");

sexT.setText("");

ageT.setText("");

callT.setText("");

departmentT.setText("");

}

}

///////////////////////////////////////////////修改

if (e.getSource() == subMenu2[3] || e.getSource() == toolBarButton[3]) {

String idid = JOptionPane.showInputDialog("请输入你要修改的学生姓名后点击提交修改键确认");

if (idid.trim() != "") {

String strSQL = "select * from stuinfo where name = + idid +

";

try {

rs = st.executeQuery(strSQL);

int count = 0;

while (rs.next()) {

classid = rs.getString("classid");

name = rs.getString("name");

department = rs.getString("department");

sex = rs.getString("sex");

age = rs.getString("age");

call = rs.getString("call");

++count;

}

if (count == 0) {

JOptionPane.showMessageDialog(null, "无此信息");

} else {

classidT.setText(classid);

nameT.setText(name);

sexT.setText(sex);

ageT.setText(age);

callT.setText(call);

departmentT.setText(department);

classidT.setEnabled(false);

}

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "修改失败");

}

}

}

/////////////////////////////////////////////////删除

if (e.getSource() == subMenu2[2] || e.getSource() == toolBarButton[2] ||

e.getSource() == subMenu1[1]) {

String idDel = JOptionPane.showInputDialog("请输入要删除的学生姓名后点击提交删除键确认");

if (idDel.trim() != "") {

String strSQL = "select * from stuinfo where name = + idDel +

";

try {

rs = st.executeQuery(strSQL);

int count = 0;

while (rs.next()) {

classid = rs.getString("classid");

name = rs.getString("name");

department = rs.getString("department");

sex = rs.getString("sex");

age = rs.getString("age");

call = rs.getString("call");

++count;

}

if (count == 0) {

JOptionPane.showMessageDialog(null, "无此信息");

} else {

classidT.setText(classid);

nameT.setText(name);

sexT.setText(sex);

ageT.setText(age);

callT.setText(call);

departmentT.setText(department);

classidT.setEnabled(false);

}

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "删除失败");

}

}

}

//////////////////////////////////////////////////提交删除

if (e.getSource() == toolBarButton[6] ||

e.getSource() == subMenu2[6]) {

if ((classidT.getText().trim()).equals("") ||

(nameT.getText().trim()).equals("") ||

(sexT.getText().trim()).equals("") ||

(ageT.getText().trim()).equals("") ||

(callT.getText().trim()).equals("") ||

(departmentT.getText().trim()).equals("")) {

JOptionPane.showMessageDialog(null, "请先点击删除键");

return;

} else {

name = nameT.getText();

String strSQL = "delete from stuinfo where name= + name +

";

try {

st.executeUpdate(strSQL);

} catch (Exception exx) {

JOptionPane.showMessageDialog(null, "删除失败");

return;

}

JOptionPane.showMessageDialog(null, "删除成功");

classidT.setText("");

nameT.setText("");

sexT.setText("");

ageT.setText("");

callT.setText("");

departmentT.setText("");

}

}

//////////////////////////////////////////////集体查询(按班级)

if (e.getSource() == subMenu3[0]) {

classidT.setText("");

nameT.setText("");

sexT.setText("");

ageT.setText("");

callT.setText("");

departmentT.setText("");

if ((classidT.getText().trim()).equals("")) {

String idclass = JOptionPane.showInputDialog("请输入班级");

String strSQL = "select * from stuinfo where classid= +

idclass +

";

try {

rs = st.executeQuery(strSQL);

displayres(rs);

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败");

}

} else {

String idclass = classidT.getText();

String strSQL = "select * from stuinfo where classid= +

idclass +

";

try {

rs = st.executeQuery(strSQL);

displayres(rs);

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败");

}

}

}

////////////////////////////按单位

if (e.getSource() == subMenu3[1]) {

classidT.setText("");

nameT.setText("");

sexT.setText("");

ageT.setText("");

callT.setText("");

departmentT.setText("");

if ((departmentT.getText().trim()).equals("")) {

String idclass = JOptionPane.showInputDialog("请输入单位");

String strSQL = "select * from stuinfo where department= +

idclass +

";

try {

rs = st.executeQuery(strSQL);

displayres(rs);

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败");

}

} else {

String idclass = departmentT.getText();

String strSQL = "select * from stuinfo where department= +

idclass +

";

try {

rs = st.executeQuery(strSQL);

displayres(rs);

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败");

}

}

}

}

public void displayres(ResultSet rs1) throws SQLException {

//定位到达第一条记录

boolean moreRecords = rs1.next();

//如果没有记录,则提示一条消息

if (!moreRecords) {

JOptionPane.showMessageDialog(null, "无此记录");

return;

}

try {

//获取数据

getNextRow(rs1);

//刷新Table

c.validate();

}

catch (SQLException sqlex) {

sqlex.printStackTrace();

}

}

public void getNextRow(ResultSet rs2) throws

SQLException {

int count = 1;

while (rs2.next()) {

name = " 姓名:" + rs2.getString(1);

age = " 年龄:" + rs2.getString(2) + " 单位:";

department = rs2.getString(3) + "

";

textT.append(count + "." + name + age + department);

count++;

}

}

}

用java编写一个简单的多文档文本编辑器

JDK自带一个简单的Notepad,你可以研究一下他的源代码。

在%JAVA_HOME%\demo\jfc\Notepad 目录下

JDK自带一个稍复杂的Stylepad,你可以研究一下他的源代码。

在%JAVA_HOME%\demo\jfc\Stylepad目录下


当前题目:java文本处理器源代码 java源程序编译为字节码的命令
本文链接:http://cdkjz.cn/article/hgppjj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220