新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java代码编辑框源码 java对话框代码

高分求两个简单的JAVA设计源代码

上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)

成都创新互联公司是一家专业提供元江县企业网站建设,专注与成都网站建设、成都网站设计H5技术、小程序制作等业务。10年已为元江县众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法

//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少

@SuppressWarnings("unchecked")

class Student implements ComparableStudent{

private String studentNo; //学号

private String studentName; //姓名

private double englishScore; //英语成绩

private double computerScore; //计算机成绩

private double mathScore; //数学成绩

private double totalScore; //总成绩

//空构造函数

public Student() {}

//构造函数

public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {

this.studentNo = studentNo;

this.studentName = studentName;

this.englishScore = englishSocre;

this.computerScore = computerScore;

this.mathScore = mathScore;

}

//计算总成绩

public double sum() {

this.totalScore = englishScore+computerScore+mathScore;

return totalScore;

}

//计算评测成绩

public double testScore() {

return sum()/3;

}

//实现compareTO方法

@Override

public int compareTo(Student student) {

double studentTotal = student.getTotalScore();

return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);

}

//重写toString方法

public String toString(){

return "学号:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英语成绩:"+this.getEnglishScore()+" 数学成绩:"+this.getMathScore()+" 计算机成绩:"+this.getComputerScore()+" 总成绩:"+this.getTotalScore();

}

//重写equals方法

public boolean equals(Object obj) {

if(obj == null){

return false;

}

if(!(obj instanceof Student)){

return false;

}

Student student = (Student)obj;

if(this.studentNo.equals(student.getStudentName())) { //照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同

return true;

} else {

return false;

}

}

/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的

在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩

*/

public String getStudentNo() {

return studentNo;

}

public void setStudentNo(String studentNo) {

this.studentNo = studentNo;

sum();

}

public String getStudentName() {

return studentName;

}

public void setStudentName(String studentName) {

this.studentName = studentName;

sum();

}

public double getEnglishScore() {

return englishScore;

}

public void setEnglishScore(double englishScore) {

this.englishScore = englishScore;

sum();

}

public double getComputerScore() {

return computerScore;

}

public void setComputerScore(double computerScore) {

this.computerScore = computerScore;

sum();

}

public double getMathScore() {

return mathScore;

}

public void setMathScore(double mathScore) {

this.mathScore = mathScore;

sum();

}

public double getTotalScore() {

return totalScore;

}

}

//Student子类学习委员类的实现

class StudentXW extends Student {

//重写父类Student的testScore()方法

@Override

public double testScore() {

return sum()/3+3;

}

public StudentXW() {}

//StudentXW的构造函数

public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {

super(studentNo,studentName,englishSocre,computerScore,mathScore);

}

}

//Student子类班长类的实现

class StudentBZ extends Student {

//重写父类Student的testScore()方法

@Override

public double testScore() {

return sum()/3+5;

}

public StudentBZ() {}

//StudentXW的构造函数

public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {

super(studentNo,studentName,englishSocre,computerScore,mathScore);

}

}

//测试类

public class Test {

public static void main(String[] args) {

//生成若干个student类、StudentXW类、StudentBZ类

Student student1 = new Student("s001","张三",70.5,50,88.5);

Student student2 = new Student("s002","李四",88,65,88.5);

Student student3 = new Student("s003","王五",67,77,90);

StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);

StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);

Student[] students = {student1,student2,student3,student4,student5};

for(int i = 0 ; istudents.length; i++){

double avgScore = students[i].testScore();

System.out.println(students[i].getStudentName()+"学生的评测成绩为:"+ avgScore+"分");

}

}

}

运行结果为:

张三学生的评测成绩为:69.66666666666667分

李四学生的评测成绩为:80.5分

王五学生的评测成绩为:78.0分

李六学生的评测成绩为:98.5分

朱漆学生的评测成绩为:60.03333333333333分

什么是java源代码 怎么查看

你说的java源代码是指编译成的class文件前的java文件。

当我们运行.java文件时,它会被系统编译成.class文件,例如Test.java编译之后就是Test.class,

源文件就是指Test.java文件,

一般部署项目时,有.class文件就可以发布运行了,但是如果想修改这个系统,.class是不能修改的,要有.java文件才能修改

也可以上网去下反编译软件,就是能把.class文件大部分还原成.java文件的工具,但不是100%还原,而且如果不是正版的,小心有毒啊,什么的。

eclipse怎么查看java源代码

在Eclipse中查看JDK类库的源代码

设置:

1.点 “window”- "Preferences" - "Java" - "Installed JRES"

2.此时"Installed JRES"右边是列表窗格,列出了系统中的 JRE 环境,选择你的JRE,然后点边上的 "Edit...", 会出现一个窗口(Edit JRE)

3.选中rt.jar文件的这一项:“c:\program files\java\jre_1.5.0_06\lib\rt.jar” 

点 左边的“+” 号展开它

4.展开后,可以看到“Source Attachment:(none)”,点这一项,点右边的按钮“Source Attachment...”, 选择你的JDK目录下的 “src.zip”文件

5.一路点"ok",结束。

dt.jar是关于运行环境的类库,主要是swing的包 

tools.jar是关于一些工具的类库 

rt.jar包含了jdk的基础类库,也就是你在java doc里面看到的所有的类的class文件

使用:

可以在 Java 源代码编辑器或代码片段编辑测试窗中选择类型、方法或字段的名称,然后对元素的定义打开编辑器。

在 Java 编辑器中,选择类型、方法或字段的名称。您也可以仅仅在名称中单击一次。 

执行下列其中一项操作: 

1.从菜单栏中,选择浏览 打开声明 

2.从编辑器的弹出菜单中,选择打开声明 

3.按 F3 键,如下图

java源代码编辑器 设计用于编写Java源代码的编辑器,基本要求:可以完成源程序的文件打开,编辑和文件保存

一. 高亮的内容:

需要高亮的内容有:

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 true; }

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 true; }

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代码编辑框源码 java对话框代码
URL分享:http://www.wjwzjz.com/article/doopppo.html
在线咨询
服务热线
服务热线:028-86922220
TOP