📄 main.java
字号:
return "Text Files";
}
});
int saveResult = chooser.showSaveDialog(Main.this);
if (saveResult == JFileChooser.APPROVE_OPTION) {
try {
write = new PrintWriter(new FileOutputStream(chooser
.getSelectedFile().getName()), true);
write.println(text.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(Main.this, "文件不能保存",
"保存文件", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(Main.this, "请用户保存需要的文件",
"保存文件", JOptionPane.WARNING_MESSAGE);
}
}
private void saveDialog() {
chooser.setCurrentDirectory(new File("."));
// 文件过滤
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
String fileName = f.getName().toLowerCase();
return fileName.endsWith(".txt") || f.isDirectory();
}
public String getDescription() {
return "Text Files";
}
});
int saveResult = chooser.showSaveDialog(Main.this);
if (saveResult == JFileChooser.APPROVE_OPTION) {
try {
write = new PrintWriter(new FileOutputStream(chooser
.getSelectedFile().getName()), true);
write.println(text.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(Main.this, "文件不能保存",
"保存文件", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(Main.this, "请用户保存需要的文件",
"保存文件", JOptionPane.WARNING_MESSAGE);
}
}
public void openDialog() {
chooser.setCurrentDirectory(new File("."));
// 文件过滤
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
String fileName = f.getName().toLowerCase();
return fileName.endsWith(".txt") || f.isDirectory();
}
public String getDescription() {
return "Text Files";
}
});
int openResult = chooser.showOpenDialog(Main.this);
if (openResult == JFileChooser.APPROVE_OPTION) {
text.setText("");
try {
in = new BufferedReader(new FileReader(chooser
.getSelectedFile().getPath()));
String line;
while ((line = in.readLine()) != null) {
text.append(line);
text.append("\n");
text.validate();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(Main.this, "文件不能打开",
"打开文件", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(Main.this, "请用户选择需要打开的文件",
"打开文件", JOptionPane.WARNING_MESSAGE);
}
}
class FindDialog extends JDialog implements ActionListener, WindowListener,
KeyListener {
final JTextArea text1;
JLabel lfind, lreplace;
JTextField tfind, treplace;
JButton bfind, breplace, bnext, bcancel;
JPanel fieldPane, buttonPane;
String strFind, strReplace;
int txtPlace, txtLen, strNext;
FindDialog(final JTextArea text, JFrame findDialog) {
super(findDialog, "查找", true);
this.text1 = text;
Container findPane = getContentPane();
lfind = new JLabel("查找内容");
lreplace = new JLabel("替换");
tfind = new JTextField();
treplace = new JTextField();
bfind = new JButton("查找");
breplace = new JButton("替换");
bcancel = new JButton("取消");
bnext = new JButton("下一个");
fieldPane = new JPanel();
buttonPane = new JPanel();
fieldPane.setLayout(new GridLayout(2, 2));
findPane.add(fieldPane, BorderLayout.CENTER);
findPane.add(buttonPane, BorderLayout.SOUTH);
fieldPane.add(lfind);
fieldPane.add(tfind);
fieldPane.add(lreplace);
fieldPane.add(treplace);
buttonPane.add(bfind);
buttonPane.add(breplace);
buttonPane.add(bnext);
buttonPane.add(bcancel);
bfind.addActionListener(this);
breplace.addActionListener(this);
bcancel.addActionListener(this);
this.addWindowListener(this);
tfind.addKeyListener(this);
treplace.addKeyListener(this);
bnext.addActionListener(this);
setSize(370, 120);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bfind) {
find();
}
if (e.getSource() == breplace) {
replace();
}
if (e.getSource() == bcancel) {
setVisible(false);
}
if (e.getSource() == bnext) {
findNext();
}
}
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
private void find() {
strFind = tfind.getText();
strNext = 0; //开始位置
txtPlace = text1.getText().indexOf(strFind, strNext);
txtLen = txtPlace + strFind.length();
text1.requestFocus(true);
text1.select(txtPlace,txtLen);
strNext = text1.getSelectionEnd(); //选中内容的最后位置
if (txtPlace == -1) /*没有找到的处理*/ {
JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找",
JOptionPane.INFORMATION_MESSAGE);
}
bnext.setEnabled(true);
}
private void findNext() {
bfind.setEnabled(false);
txtPlace = text1.getText().indexOf(strFind, strNext);
txtLen = txtPlace + strFind.length();
text1.requestFocus(true);
text1.select(txtPlace, txtLen);
strNext = text1.getSelectionEnd(); //获取查找下一个内容的最后位置
if (txtPlace == -1) /*没有找到的处理*/ {
JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找下一个",
JOptionPane.INFORMATION_MESSAGE);
strNext = 0; //没有找到初始化位置,以变重新查找
tfind.setText("");
bnext.setEnabled(false);
breplace.setEnabled(false);
}
}
private void replace() {
strReplace = treplace.getText();
text1.replaceRange(strReplace, txtPlace, txtPlace + strFind.length());
}
}
class FontDialog extends JDialog implements ActionListener {
Font font;
JButton yes= new JButton("确定");
JButton no = new JButton("退出");
JLabel fontLabel;
JLabel colorLabel1;
JLabel colorLabel10;
JSpinner spinner;
JComboBox colorComboBox1,colorComboBox10,fontComboBox;
JTextArea text;
FontDialog(JFrame frame,JTextArea text){
super(frame,true);
this.text = text;
this.setLayout(new GridLayout(1,4));
yes.addActionListener(this);
no.addActionListener(this);
Container container = this.getContentPane();
fontLabel = new JLabel("选择字体");
String[] a = new String[]{"font1","font2","font3","font4"};
fontComboBox = new JComboBox(a);
container.add(fontLabel);
container.add(fontComboBox);
colorLabel1 = new JLabel("选择背景颜色");
colorLabel10 = new JLabel("字体颜色");
String[] b10 = new String[]{"RED","MAGENTA","GRAY","PINK","WHITE"};
String[] b1 = new String[]{"BLACK","CYAN","YELLOW","BLUE","PINK"};
colorComboBox1 = new JComboBox(b1);
colorComboBox10 = new JComboBox(b10);
container.add(colorLabel1);
container.add(colorComboBox10);
container.add(colorLabel10);
container.add(colorComboBox1);
container.add(yes);
container.add(no);
this.pack();
this.setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == yes){
Color color;
int result1 = colorComboBox1.getSelectedIndex();
int result10 = colorComboBox10.getSelectedIndex();
switch(result10){
case 0: color = Color.WHITE;break;
case 1: color = Color.MAGENTA;break;
case 2: color = Color.GRAY;break;
case 3: color = Color.PINK;break;
default: color = Color.RED;
}
text.setBackground(color);
switch(result1){
case 0: color = Color.BLACK;break;
case 1: color = Color.CYAN;break;
case 2: color = Color.YELLOW;break;
case 3: color = Color.BLUE;break;
default: color = Color.PINK;
}
text.setForeground(color);
int result2 = fontComboBox.getSelectedIndex();
switch (result2){
case 0: font = new Font(null,Font.PLAIN,15);break;
case 1: font = new Font(null,Font.BOLD,15);break;
case 2: font = new Font(null,Font.ITALIC,15);break;
case 3: font = new Font(null,Font.CENTER_BASELINE,15);break;
case 4: font = new Font(null,Font.HANGING_BASELINE,20);
}
text.setFont(font);
}
if(e.getSource() == no){
this.dispose();
}
}
}
class showPanel extends JPanel{
Image image;
showPanel(Image image){
this.image = image;
}
public void paintComponent(Graphics g){
super.paintComponents(g);
if(image!=null){
g.drawImage(image,0,0,300,300,this);
}
else {
System.out.println("Wrong!");
}
}
}
class EJButton extends JButton{
EJButton(String str){
Icon icon = new ImageIcon("image" + File.separatorChar + "1.jpg");
this.setIcon(icon);
this.setText(str);
this.setHorizontalTextPosition(SwingConstants.CENTER);
this.setToolTipText(str);
this.setSize(20,20);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -