import java.awt.*;
import java.awt.event.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.*;
class FristWindow extends JFrame implements ActionListener
{
Container con;
JMenuBar menubar;
JMenu menu1,menu2,menu3,menu4,menu5,menu5_1;
JSplitPane splitPane; //拆分窗格
JTextArea text1,text2; //文档区
JTextField titletext; //文档框
JPasswordField passwordtext; //密码文档框
JLabel label1,label2;
JButton buttonColor,buttonFile,buttonCopy,buttonPaste,buttonCut,buttonPlay;
JFileChooser fileChooser;
JToolBar tbar;
JPopupMenu menum; //鼠标菜单
JMenuItem item1_1,item1_2,item1_3,
item2_1,item2_2,item2_3,item2_4,item2_5,
item3_1,item3_2,
item4_1,
item5_1,item5_2_1,item5_2_2,
itemCopy,itemCut,itemPaste;
FristWindow(String s) //定义窗体各项参数
{
super(s); //调用父类的构照方法
setSize(500,400); //设置窗体初始大小
setLocation(120,120); //设置窗体初始位置
setVisible(true); //设置窗体是否可见
setResizable(true); //设置窗体是否可调整大小
menubar=new JMenuBar(); //初始化各项参数
//定义各项菜单
//定义菜单menu1
menu1=new JMenu("文件(F)");
menu1.setMnemonic('F');
item1_1=new JMenuItem("打开",new ImageIcon("open.gif"));
item1_2=new JMenuItem("保存",new ImageIcon("save.gif"));
item1_3=new JMenuItem("退出",new ImageIcon("exit.gif"));
item1_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); //设置热键
item1_2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
item1_3.setAccelerator(KeyStroke.getKeyStroke('X'));
menu1.add(item1_1);
menu1.add(item1_2);
menu1.addSeparator(); //添加菜单项分隔线
menu1.add(item1_3);
//定义菜单menu2
menu2=new JMenu("编辑(E)");
menu2.setMnemonic('E');
item2_1=new JMenuItem("复制",new ImageIcon("copy.gif"));
item2_2=new JMenuItem("剪切",new ImageIcon("cut.gif"));
item2_3=new JMenuItem("粘贴",new ImageIcon("press.gif"));
item2_4=new JMenuItem("删除",new ImageIcon("delete.gif"));
item2_5=new JMenuItem("撤消",new ImageIcon("unistall.gif"));
item2_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
item2_2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
item2_3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
item2_4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
item2_5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK));
item2_1.addActionListener(this);
item2_2.addActionListener(this);
item2_3.addActionListener(this);
menu2.add(item2_1);
menu2.add(item2_2);
menu2.add(item2_3);
menu2.add(item2_4);
menu2.addSeparator(); //添加菜单项分隔线
menu2.add(item2_5);
//定义菜单menu3
menu3=new JMenu("格式(G)");
menu3.setMnemonic('G');
item3_1=new JMenuItem("自动换行");
item3_2=new JMenuItem("字体");
item3_1.setAccelerator(KeyStroke.getKeyStroke('W'));
item3_2.setAccelerator(KeyStroke.getKeyStroke('F'));
menu3.add(item3_1);
menu3.add(item3_2);
//定义菜单menu4
menu4=new JMenu("查看(L)");
menu4.setMnemonic('L');
item4_1=new JMenuItem("状态");
item4_1.setAccelerator(KeyStroke.getKeyStroke('S'));
item4_1.setEnabled(false); //使菜单项不可用
menu4.add(item4_1);
//定义菜单menu5
menu5=new JMenu("帮助(H)");
menu5.setMnemonic('H');
menu5_1=new JMenu("关于记事本(A)");
item5_1=new JMenuItem("帮助主题");
item5_2_1=new JMenuItem("关于记事本");
item5_2_2=new JMenuItem("版本信息");
item5_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
item5_2_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
item5_2_2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,InputEvent.CTRL_MASK));
menu5.add(item5_1);
menu5.addSeparator(); //添加菜单项分隔线
menu5_1.add(item5_2_1);
menu5_1.add(item5_2_2);
menu5.add(menu5_1); //向菜单项中加子菜单
//定义鼠标弹出式菜单
menum=new JPopupMenu();
itemCut=new JMenuItem("剪切");
itemCopy=new JMenuItem("复制");
itemPaste=new JMenuItem("粘贴");
itemCopy.addActionListener(this);
itemCut.addActionListener(this);
itemPaste.addActionListener(this);
menum.add(itemCopy);
menum.add(itemCut);
menum.add(itemPaste);
menubar.add(menu1); //向菜单条中添加各项菜单
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
menubar.add(menu5);
setJMenuBar(menubar); //将菜单条添加到窗体中
text1=new JTextArea(); //定义文本文档区
text2=new JTextArea();
text1.addMouseListener(new MouseAdapter() //文档text1的触发监听器
{
public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{
menum.show(text1,e.getX(),e.getY());
}
}
}
);
// 添加标签和文本框
label1=new JLabel("标题");
label2=new JLabel("密码");
titletext=new JTextField(10);
passwordtext=new JPasswordField(10);
passwordtext.setEchoChar('*');
titletext.addActionListener(this);
passwordtext.addActionListener(this);
// 添加按纽和工具栏
buttonColor=new JButton("设置颜色");
buttonColor.addActionListener(this);
buttonFile=new JButton("打开文件");
buttonFile.addActionListener(this);
buttonCut=new JButton("剪切");
buttonCut.addActionListener(this);
buttonCopy=new JButton("复制");
buttonCopy.addActionListener(this);
buttonPaste=new JButton("粘贴");
buttonPaste.addActionListener(this);
buttonPlay=new JButton("娱乐");
buttonPlay.addActionListener(this);
tbar=new JToolBar();
tbar.add(buttonColor);
tbar.add(buttonFile);
tbar.add(buttonCopy);
tbar.add(buttonCut);
tbar.add(buttonPaste);
tbar.add(buttonPlay);
// 添加面板
JPanel jpan=new JPanel();
jpan.setLayout(new FlowLayout());
jpan.add(label1);
jpan.add(titletext);
jpan.add(label2);
jpan.add(passwordtext);
jpan.validate();
con=getContentPane(); //取得一个面板的引用
con.add(jpan,BorderLayout.SOUTH);
con.add(text1,BorderLayout.CENTER);
con.add(tbar,BorderLayout.NORTH);
//con.add(new JScrollPane(text1),BorderLayout.CENTER);
con.validate();
validate(); //刷新
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //设置单击窗体右上关闭图标后程序所做的处理
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==itemCopy||e.getSource()==item2_1) //复制
text1.copy();
else if(e.getSource()==itemCut||e.getSource()==item2_2) //剪切
text1.cut();
else if(e.getSource()==itemPaste||e.getSource()==item2_3)//粘贴
text1.paste();
else if(e.getSource()==titletext)
{
this.setTitle(titletext.getText()); //使文本框内容为窗体标题
}
else if(e.getSource()==passwordtext)
{
char c[]=passwordtext.getPassword(); //使密码框内容在文本框中显示
titletext.setText(new String(c));
}
else if(e.getSource()==buttonColor) //颜色对话框
{
Color newColor=JColorChooser.showDialog(this,"调色板",text1.getForeground());
text1.setForeground(newColor);
}
else if(e.getSource()==buttonFile) //文件对话框
{
text1.setText(null);
int n=fileChooser.showOpenDialog(con);
if(n==JFileChooser.APPROVE_OPTION)
{
File file=fileChooser.getSelectedFile();
try
{
FileReader readfile=new FileReader(file);
BufferedReader in=new BufferedReader(readfile);
String s=null;
while((s=in.readLine())!=null)
{
text1.append(s+"\n");
}
}
catch(IOException ee)
{
text1.setText("你没有选择文件");
}
}
}
else if(e.getSource()==buttonPlay) //消息框
{
JOptionPane.showMessageDialog(this,"嘿嘿,正在开发中~!~!","娱乐框",JOptionPane.WARNING_MESSAGE);
}
}
}
public class Example
{
public static void main(String[] args)
{
FristWindow win=new FristWindow("Skyer's Windows");
}
}
最近在学习用JAVA制作窗体,写了一个小程序,在这个程序中我想利用ButtonFile按钮打开选择系统文件的对话框,可是点击后没反应,不知道哪位可以帮我解决一下,谢谢了!