package anjiajia;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Text {
private JFrame frame;
private JToolBar toolBar;
private JTextArea textArea;
private JScrollPane jScrollPane;
public Text(){
//记事本布局
frame =new JFrame("记事本");
frame.setSize(600, 400);
frame.setLocation(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//设置文本域
textArea =new JTextArea();
textArea.setFont(new Font("楷体",Font.PLAIN,20));//设置文本的默认格式
textArea.setForeground(Color.BLACK);
frame.add(textArea,BorderLayout.CENTER);
//设置滚动条
jScrollPane = new JScrollPane(textArea);// 创建一个滚动条,并且加到文本居于上
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);// 设置滚动条出现方式为“当需要时”
frame.add(jScrollPane, BorderLayout.CENTER);
//设置工具栏
toolBar =new JToolBar();
frame.add(toolBar,BorderLayout.NORTH);
//重构抽取方法
addFontBox();//设置字体风格
addSizeBox();//设置字体大小
addStyleBox();//设置字体为粗体或斜体
addColorBox();//设置颜色
addOpenBox();//打开文件
addSaveBox();//保存文件
frame.setVisible(true);
}
//具体实现
private void addSaveBox() {
JButton saveBtn=new JButton("保存");
saveBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser =new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("文本文件","txt","dat"));
int back =chooser.showSaveDialog(Text.this.frame);
if(back==JFileChooser.APPROVE_OPTION) {
String filename=chooser.getSelectedFile().getPath();
TextTool.write(filename,textArea);
}
}
});
toolBar.add(saveBtn);
}
private void addOpenBox() {
JButton openBtn=new JButton("打开");
openBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser =new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("文本文件(*.txt,*.dat)","txt","dat"));
//chooser.setFileFilter(filter);
int back =chooser.showOpenDialog(Text.this.frame);
if(back==JFileChooser.APPROVE_OPTION) {
String filename=chooser.getSelectedFile().getPath();
TextTool.read(filename,textArea);
}
}
});
toolBar.add(openBtn);
}
private void addColorBox() {
JButton colorBtn=new JButton("■");
colorBtn.setForeground(textArea.getForeground());
colorBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color color=JColorChooser.showDialog(frame, "设置字体颜色", colorBtn.getBackground());
colorBtn.setForeground(color);
textArea.setForeground(color);
}
});
toolBar.add(colorBtn);
}
private void addStyleBox() {
JCheckBox boldBox=new JCheckBox("粗体");
JCheckBox italBox=new JCheckBox("斜体");
ActionListener styleAction=new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean bold=boldBox.isSelected();
boolean ital=italBox.isSelected();
Font font =textArea.getFont();
int style =(bold?Font.BOLD:Font.PLAIN ) | (ital?Font.ITALIC:Font.PLAIN);
textArea.setFont(new Font(font.getName(),style,font.getSize()));
}
};
boldBox.addActionListener(styleAction);
italBox.addActionListener(styleAction);
toolBar.add(boldBox);
toolBar.add(italBox);
}
private void addFontBox() {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = e.getAvailableFontFamilyNames();
JComboBox<String> fontNameBox=new JComboBox<String>(fontNames);
Font font=textArea.getFont();
fontNameBox.setSelectedItem(font.getName());//设置默认值
fontNameBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font =textArea.getFont();
String fontName=(String)fontNameBox.getSelectedItem();
textArea.setFont(new Font(fontName,font.getStyle(),font.getSize()));
}
});
toolBar.add(fontNameBox);
}
private void addSizeBox() {
JComboBox<Integer> SizeBox=new JComboBox<Integer>(new Integer[] {8,10,12,14,16,18,20,24,36,48,72});
SizeBox.setSelectedItem(textArea.getFont().getSize());
SizeBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font =textArea.getFont();
Integer fontSize=(Integer)SizeBox.getSelectedItem();
textArea.setFont(new Font(font.getName(),font.getStyle(),fontSize));
}
});
toolBar.add(SizeBox);
}
//调用
public static void main(String[] args) {
new Text();
}
}
评论0