JavaSwing之图形界面编程之应用(一)

package three.day.frame;


import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.Dialog.ModalExclusionType;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;




public class Login extends JFrame {


/**
* Launch the application.
*/

public static void main(String[] args) 
{
new Login().setVisible(true);
}


/**
* Create the frame.
*/
public Login() {
//setType(Type.POPUP);
setTitle("登录");
getContentPane().setFont(new Font("宋体", Font.PLAIN, 12));
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});

getContentPane().setBackground(new Color(204, 255, 255));

//通过BoxLaout和Box实现登录界面的布局
BoxLayout vBoxLayout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
getContentPane().setLayout(vBoxLayout);
setBackground(Color.CYAN);
setBounds(600, 250, 300, 210);
Box hBox1 = new Box(BoxLayout.X_AXIS);
Box hBox2 = new Box(BoxLayout.X_AXIS);
Box hBox3 = new Box(BoxLayout.X_AXIS);
Box hLineBox1 = new Box(BoxLayout.X_AXIS);
Box hLineBox2 = new Box(BoxLayout.X_AXIS);
Box hLineBox3 = new Box(BoxLayout.X_AXIS);
getContentPane().add(hBox1);
getContentPane().add(hBox2);
getContentPane().add(hBox3);
getContentPane().add(hLineBox1);
getContentPane().add(hLineBox2);
getContentPane().add(hLineBox3);
JLabel lbName = new JLabel("姓名    ");
JLabel lbPasswd = new JLabel("密码    ");
final JTextField tfName = new JTextField("admin",10);
final JPasswordField tfPasswd = new JPasswordField("admin",10);
JButton btSure = new JButton("确定");
//添加时间监听器
btSure.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent e) 
{
//简单判断登录
if(tfName.getText().equals("admin") && new String(tfPasswd.getPassword()).equals("admin"))
{
new MainFrm("图书管理系统").setVisible(true);
dispose();
}
else
{
JOptionPane.showMessageDialog(null, "输入不正确,请重新输入");
}
}
});

//重置,清空
JButton btExit = new JButton("重置");
btExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfName.setText("");
tfPasswd.setText("");
}
});
//hLineBox1.add(Box.createRigidArea(new Dimension(20,5)));
hBox1.add(lbName);
hBox1.add(tfName);
hBox2.add(lbPasswd);
hBox2.add(tfPasswd);
hBox3.add(btSure);
hBox3.add(btExit);
}


}






class MainFrm extends JFrame
{



//private JPanel contentPane;
//声明菜单条
private JMenuBar menubar;
//声明菜单
private JMenu bkInfoInput;
private JMenu bkInfoModify;
private JMenu bkInfoCheck;
private JMenu bkGetCard;
private JMenu notepad;
//声明菜单项
private JMenuItem chkInfoByID;
private JMenuItem chkInfoByAll;


/**
* Create the frame.
*/
public MainFrm(String title) throws HeadlessException 
{
super(title);

//初始化菜单条
menubar = new JMenuBar();

//初始化菜单项,添加事件监听器
bkInfoInput = new JMenu("录入信息");
bkInfoInput.addMouseListener(new MouseAdapter()//实现匿名内部类(适配器)
{
@Override
public void mouseClicked(MouseEvent arg0) {
JOptionPane.showMessageDialog(null, "请录入办卡人信息");
}
});
//初始化菜单项,添加事件监听器
bkInfoModify = new JMenu("修改信息");
bkInfoModify.addMouseListener(new MouseAdapter()//实现匿名内部类(适配器)
{
@Override
public void mouseClicked(MouseEvent arg0) {
JOptionPane.showMessageDialog(null, "请录入办卡人信息");
}
});
//初始化菜单项,添加事件监听器
bkInfoCheck = new JMenu("查询信息");
//初始化菜单项,添加事件监听器
bkGetCard = new JMenu("办理图书证");
bkGetCard.addMouseListener(new MouseAdapter()//实现匿名内部类(适配器)
{
@Override
public void mouseClicked(MouseEvent arg0) {
JOptionPane.showMessageDialog(null, "请录入办卡人信息");
}
});
//初始化菜单项,添加事件监听器
notepad = new JMenu("记事本");
notepad.addMouseListener(new MouseAdapter()//实现匿名内部类(适配器
{
@Override
public void mouseClicked(MouseEvent arg0) {
new NotepadFrm().setVisible(true);
}
});

chkInfoByID = new JMenuItem("编号查询");
chkInfoByAll = new JMenuItem("全部查询");
bkInfoCheck.add(chkInfoByID);
bkInfoCheck.add(chkInfoByAll);
//添加事件监听器
chkInfoByID.addActionListener(new ActListen());//使用内部类对象作为事件委托对象(监听者或监听器)
chkInfoByAll.addActionListener(new ActListen());


setJMenuBar(menubar);

menubar.add(bkInfoInput);
menubar.add(bkInfoModify);
menubar.add(bkInfoCheck);
menubar.add(bkGetCard);
menubar.add(notepad);

setBounds(400, 125, 600,500);


addWindowListener(new WindowAdapter()
{


@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}

});
}
//内部类(内置类)
class ActListen implements ActionListener
{


@Override
public void actionPerformed(ActionEvent e) 
{
// TODO Auto-generated method stub
if(e.getSource()==chkInfoByID)
{
JOptionPane.showMessageDialog(null, "通过编号查询到的信息");
}
if(e.getSource()==chkInfoByAll)
{
JOptionPane.showMessageDialog(null, "查询全部的信息");
}
}
}//end definition of class ActListen
}//end MainFrm




class NotepadFrm extends JFrame
{

public NotepadFrm() 
{



setTitle("记事本");
//设置窗口大小,初始显示位置
setLocation(new Point(450,100));
this.setBounds(450, 100, 400, 500);
//添加一个文本域
final JTextArea ta = new JTextArea();
ta.setText("The Function is simple");
add(ta);

//通过工具包获取系统剪切板,用于暂存复制的文本内容
final Clipboard cb = this.getToolkit().getSystemClipboard();


//添加菜单功能
JMenuBar JMenubar = new JMenuBar();
JMenu mnFile = new JMenu("文件");
JMenu mnEdit = new JMenu("编辑");
JMenu mnFormat = new JMenu("格式");
JMenuItem miNew = new JMenuItem("新建");
//添加菜单项事件监听器
miNew.addActionListener(new ActionListener()//匿名内部类
{


@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
ta.setText("");
}

});
JMenuItem miOpen = new JMenuItem("打开");
//添加菜单项事件监听器
miOpen.addActionListener(new ActionListener()//匿名内部类
{


@Override
public void actionPerformed(ActionEvent e)
{
//显示一个打开文件对话框,获取其目录路径和文件名,并根据目录路径和文件名创建
//对应的文件,然后读取该文件内容,并保存到一个字符串对象,并在文本域显示
FileDialog fd = new FileDialog((Frame) getParent(), "打开文件",FileDialog.LOAD);
fd.setVisible(true);
  //fd.setBounds(new Rectangle(new Point(400,400)));
String strName = fd.getDirectory()+fd.getFile();

//strName有可能为空(用户不选择文件),为空则不处理
if(strName!=null)
{
try{
//创建文件
   File fl = new File(strName); 
   int fsize = (int) fl.length();
   
   //读取文件
FileReader  fr = new FileReader(fl);
char[] fileStr = new char[fsize];
int charsRead = 0;
       while(fr.ready())
       {
        charsRead += fr.read(fileStr, charsRead, fsize-charsRead);
       }
       fr.close();
       //显示文件内容
       ta.setText(new String(fileStr,0,charsRead));

   } catch (IOException e1) 
   {
// TODO Auto-generated catch block
e1.printStackTrace();
   }
}
}
}); //end miOpen.addActionListener


JMenuItem miSave = new JMenuItem("保存");
miSave.addActionListener(new ActionListener()
{


@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//显示保存文件对话框,与打开文件对话框仅仅一个参数不同
FileDialog fd = new FileDialog((Frame) getParent(), "保存文件",FileDialog.SAVE);
fd.setVisible(true);
String strPath = fd.getDirectory();
String strName02 = fd.getFile();

//可能为空,为空则不处理
   if(strName02!=null)
{
    try{
    //创建文件
    File fl = new File(strPath,strName02); 
    FileWriter file_writer = new FileWriter(fl);
    //将文本内容写入文件
    BufferedWriter out = new BufferedWriter(file_writer);
out.write(ta.getText(),0,(ta.getText()).length());
out.close();
file_writer.close();
    }catch(Exception e)
    {
    e.printStackTrace();
    }
}
}

});//end miSave.addActionListener

JMenuItem miExit = new JMenuItem("退出");
miExit.addActionListener(new ActionListener()
{


@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});

   
JMenuItem miCopy = new JMenuItem("复制");
miCopy.addActionListener(new ActionListener()
{


@Override
public void actionPerformed(ActionEvent e) {
// 获取选择的文本
String tempStr = ta.getSelectedText();
StringSelection ss = new StringSelection(tempStr);
//将选择的文本保存到系统剪切板
cb.setContents(ss, null);
}

});


JMenuItem miCut = new JMenuItem("剪切");
miCut.addActionListener(new ActionListener()
{


@Override
public void actionPerformed(ActionEvent e) {
// 获取选择的文本
String tempStr = ta.getSelectedText();
StringSelection ss = new StringSelection(tempStr);
//将选择的文本保存到系统剪切板
cb.setContents(ss, null);
int sStart = ta.getSelectionStart();
int sEnd = ta.getSelectionEnd();
//将选择的内容删除
ta.replaceRange("", sStart, sEnd);
}

});


JMenuItem miPaste = new JMenuItem("黏贴");
miPaste.addActionListener(new ActionListener()
{


@Override
public void actionPerformed(ActionEvent e) {
// 黏贴
Transferable tf = cb.getContents(this);
DataFlavor df = DataFlavor.stringFlavor;
if(tf.isDataFlavorSupported(df)) 
                    try{     String   str; 
                                str=(String)tf.getTransferData(df); 
                                ta.append(str); 
                          } 
                    catch(Exception   ee)
                    {
                    ee.printStackTrace();
                    } 
            
}

});


JMenuItem miFont = new JMenuItem("字体");
miFont.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//显示颜色选择对话框
JColorChooser.showDialog(null, "字体颜色", new Color(0,10,0));
}

});

mnFile.add(miNew);
mnFile.add(miOpen);
mnFile.add(miSave);
mnFile.add(miExit);
mnEdit.add(miCopy);
mnEdit.add(miCut);
mnEdit.add(miPaste);
mnFormat.add(miFont);
JMenubar.add(mnFile);
JMenubar.add(mnEdit);
JMenubar.add(mnFormat);
setJMenuBar(JMenubar);

//关闭记事本
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});




}//end NotepadFrm() 


}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值