黑马程序员—GUI(菜单)小例子
2013-12-14 19:58
495 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
package twenty_two;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Ten {
public static void main(String[] args) {
new Mwinn();
}
}
class Mwinn
{
private Frame f;
private TextField tf;
private Button but;
private Button okbut;
private TextArea ta;
private Label lae;
private Dialog dg;
Mwinn()
{
init();
}
public void init()
{
f=new Frame("中");
f.setBounds(300,200,500,999);
f.setLayout(new FlowLayout());
tf=new TextField(30);
but=new Button("转到");
ta=new TextArea(15,40);
dg=new Dialog(f,"提示信息—self",true);
dg.setBounds(500,200,300,160);
dg.setLayout(new FlowLayout());
lae=new Label();
okbut=new Button("确定");
dg.add(lae);
dg.add(okbut);
f.add(tf);
f.add(but);
f.add(ta);
mevent();
f.setVisible(true);
}
private void mevent()
{
okbut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dg.setVisible(false);
}});
tf.addKeyListener( new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER);
showDir();
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
dg.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dg.setVisible(false);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private void showDir()
{
String dirPath=tf.getText();
File dir=new File(dirPath);//D:\\java\\workspace\\heima\\src\\twenty_two
if(dir.exists()&&dir.isDirectory())
{
ta.setText("");//不加它两次目录的东西都会存在。
String[]names=dir.list();
for(String name :names)
{
ta.append(name+"\r\n");//将所有的文件都输出来。
}
}
else
{
String info="您的路径为:"+ dirPath+"这是错误的路径";
lae.setText(info);
dg.setVisible(true);
}
}
}
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
package twenty_two;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Ten {
public static void main(String[] args) {
new Mwinn();
}
}
class Mwinn
{
private Frame f;
private TextField tf;
private Button but;
private Button okbut;
private TextArea ta;
private Label lae;
private Dialog dg;
Mwinn()
{
init();
}
public void init()
{
f=new Frame("中");
f.setBounds(300,200,500,999);
f.setLayout(new FlowLayout());
tf=new TextField(30);
but=new Button("转到");
ta=new TextArea(15,40);
dg=new Dialog(f,"提示信息—self",true);
dg.setBounds(500,200,300,160);
dg.setLayout(new FlowLayout());
lae=new Label();
okbut=new Button("确定");
dg.add(lae);
dg.add(okbut);
f.add(tf);
f.add(but);
f.add(ta);
mevent();
f.setVisible(true);
}
private void mevent()
{
okbut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dg.setVisible(false);
}});
tf.addKeyListener( new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER);
showDir();
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
dg.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dg.setVisible(false);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private void showDir()
{
String dirPath=tf.getText();
File dir=new File(dirPath);//D:\\java\\workspace\\heima\\src\\twenty_two
if(dir.exists()&&dir.isDirectory())
{
ta.setText("");//不加它两次目录的东西都会存在。
String[]names=dir.list();
for(String name :names)
{
ta.append(name+"\r\n");//将所有的文件都输出来。
}
}
else
{
String info="您的路径为:"+ dirPath+"这是错误的路径";
lae.setText(info);
dg.setVisible(true);
}
}
}
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
相关文章推荐
- java String,StringBuffer,StringBuilder区别及联系
- uiautomator介绍和使用
- Unique Paths II
- 云计算(十六)- HDFS配额指南(HDFS Quotas Guide)
- Xcode 5 Error CertUIFramework.axbundle
- How to build the chromium on ubuntu12.04
- 32位DLL注册到64位系统下,需要把IIS中的“启用32位应用程序改为:true
- (转)關於flashback query的ORA-01466錯誤
- Android之AlertDialog.Builder详解
- Storyboard里面的几种Segue区别:push,modal,popover,replace和custom(转载)
- [LeetCode] [N皇后问题] N-Queens & N-Queens II
- 修改easyui combobox源码,实现模糊搜索
- 关于J-UI DWZ富框架在IE10下兼容性问题
- UIView之setNeedsDisplay、setNeedsLayout
- get values of particular key in nsdictionary
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- codeforces B. Making Sequences is Fun 解题报告
- iOS小技巧:如何给UIPageControl更换图片和颜色
- fw ->>>>>>> 2013年12月13日23:16:30 ->> quick_sort
- 动态改变actionbar的menu菜单MenuItem