黑马程序员:Java基础总结
GUI图形化界面
ASP.Net+Android+IO开发、.Net培训、期待与您交流!
GUI(Graphical User Interface)图形化界面编程总结Java.Awt:Abstract Window ToolKit(抽象窗口工具包)Javax.Swing:
图形化界面基本设置:设置窗体大小:setSize(长,宽);设置窗体位置:setLocation(距离左,距离上);setBounds(长,宽,距离左,距离上);设置布局:setLayout(new FlowLayout());使窗体可见:setVisible(true);事件监听机制:
事件监听机制的特点:1,事件源。2,事件。3,监听器。4,事件处理。事件源:就是awt包或者swing包中的那些图形界面组件。事件:每一个事件源都有自己特有的对应事件和共性事件。监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。以上三者,在java中都已经定义好了。直接获取其对象来用就可以了。 我们要做的事情是,就是对产生的动作进行处理
Eg:编写程序,练习图形化界面编程!
import java.awt.*;import java.awt.event.*;import java.io.*;
public class MyWindowDemo { private Frame f ; private TextField tf ; private Button but ; private TextArea ta ; private Dialog d ; private Label lab ; private Button okBut ;
MyWindowDemo() { init(); }
public void init() { f = new Frame("my window" ); f.setBounds(300, 100, 600, 500); f.setLayout( new FlowLayout()); tf = new TextField(60); but = new Button( "转到"); ta = new TextArea(25, 70); d = new Dialog(f , "提示信息-self" , true); d.setBounds(400, 200, 240, 150); d.setLayout( new FlowLayout()); lab = new Label(); okBut = new Button( "确定"); d.add( lab ); d.add( okBut ); f.add( tf); f.add( but ); f.add( ta); myEvent(); f.setVisible( true ); }
private void myEvent() { okBut .addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { d.setVisible( false ); } }); d.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { d.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(); } }); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System. exit(0); } }); }
private void showDir() { String dirPath = tf.getText(); File dir = new File(dirPath); if (dir.exists() && dir.isDirectory()) { ta.setText( "" ); String[] names = dir.list(); for (String name : names) { ta.append(name + "\r\n" ); } } else { String info = "您输入的信息:" + dirPath + "是错误的。请重输" ; lab .setText(info); d.setVisible( true ); } }
public static void main(String[] args) { new MyWindowDemo(); }}
菜单:
MenuBar 菜单整体;Menu 包含于MenuBar中;MenuItem包含于Menu中!以上3个关系添加用add();将MenuBar放进Frame中用setMenuBar();FileDialog.LOAD:打开的modeFileDialog.Save:保存的mode
练习:一个简易的记事本
import java.awt.*;import java.awt.event.*;import java.io.*;
public class MyMenuDemo { private Frame f ; private TextArea ta ; private MenuBar mb ; private Menu me ; private MenuItem miNew , miSave , miLoad ; private File file ; private FileDialog openDia , saveDia ;
MyMenuDemo() { init(); }
public void init() { f = new Frame("万琪--记事本" ); f.setBounds(300, 100, 650, 600); ta = new TextArea(); // ta.setBounds(302,105,400,300); // f.setLayout(new FlowLayout());//为什么设置成了流式布局之后再设置文本区域没有效果 mb = new MenuBar(); me = new Menu("文件(F)" ); miNew = new MenuItem("新建"); miSave = new MenuItem("保存(S)"); miLoad = new MenuItem("打开(O)"); mb.add( me); me.add( miNew); me.add( miLoad); me.add( miSave); f.setMenuBar( mb); f.add( ta); openDia = new FileDialog(f, "打开文件", FileDialog. LOAD); saveDia = new FileDialog(f, "保存文件", FileDialog. SAVE); myEvent(); f.setVisible( true); }
public void myEvent() { f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System. exit(0); } }); miLoad.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openDia.setVisible(true); String dirPath = openDia.getDirectory(); String fileName = openDia.getFile(); if (dirPath == null || fileName == null) return; ta.setText( ""); file = new File(dirPath, fileName); try { BufferedReader bufr = new BufferedReader(new FileReader( file)); String line = null; while ((line = bufr.readLine()) != null) { ta.append(line + "\r\n"); } bufr.close(); } catch (IOException ex) { throw new RuntimeException("读取失败"); } } }); miSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (file == null) { saveDia.setVisible(true); String dirPath = saveDia.getDirectory(); String fileName = saveDia.getFile(); if (dirPath == null || fileName == null) return; file = new File(dirPath, fileName); } try { BufferedWriter bufw = new BufferedWriter(new FileWriter( file)); String text = ta.getText(); bufw.write(text); bufw.close(); } catch (IOException ex) { throw new RuntimeException(""); } } }); }
public static void main(String[] args) { new MyMenuDemo(); }}
打jar包的过程:1、 编译包 jar –cvf jar名 包名2、 写一个文件main-class: 包名.类名【加个回车】3、 jar –cvfm jar名 文件名 包名配置jar执行的过程:工具---文件夹选项---文件类型---新建---文件扩展名jar然后确定----高级----修改图标-----open------javaw的路径—jar即可!
ASP.Net+Android+IO开发、.Net培训、期待与您交流!