📄 zy.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class zy extends WindowAdapter implements ActionListener
{
JFrame f=new JFrame("登陆窗口"); //创建登陆窗体
JPanel p=new JPanel(); //创建面板
JLabel l1=new JLabel("用户名:"); //创建用户名标签
JLabel l2=new JLabel("密 码: "); //创建密码标签
JTextField name=new JTextField(12); //创建用户名框
JPasswordField psd=new JPasswordField(12); //创建密码框
JButton b1=new JButton("确定"); //创建确定按钮
JButton b2=new JButton("退出"); //创建退出按钮
public zy()
{
p.add(l1);
p.add(name);
p.add(l2);
p.add(psd);
p.add(b1);
p.add(b2);
b1.addActionListener(this);//为确定按钮添加监听器
b2.addActionListener(this);//为退出按钮添加监听器
f.getContentPane().add(p); //获取内容窗格
f.setSize(200,150);//设置窗体大小位置
f.setVisible(true);//设置窗体为可见
}
public static void main(String args[])
{
new zy();//调用Public类的构造方法
}
public void actionPerformed(ActionEvent e)
{ if (e.getSource()==b1) //判断用户点击的为那个按钮
{
if(name.getText().equals("zy")&&psd.getText().equals("123"))
{
new JM ();f.setVisible(false);//关闭登陆窗口
}
else
{
JOptionPane.showConfirmDialog(null, "用户名和密码不正确!","注意",
JOptionPane.INFORMATION_MESSAGE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name.setText(""); //将用户名清空
psd.setText(""); //密码清空
}
}
else if(e.getSource()==b2)
{
System.exit(0);
}
}
}
class JM extends JFrame implements ActionListener//主窗体
{
JMenuItem a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;
JTextArea t;
Font ft;
int fsize=25;
Color color=Color.blue;
boolean isChange=false;
JToolBar bar=new JToolBar();
//创建工具栏
Action xjAction=new AbstractAction("new",new ImageIcon("new.JPG"))//工具拦
{
public void actionPerformed(ActionEvent e)
{
t.setText("");
}
};
Action dkAction=new AbstractAction("new",new ImageIcon("open.JPG"))
{
public void actionPerformed(ActionEvent e)
{
fileopen();
}
};
Action bcAction=new AbstractAction("new",new ImageIcon("save.JPG"))
{
public void actionPerformed(ActionEvent e)
{
fileSave();
}
};
JM()
{
super("菜单的使用");
Container c=getContentPane();
ft=new Font("宋体",Font.PLAIN,fsize);
t=new JTextArea(7,20);
t.setFont(ft);
t.setForeground(color);
JScrollPane sp=new JScrollPane(t);
c.add(sp,"South");
bar.add(xjAction);
bar.add(dkAction);
bar.add(bcAction);
c.add(bar,"Center");
//创建菜单
a=new JMenuItem("新建(N)");
a.setMnemonic(KeyEvent.VK_N);
a1=new JMenuItem("打开(O)");
a1.setMnemonic(KeyEvent.VK_O );
a2=new JMenuItem("保存(C)");
a2.setMnemonic(KeyEvent.VK_C );
a3=new JMenuItem("退出(E)");
a3.setMnemonic(KeyEvent.VK_E );
JMenu m2=new JMenu("文件(F)");//创建主菜单
m2.setMnemonic(KeyEvent.VK_F );
m2.add(a);
m2.add(a1);
m2.add(a2);
m2.addSeparator();//分割线
m2.add(a3);
a.addActionListener(this);
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4=new JMenuItem("复制(C)");
a4.setMnemonic(KeyEvent.VK_C );
a5=new JMenuItem("粘贴(P)");
a5.setMnemonic(KeyEvent.VK_P );
a7=new JMenuItem("粗体");
a8=new JMenuItem("斜体");
a9=new JMenuItem("大小(S)");
a9.setMnemonic(KeyEvent.VK_S );
a10=new JMenuItem("颜色(C)");
a10.setMnemonic(KeyEvent.VK_C );
JMenu m3=new JMenu("编辑(E)");
m3.setMnemonic(KeyEvent.VK_E );
m3.add(a4);
m3.add(a9);
m3.add(a10);
m3.add(a5);
m3.add(a7);
m3.add(a8);
a4.addActionListener(this);
a5.addActionListener(this);
a7.addActionListener(this);
a8.addActionListener(this);
a9.addActionListener(this);
a10.addActionListener(this);
a6=new JMenuItem(" 关于我");
JMenu m4=new JMenu("帮助(H)");
m4.setMnemonic(KeyEvent.VK_H);
m4.add(a6);
a6.addActionListener(this);
JMenuBar mb=new JMenuBar();
mb.add(m2);
mb.add(m3);
mb.add(m4);
setJMenuBar(mb) ;
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==a9)
{
String s=JOptionPane.showInputDialog("字号大小:");
fsize=Integer.parseInt(s);
ft=new Font("宋体",Font.PLAIN,fsize);
t.setFont(ft);
}
if(e.getSource()==a10)
{
color=JColorChooser.showDialog(this,"调色板",Color.red);
t.setForeground(color);
}
if(e.getSource()==a1)//打开命令
{
FileDialog fd=new FileDialog(this, "打开文件");
fd.setVisible(true);
String s=fd.getDirectory()+fd.getFile();
setTitle(s);
File f=new File(s);
s="";
try
{
FileReader fr=new FileReader(f);
int i;
while((i=fr.read())!=-1)
s=s+(char)i;
fr.close();
t.setText(s);
}
catch(IOException ee){}
}
if(e.getSource()==a)//新建
{
t.setText("");
}
if(e.getSource()==a2)//保存
{
fileSave();
}
if(e.getSource()==a3)//退出
{
if(isChange==false)
{
System.exit(0);
}
}
if(e.getSource()==a4)//复制
{
t.copy();
}
if(e.getSource()==a5)//粘贴
{
t.paste();
}
if(e.getSource()==a6)//关于我
{
JFrame ff=new JFrame("我的个人简历");
JLabel j1=new JLabel("班级:");
JLabel j2=new JLabel("姓名:");
JLabel j3=new JLabel("学号:");
JLabel j4=new JLabel("专业:");
JLabel j5=new JLabel("爱好:");
JLabel k1=new JLabel("D06计2");
JLabel k2=new JLabel("张勇");
JLabel k3=new JLabel("0603210210");
JLabel k4=new JLabel("计算机应用技术");
JLabel k5=new JLabel("旅游");
ff.setLayout(new FlowLayout());
ff.add(j1);
ff.add(k1);
ff.add(j2);
ff.add(k2);
ff.add(j3);
ff.add(k3);
ff.add(j4);
ff.add(k4);
ff.add(j5);
ff.add(k5);
ff.setSize(280,350);
ff.setVisible(true);
}
if(e.getSource()==a7)//字体加粗
{
int mode=0,size=10;
mode=mode+Font.BOLD;
t.setFont(new Font("楷书",mode,20));
}
if(e.getSource()==a8)//字体斜体
{
int mode=0,size=10;
mode=mode+Font.ITALIC;
t.setFont(new Font("楷书",mode,20));
}
if(e.getSource()==a2)//文件打开
{
int selection = JOptionPane.showConfirmDialog(null,"是否对该文件进行保存", "提示!",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(selection == JOptionPane.OK_OPTION)
{
fileSave();
t.setText("");
}
else
t.setText("");
}
}
private void fileopen()
{
FileDialog fd=new FileDialog(this, "打开文件");
fd.setVisible(true);
String s=fd.getDirectory()+fd.getFile();
setTitle(s);
File f=new File(s);
s="";
try
{
FileReader fr=new FileReader(f);
int i;
while((i=fr.read())!=-1)
s=s+(char)i;
fr.close();
t.setText(s);
}
catch(IOException ee){}
}
private void fileSave()
{
FileDialog fd=new FileDialog(this, "保存文件",FileDialog.SAVE);
fd.setVisible(true);
File f=new File(fd.getDirectory()+fd.getFile());
try
{
FileWriter fr=new FileWriter(f);
String s=t.getText();
fr.write(s);
fr.close();
}
catch(IOException ee){}
}
};
class NoteBook
{
public static void main(String[] args)
{
JM obj=new JM();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -