「属性编辑器java」性属性编辑器
今天给各位分享属性编辑器java的知识,其中也会对性属性编辑器进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、thymeleaf 能执行java代码吗
- 2、请问怎么用java编程完成组件属性编辑器功能的任务?比如选择字体大小的功能实现
- 3、java源代码编辑器 设计用于编写Java源代码的编辑器,基本要求:可以完成源程序的文件打开,编辑和文件保存
- 4、java spring mvc 定义接口属性为什么是null
- 5、用JAVA制作一个简单的皮肤属性编辑器。
thymeleaf 能执行java代码吗
简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:
1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
标准表达式语法
它们分为四类:
1.变量表达式
2.选择或星号表达式
3.文字国际化表达式
4.URL表达式
变量表达式
变量表达式即OGNL表达式或Spring EL表达式(在Spring术语中也叫model attributes)。如下所示:${session.user.name}
它们将以HTML标签的一个属性来表示:
span th:text="${book.author.name}" li th:each="book : ${books}"
选择(星号)表达式
选择表达式很像变量表达式,不过它们用一个预先选择的对象来代替上下文变量容器(map)来执行,如下:*{customer.name}
被指定的object由th:object属性定义:
div th:object="${book}"
...
span th:text="*{title}".../span
...
/div
文字国际化表达式
文字国际化表达式允许我们从一个外部文件获取区域文字信息(.properties),用Key索引Value,还可以提供一组参数(可选).
#{main.title}
#{message.entrycreated(${entryId})}
可以在模板文件中找到这样的表达式代码:
table
...
th th:text="#{header.address.city}".../th
th th:text="#{header.address.country}".../th
...
/table
URL表达式
URL表达式指的是把一个有用的上下文或回话信息添加到URL,这个过程经常被叫做URL重写。@{/order/list}URL还可以设置参数:@{/order/details(id=${orderId})}相对路径:@{../documents/report}
让我们看这些表达式:
form th:action="@{/createOrder}"
a href="main.html" th:href="@{/main}"
变量表达式和星号表达有什么区别吗?
如果不考虑上下文的情况下,两者没有区别;星号语法评估在选定对象上表达,而不是整个上下文什么是选定对象?就是父标签的值,如下:
div th:object="${session.user}"
pName: span th:text="*{firstName}"Sebastian/span./p
pSurname: span th:text="*{lastName}"Pepper/span./p
pNationality: span th:text="*{nationality}"Saturn/span./p
/div
这是完全等价于:
div th:object="${session.user}"
pName: span th:text="${session.user.firstName}"Sebastian/span./p
pSurname: span th:text="${session.user.lastName}"Pepper/span./p
pNationality: span th:text="${session.user.nationality}"Saturn/span./p
/div
当然,美元符号和星号语法可以混合使用:
div th:object="${session.user}"
pName: span th:text="*{firstName}"Sebastian/span./p
pSurname: span th:text="${session.user.lastName}"Pepper/span./p
pNationality: span th:text="*{nationality}"Saturn/span./p
/div
表达式支持的语法
字面(Literals)
文本文字(Text literals): 'one text', 'Another one!',…
数字文本(Number literals): 0, 34, 3.0, 12.3,…
布尔文本(Boolean literals): true, false
空(Null literal): null
文字标记(Literal tokens): one, sometext, main,…
文本操作(Text operations)
字符串连接(String concatenation): +
文本替换(Literal substitutions): |The name is ${name}|
算术运算(Arithmetic operations)
二元运算符(Binary operators): +, -, *, /, %
减号(单目运算符)Minus sign (unary operator): -
布尔操作(Boolean operations)
二元运算符(Binary operators):and, or
布尔否定(一元运算符)Boolean negation (unary operator):!, not
比较和等价(Comparisons and equality)
比较(Comparators): , , =, = (gt, lt, ge, le)
等值运算符(Equality operators):==, != (eq, ne)
条件运算符(Conditional operators)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
所有这些特征可以被组合并嵌套:
'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
请问怎么用java编程完成组件属性编辑器功能的任务?比如选择字体大小的功能实现
// class name :Edit
// implements :none
// extends :none
// attribute :public
// founction :entrance of the class
//*****************************************************
public class Edit{ //主类
public static void main(String args[]){
EditInstantiation myEdit= new EditInstantiation();
myEdit.init(); //初始化
myEdit.displayEdit(); //显示窗体
} //****************************************************
// class name :EditInstantiation
// implements :none
// extends :WindowAdapter
// attribute :default
// founction :EditInstantiation of the main class
//*****************************************************
class EditInstantiation extends WindowAdapter{ //默认属性类
//define sources 定义各部件
Frame myFrm=new Frame("我的编辑器"); //定义窗体
TextArea myText=new TextArea(); //定义窗体
Dialog myDlgFind=new Dialog(myFrm,"查找",true); //定义查找对话框
Dialog myDlgFindont=new Dialog(myFrm,"字体",true); //定义字体对话框
TextField TFfind=new TextField(30);//定义查找对话中的查找输入框
TextField TFreplace=new TextField(30); //定义查找对话中的替换输入框
TextField TFfontSize,TFfontDemo;//定义字体大小文本框和示例框
Choice CHfontName;//定义字体大小选择框
List LTfontSize; //定义字体大小标签
Button BfontOK,BfontCancel; //定义字体大小对话中的确定及取消按钮
Button Bfind=new Button("查找");//定义查找对话中的查找按钮
Button Breplace=new Button("替换");//定义查找对话中的替换及取消按钮
Button Bnext=new Button("下一个");//定义查找对话中的下一个及取消按钮
Button Bcancel=new Button("取消");//定义查找对话中的确定及取消按钮
Label Lf=new Label("查找内容:");
Label Lr=new Label("替换为: ");
Label Ls=new Label("字体大小:");
FileDialog myFDlg;
Clipboard myCB=Toolkit.getDefaultToolkit().getSystemClipboard(); //剪切板对象
Menu Mfile,Medit,Mformation,Mhelp;
MenuBar MBbar;
//define varities //定义各变量
private int VIindex = 1;
private String VSsubFixStr=".java";
private String VSmyFileName = "Document1",VSmyBFileName;
private File VFmyFile=new File(VSmyFileName+VIindex+".java");
private int VIsearchPosition=0;
private Timer VTtime;
private int VImin=1; // parameter in time schedul
private int VItype=JOptionPane.INFORMATION_MESSAGE;
private String VStitle;
private String VSmessage;
// flag of the changeing
pr
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 spring mvc 定义接口属性为什么是null
springmvc属性编辑器:
对于日期类型的属性转换通过注入属性编辑器。
可以在action类中通过@Binder注解注册属性编辑器。
通过自定义属性编辑类方法,定义属性编辑器。
定义方法:
需要实现implements ConverterString, java.util.Date
String就是原始类型
java.util.Date就是目标类型
目标:
将页面输入的开始采购时间cjtime_start(原始类型),向YycgdCustom类中java.util.Datecjtime_start属性(目标类型)赋值,需要将原始类型字符类型转成日期java.util.Date。
注意 :属性编辑器中目标类型和对象中属性类型一致。
接口方法:
public Date convert(String source) {
如果原始类型转换目标类型成功,通过return 向对象的属性赋值。
如果原始类型转换目标类型不成功,通过return 向对象的属性赋值为null.
日期类型转换的属性编辑器:
public class DateConverter implements ConverterString, Date {
public Date convert(String source) {
if(source != null){//如果从浏览器传入字符串不等于开始转换
source = source.trim();//去除前后空格
if(source.equals("")){
source = null;
}
if(source!=null){//去除空格后不为空则开始转换
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
return null;
}
}
用JAVA制作一个简单的皮肤属性编辑器。
网上有免费 的代码,你可以搜下下载就可。不过下载的代码过于复杂不适合初学者学习。
关于属性编辑器java和性属性编辑器的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-30,除非注明,否则均为
原创文章,转载请注明出处。