----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------
GUI
Graphical user Interface(图形用户接口)
用图形的方式,来显示计算机操作界面
java为GUI提供的界面都存在java.AWT和java.swing两个包中
实例:
public class Jfram extends JFrame{
public static void main(String[] args) {
Jfram jf=new Jfram();
jf.setSize(400,300);
jf.setLocation(300,100);
jf.setTitle("NUMBER");
jf.setLayout(new FlowLayout());
jf.addWindowListener(new WindowAdapter() {
public void windowclosing(){
System.out.print("关闭");
System.exit(0);
}
});//关闭窗口
TextField tf=new TextField(20);
Button button=new Button("按钮");
jf.add(tf);
jf.add(button);//添加按钮
// button.addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent arg0) {
// // TODO Auto-generated method stub
// System.exit(0);
// }
// });//退出按钮
button.addMouseListener(new MouseListener() {
private int i=0;
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("点击了: "+i++);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("鼠标进入: "+i++);
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
tf.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int code=e.getKeyCode();
if(!(code >= KeyEvent.VK_0 && code <=KeyEvent.VK_9)){
System.out.print("非法字符:"+code);
e.consume();
}
}
});
jf.setVisible(true);
}
}
简单记事本打开和保存
打开:
public class JFram2 extends JFrame{
public static Button bt;
private static TextField tf;
private static TextArea ta;
private static Dialog d;
private static Label la;
private static Button okbt;
public static void main(String[] args) {
JFram2 jf=new JFram2();
jf.setBounds(100, 100, 500, 400);
jf.setLayout(new FlowLayout());
jf.setTitle("JAVA");
d=new Dialog(jf, "显示错误",true);
la=new Label();
okbt=new Button("确定");
d.setBounds(200, 150, 300, 200);
d.setLayout(new FlowLayout());
d.add(la);
d.add(okbt);
bt=new Button("转换");
tf=new TextField(32);
ta=new TextArea(15,40);
jf.add(tf);
jf.add(bt);
jf.add(ta);
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text=tf.getText();//获得数据
File file=new File(text);
if(file.exists() && file.isDirectory()){
ta.setText("");
String[] names=file.list();
for(String name : names){
ta.append(name+"\t\n");
}
}
else
{
String info="您输入信息: "+text+" 是错误 , 请关闭重新输入";
la.setText(info);
d.setVisible(true);
}
//tf.setText("");//显示数据 制空
}
});
//按键盘回车键进入事件中
tf.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_ENTER){
String text=tf.getText();//获得数据
File file=new File(text);
if(file.exists() && file.isDirectory()){
ta.setText("");
String[] names=file.list();
for(String name : names){
ta.append(name+"\t\n");
}
}
else
{
String info="您输入信息: "+text+" 是错误 , 请关闭重新输入";
la.setText(info);
d.setVisible(true);
}
//tf.setText("");//显示数据 制空
}
}
});
//关闭弹出的对话框
d.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
d.setVisible(false);
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
});
//对话框按键
okbt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
d.setVisible(false);
}
});
//对话框回车键
okbt.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_ENTER){
d.setVisible(false);
}
}
});
jf.setVisible(true);
}
}
保存:
import javax.annotation.processing.Filer;
import javax.lang.model.element.Element;
import javax.swing.*;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileManager.Location;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class JFram3 extends JFrame{
private static Menu me,me2;
private static MenuBar mb;
private static MenuItem closeme,saveme,opendme;
private TextArea ta;
private FileDialog openfd,savefd;
private File file;
//事件体
public void eventall(){
closeme.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
//打开设计
opendme.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
openfd.setVisible(true);
String opendir=openfd.getDirectory();
String openname=openfd.getFile();
if(opendir==null ||openname==null)
return;
//ta.setText("");
File file=new File(opendir, openname);
try {
BufferedReader bf=new BufferedReader(new FileReader(file));
String line=null;
while((line=bf.readLine())!=null){
ta.append(line+"\r\n");
}
bf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//保存设计
saveme.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(file==null){
savefd.setVisible(true);
String savepath=savefd.getDirectory();
String savename=savefd.getFile();
if(savepath==null ||savename==null)
return;
file=new File(savepath, savename);
}
try {
BufferedWriter bf=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bf.write(text);
bf.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}
//界面设计
public void windowfram(){
JFram3 jf=new JFram3();
jf.setBounds(100, 100, 500, 400);
//jf.setLayout(new FlowLayout());
jf.setTitle("JAVA");
openfd=new FileDialog(jf,"打开窗体",FileDialog.LOAD);
savefd=new FileDialog(jf,"保存文件",FileDialog.SAVE);
ta=new TextArea();
me=new Menu("文件");//菜单项
me2=new Menu("编辑");
mb=new MenuBar();
opendme=new MenuItem("打开");
saveme=new MenuItem("保存");
closeme=new MenuItem("退出");//子菜单
me.add(opendme);
me.add(saveme);
me.add(closeme);
mb.add(me);
jf.add(ta);
jf.setMenuBar(mb);
jf.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFram3 j=new JFram3();
j.windowfram();
j.eventall();
}
}
----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------