JavaFx
1、Java窗体JFrame类
- 通过构造JFrame子类创造最简单的窗体
import javax.swing.*;
public class Example extends JFrame {
Example() {
setSize(480, 360);
setVisible(true);
setTitle("win");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Example();
}
}
2、Java的界面布局管理
Java为了实现跨平台机制,引入了布局管理器来管理界面。
void setLayout(LayoutManager layoutObj)
常见的布局管理方式有:
- 流式布局管理器
setLayout(new FlowLayout(FlowLayout.LEFT));
- 边界布局管理器
panel1.setLayout(new BorderLayout());
panel1.add("Center", tfObj1);
- 网格布局管理器
setLayout(GirdLayout(int Rows, int Cols));
3、Java项目实战:字符串的查找与替换
先贴代码,然后进行详细说明
字符串查找算法类Match:
public class Match {
public StringBuffer strReplace(String a, String b, String c) {
StringBuffer res = new StringBuffer(a);
int len = b.length();
int leap = 0;
for(int i = 0; i < a.length(); i ++ ) {
if(a.charAt(i) == b.charAt(0)) {
int x = 0;
while(x < len && x + i < a.length() && a.charAt(i + x) == b.charAt(x)) {
x ++ ;
}
if(x == len) {
res.replace(i + leap, i + x + leap, c);
leap += c.length();
}
}
}
return res;
}
}
创造窗体类Text:
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Text extends JFrame implements ActionListener {
private JTextPane tPane = new JTextPane();
private JTextPane tpObj1 = new JTextPane();
private JTextPane tpObj2 = new JTextPane();
private JButton btnReplace;
private JLabel labObj1, labObj2, labObj3;
private JPanel pObj1, pObj2, pObj3, pObj4, pObj5;
StyledDocument doc = tPane.getStyledDocument();
StyledDocument doc1 = tpObj1.getStyledDocument();
StyledDocument doc2 = tpObj2.getStyledDocument();
SimpleAttributeSet text = new SimpleAttributeSet();
Text() {
setTitle("文本查找与替换");
setSize(720, 480);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
StyleConstants.setFontSize(text, 24);
doc.setCharacterAttributes(0, 12, text, true);
doc1.setCharacterAttributes(0, 12, text, true);
doc2.setCharacterAttributes(0, 12, text, true);
JScrollPane scroller = new JScrollPane(tPane);
btnReplace = new JButton("替换");
labObj1 = new JLabel("文本区域:");
labObj2 = new JLabel("查找:");
labObj3 = new JLabel("替换:");
pObj1 = new JPanel();
pObj2 = new JPanel();
pObj3 = new JPanel();
pObj4 = new JPanel();
pObj5 = new JPanel();
pObj1.setLayout(new BorderLayout());
pObj1.add("North", labObj1);
pObj1.add("Center", scroller);
//查找文本框
pObj2.setLayout(new BorderLayout());
pObj2.add("North", labObj2);
pObj2.add("Center", tpObj1);
//替换文本框
pObj3.setLayout(new BorderLayout());
pObj3.add("North", labObj3);
pObj3.add("Center", tpObj2);
pObj4.setLayout(new FlowLayout());
pObj4.add(btnReplace);
pObj5.setLayout(new GridLayout(3, 1));
pObj5.add(pObj2);
pObj5.add(pObj3);
pObj5.add(pObj4);
setLayout(new GridLayout(1, 2));
add(pObj1);
add(pObj5);
validate();
btnReplace.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton butObj = (JButton)(e.getSource());
Match classObj = new Match();
if(butObj.getLabel() == "替换") {
String str1 = tPane.getText();
String str2 = tpObj1.getText();
String str3 = tpObj2.getText();
StringBuffer res = classObj.strReplace(str1, str2, str3);
tPane.setText(res.toString());
}
}
}
主方法:
public class Example {
public static void main(String[] args) {
new Text();
}
}
以上就是实现字符串查找与替换窗口的几个类;
接下来,我将详细说明每个新的知识点。
4、新知识点说明
JTextPane
JTextPane是我们在GUI界面上看到的输入文本框
//新建一个JTextPane对象
JTextPane tPane = new JTextPane;
//获得该文本框的各项信息,如字体大小等
StyledDocument doc = tPane.getStyledDocument();
//使用SimpleAttributeSet来储存一整个集合的字体设定
SimpleAttributeSet text = new SimpleAttributeSet();
//使用StyleConstants方法往text中加入一些字体设定。
StyleConstants.setFontSize(text, 24);
//将tPane的doc文件更新,更新为之前设定好的text中的内容。
doc.setCharacterAttributes(0, 12, text, true);
JPanel GUI 界面的容器
JPanel pObj1 = new JPanel(); //新建一个JPanel对象
//使用BorderLayout界面管理器
pObj1.setLayout(new BorderLayout());
//将之前定义好的labObj1和scroller加入到pObj1这个容器中
pObj1.add("North", labObj1);
pObj1.add("Center", scroller);
ActionListener 动作捕捉
//按我的理解,这边应该是继承了ActionListener这个接口,然后自己写方法实现。
class Text extends JFrame implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//监听被按按钮的名字
JButton butObj = (JButton)(e.getSource());
Match classObj = new Match();
//如果被按的按钮是替换
if(butObj.getLabel() == "替换") {
//取得文本框里面的文字
String str1 = tPane.getText();
String str2 = tpObj1.getText();
String str3 = tpObj2.getText();
StringBuffer res = classObj.strReplace(str1, str2, str3);
tPane.setText(res.toString());
}
}
}
以上就是我学习这个项目时,遇见的新东西。记录在此,供自己以后复习,以及有需要的人参考。