@FishC 你可以帮我解决代码中的乱码问题吗import javax.swing.*;
import javax.swing.colorchooser.DefaultColorSelectionModel;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Txt extends JFrame implements ActionListener{
private StringBuilder ss=new StringBuilder();
private JMenuBar jm1=new JMenuBar(); //�����˵�������Ӧ�˵���
private JMenu jme1=new JMenu("�ļ�(F)");
private JMenuItem jm3=new JMenuItem("�½�(N) ");
private JMenuItem jm4=new JMenuItem("�´���(W) ");
private JMenuItem jm5=new JMenuItem("��(O)... ");
private JMenuItem jm6=new JMenuItem("����(S) ");
private JMenuItem jm7=new JMenuItem("���Ϊ(A) ");
private JMenuItem jm8=new JMenuItem("�˳�(X)");
private JMenu jme2=new JMenu("��ʽ(O)");
private JMenuItem jm9=new JMenuItem("�Զ�����(W)");
private JMenu jme3=new JMenu("��ɫ(C)");
private JMenuItem jm10=new JMenuItem("������ɫ(C)");
private JMenu jme4=new JMenu("����(H)");
private JMenuItem jm11=new JMenuItem("�鿴����(H)");
private DefaultColorSelectionModel modle=new DefaultColorSelectionModel(); //Ĭ����ɫѡ��ģ��
private JColorChooser jcc=new JColorChooser(modle);
private JFileChooser jfc=new JFileChooser(new File(""));
private JTextArea jt=new JTextArea(10,30);
private JScrollPane jsp=new JScrollPane(jt);
private Font font=new Font("����",0,22);
Txt()
{
super("���±�"); //���ÿ�ܱ���
add(jm1,BorderLayout.NORTH);
add(jsp,BorderLayout.CENTER);
jt.setLayout(null);
jt.setFont(font);
jm1.add(jme1);jm1.add(jme2);jm1.add(jme3);jm1.add(jme4); //����Ӧ�˵�ѡ������Ӧ�˵���
jme1.add(jm3);jme1.add(jm4);jme1.add(jm5);jme1.add(jm6);
jme1.add(jm7);jme1.addSeparator();
jme1.addSeparator();jme1.add(jm8);
jme2.add(jm9);
jme3.add(jm10);
jme4.add(jm11);
jme1.setMnemonic('F'); //mnemonic n/���Ƿ� adj.����� �������Ƿ�����alt+�ַ� ��Ӧ�˵���
jme2.setMnemonic('O');
jme3.setMnemonic('C');
jme4.setMnemonic('H');
jm3.setMnemonic('N');
jm4.setMnemonic('W');
jm5.setMnemonic('O');
jm6.setMnemonic('S');
jm7.setMnemonic('A');
jm8.setMnemonic('X');
jm9.setMnemonic('W');
jm10.setMnemonic('C');
//���ÿ�ݼ�������Ӧ��ݼ�������Ӧ����
jm3.setAccelerator((KeyStroke.getKeyStroke(KeyEvent.VK_W,ActionEvent.CTRL_MASK)));
jm4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK+ActionEvent.SHIFT_MASK));
jm5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
jm6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
jm7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK+ActionEvent.SHIFT_MASK));
jm8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
jm10.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK+ActionEvent.SHIFT_MASK));
jm3.addActionListener(new ActionListener() { //���������ڲ������ʽ��д�¼�����
@Override
public void actionPerformed(ActionEvent e) {
jt.setText("");
setTitle("�ޱ���-���±�");
}
});
jm4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Txt t1=new Txt();
t1.setTitle("�ޱ���-���±�");
}
});
jm8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jm11.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Txt.this,"��ͬ�ɳ�����","����",JOptionPane.PLAIN_MESSAGE);
}
});
jm5.addActionListener(this); //�Բ˵�����Ӽ���
jm7.addActionListener(this);
jm8.addActionListener(this);
jm6.addActionListener(this);
jm9.addActionListener(this);
jm10.addActionListener(this);
setBounds(700,250,700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) { //������
Txt t=new Txt();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("��(O)... ")) //���ļ�
{
int open=jfc.showOpenDialog(this);
if(open!=JFileChooser.APPROVE_OPTION)
{}
else
{
try{
File f=jfc.getSelectedFile();
ss.append(f.getAbsolutePath());
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
StringBuilder info=new StringBuilder();
String str;
while((str=br.readLine())!=null) {info.append(str+"\r\n");}
jt.setText(String.valueOf(info));
br.close();
}catch (IOException ioe){}
}
}
else if(e.getActionCommand().equals("���Ϊ(A) ")) //�洢�ļ�
{
int save=jfc.showSaveDialog(this);
if(save==JFileChooser.APPROVE_OPTION)
{
try{
File f2=jfc.getSelectedFile();
FileWriter fw=new FileWriter(f2); //�ڶ��ִ�ʩ
String str=jt.getText();
fw.write(str);
fw.close();
}catch (IOException io){System.out.println(io.getMessage());}
}
}
else if(e.getActionCommand().equals("����(S) ")) //�����ļ�
{
if(this.getTitle()=="�ޱ���-���±�") {
int save=jfc.showSaveDialog(this);
if(save==JFileChooser.APPROVE_OPTION)
try {
File f2 = jfc.getSelectedFile();
FileWriter fw = new FileWriter(f2); //�ڶ��ִ�ʩ
String str = jt.getText();
fw.write(str);
fw.close();
}catch (IOException i){}
}
else {
try {
String str = jt.getText();
FileWriter ra=new FileWriter(String.valueOf(ss));
ra.write(str);
ra.close();
}catch (IOException i){System.out.println(i.getMessage());}
} }
else if(e.getActionCommand().equals("�Զ�����(W)")) //�����ı��Ƿ��Զ�����
{
boolean b=!(jt.getLineWrap());
jt.setLineWrap(b);
}
else if(e.getActionCommand().equals("������ɫ(C)")) //�ı�������ɫ
{
Color color=jcc.getColor();
Color c=JColorChooser.showDialog(this,"��ɫѡ��",color);
jt.setForeground(c);
} }}
|