【转自】http://blog.sina.com.cn/s/blog_616e189f0100ne1t.html

1.       基本的java Frame操作。

Java的图形界面的类主要包括AWT和Swing。在AWT中图形元素的父类为Component。

继承关系如下:Component->Container->Window->Frame->JFrame。(注意:Swing对AWT进行了扩展)。

下面给出一个简单的java图形程序:

package com.guan.visualTest.frameTest;

import java.awt.Button;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class MainFrame {

public static void main(String[] args)

{

//创建frame

JFrame frame = new JFrame("welcome!!");

//调整frame的大小和初始位置

frame.setSize(400, 400);

frame.setLocation(100, 100);

//新建5个Button

Button button1 = new Button("hello1");

Button button2 = new Button("hello2");

Button button3 = new Button("hello3");

Button button4 = new Button("hello4");

Button button5 = new Button("hello5");

//将5个Button添加到frame中

frame.add(button1,"East");

frame.add(button2,"West");

frame.add(button3,"South");

frame.add(button4,"Center");

frame.add(button5,"North");

//增加窗口监听事件,使用内部类方法,并用监听器的默认适配器

frame.addWindowListener(new WindowAdapter(){

//重写窗口关闭事件

@Override

public void windowClosing(WindowEvent arg0) {

System.exit(0);

}

});

//显示窗体

frame.setVisible(true);

}

}

执行效果:

2.       AWT的布局管理器

AWT中主要有四种布局管理器:FlowLayout、GridLayout、BorderLayout和CardLayout。

下面给出这四种布局管理器的源码:

package com.guan.visualTest.frameTest;

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.CardLayout;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class YourFrame extends Frame{

private static final long serialVersionUID = 1L;

Panel borderLayoutPanel;

Panel cardLayoutPanel;

Panel flowLayoutPanel;

Panel gridLayoutPanel;

private void generateGridLayoutPanel() {

gridLayoutPanel = new Panel();

gridLayoutPanel.setLayout(new GridLayout(2,2));

Button button1 = new Button("button1");

Button button2 = new Button("button2");

Button button3 = new Button("button3");

Button button4 = new Button("button4");

gridLayoutPanel.add(button1);

gridLayoutPanel.add(button2);

gridLayoutPanel.add(button3);

gridLayoutPanel.add(button4);

}

private void generateFlowLayoutPanel() {

flowLayoutPanel = new Panel();

flowLayoutPanel.setLayout(new FlowLayout());

Button button1 = new Button("button1");

Button button2 = new Button("button2");

Button button3 = new Button("button3");

Button button4 = new Button("button4");

Button button5 = new Button("button5");

button1.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

((Button)e.getSource()).setLabel("welcome ");

}

});

flowLayoutPanel.add(button1);

flowLayoutPanel.add(button2);

flowLayoutPanel.add(button3);

flowLayoutPanel.add(button4);

flowLayoutPanel.add(button5);

}

private void generateBorderLayoutPanel() {

borderLayoutPanel = new Panel();

borderLayoutPanel.setLayout(new BorderLayout());

Button button1 = new Button("South");

Button button2 = new Button("West");

Button button3 = new Button("East");

Button button4 = new Button("North");

Button button5 = new Button("Center");

borderLayoutPanel.add(button1,BorderLayout.SOUTH);

borderLayoutPanel.add(button2,BorderLayout.WEST);

borderLayoutPanel.add(button3,BorderLayout.EAST);

borderLayoutPanel.add(button4,BorderLayout.NORTH);

borderLayoutPanel.add(button5,BorderLayout.CENTER);

}

private void genrateCardLayoutPanel() {

cardLayoutPanel = new Panel();

final CardLayout cl = new CardLayout();

cardLayoutPanel.setLayout(cl);

Button button1 = new Button("black");

Button button2 = new Button("red");

ActionListener al = new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

cl.next(cardLayoutPanel);

}

};

button1.addActionListener(al);

button2.addActionListener(al);

cardLayoutPanel.add(button1,"1");

cardLayoutPanel.add(button2,"2");

}

public YourFrame(String panelName) {

super("panelName");

generateBorderLayoutPanel();

generateFlowLayoutPanel();

generateGridLayoutPanel();

genrateCardLayoutPanel();

setLayout(new GridLayout(2,2));

add(borderLayoutPanel);

add(flowLayoutPanel);

add(gridLayoutPanel);

add(cardLayoutPanel);

setSize(800, 800);

setLocation(100,100);

addWindowListener(new WindowAdapter(){

@Override

public void windowClosing(WindowEvent arg0) {

System.exit(0);

}

});

}

public static void main(String[] args) {

YourFrame yourFrame = new YourFrame("welcome");

yourFrame.setVisible(true);

}

}

运行结果:

3.       菜单栏的实现:

菜单栏关键的类包括MenuBar、Menu和MenuItem。下面给出测试代码:

package com.guan.visualTest.frameTest;

import java.awt.FileDialog;

import java.awt.Frame;

import java.awt.Menu;

import java.awt.MenuBar;

import java.awt.MenuItem;

import java.awt.TextArea;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

public class MenuFrame {

public static void main(String[] args) {

final Frame frame = new Frame();

frame.setSize(800,800);

frame.setLocation(100,100);

frame.addWindowListener(new WindowAdapter(){

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

final TextArea ta = new TextArea();

frame.add(ta);

//创建菜单栏

MenuBar mb = new MenuBar();

//创建菜单

Menu file = new Menu("File");

Menu edit = new Menu("Edit");

//创建菜单项

MenuItem mi1 = new MenuItem("Open");

//添加打开文件功能响应

mi1.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

FileDialog fd = new FileDialog(frame,"打开文件",FileDialog.LOAD);

fd.setVisible(true);

String fileName = fd.getDirectory()+fd.getFile();

if(fileName != null)

{

try {

FileInputStream fis = new FileInputStream(fileName);

byte[] buf = new byte[10*1024];

try {

int len = fis.read(buf);

ta.append(new String(buf,0,len));

fis.close();

catch (IOException e1) {

e1.printStackTrace();

}

catch (FileNotFoundException e1) {

e1.printStackTrace();

}

}

}

});

MenuItem mi2 = new MenuItem("Save");

MenuItem mi3 = new MenuItem("Other Save");

MenuItem mi4 = new MenuItem("Close");

//添加 关闭响应

mi4.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

System.exit(0);

}

});

MenuItem mi5 = new MenuItem("Cope");

MenuItem mi6 = new MenuItem("Paste");

file.add(mi1);

file.add(mi2);

file.add(mi3);

file.add(mi4);

edit.add(mi5);

edit.add(mi6);

mb.add(file);

mb.add(edit);

frame.setMenuBar(mb);

frame.setVisible(true);

}

}

执行结果:

4.       最后Swing的简单测试

package com.guan.visualTest.frameTest;

import java.awt.BorderLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

public class SwingFrame {

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton button = new JButton("ok");

frame.getContentPane().add(button,BorderLayout.WEST);

frame.setSize(800,800);

frame.setLocation(100, 100);

frame.setVisible(true);

}

}

【转】java图形界面设计(AWT)的更多相关文章

  1. java图形界面设计

    1.       基本的java Frame操作. Java的图形界面的类主要包括AWT和Swing.在AWT中图形元素的父类为Component. 继承关系如下:Component->Cont ...

  2. Java图形界面设计——substance皮肤

    http://jianweili007-163-com.iteye.com/blog/1141358 ————————————————————————————————————————————————— ...

  3. Java图形界面学习---------简易登录界面

    /** * @author Administrator * Java图形界面学习---------简易登录界面 * date:2015/10/31 */ import java.awt.BorderL ...

  4. Java 图形界面开发--图文并茂建立学生管理系统

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客) 图形用户界面(Graphics U ...

  5. Java图形界面GUI

    Java图形界面GUI 设置窗体JFrame对象 package com.Aha.Best; import javax.swing.ImageIcon; import javax.swing.JFra ...

  6. MATLAB图形界面设计(下)

    文章参考Blue Mountain https://www.cnblogs.com/BlueMountain-HaggenDazs/p/4307777.html 一.菜单设计 1.建立菜单项 (1)建 ...

  7. Sprint 2 : ios图形界面设计与代码整合

    这周我们主要focus在personal photo experience 项目的ios图形界面设计与代码整合工作上. 工作进度: 1. 图形界面设计方面:兆阳和敏龙基本已经将ios手机客户端的雏形界 ...

  8. python的re模块一些方法 && Tkinter图形界面设计 && 终止python运行函数 && python读写文件 && python一旦给字符串赋值就不能单独改变某个字符,除非重新给变量赋值

    Tkinter图形界面设计见:https://www.cnblogs.com/pywjh/p/9527828.html#radiobutton 终止python运行函数: 采用sys.exit(0)正 ...

  9. Java图形界面实战案例——实现打字母游戏

    实现打字母的游戏 这次这个案例能够说是头几次所讲的内容的一个技术汇总,主要是 运用了几大块的知识.我们先来定义一下案例的背景:在一个300*400的窗口上.有10个随机产生的字母下落,在键盘上敲击字母 ...

随机推荐

  1. windows 安装 docker

    .https://download.docker.com/win/stable/InstallDocker.msi .设置环境变量 C:\Program Files (x86)\Git\bin .如果 ...

  2. 封装document.getElementById(id)

      CreateTime--2016年12月18日11:42:45Author:Marydon封装document.getElementById(Id)方法 <script type=" ...

  3. java线程具体解释

    线程与进程的差别 (1)程序是一段静态的代码,进程是程序的一次动态执行过程.它是操作系统资源调度的基本单位.线程是比进程更小的执行单位.一个进程在其执行过程中,能够产生多个线程.所以又称线程为&quo ...

  4. dojo之配置dojoconfig

    官方教程:Configuring Dojo with dojoConfig例子: <-- set Dojo configuration, load Dojo --> <script& ...

  5. gzip和zipfile模块

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #gzip和zipfile模块 #http://www.open-open.com/lib/view/open ...

  6. RHEL7 timedatectl命令

    1.要显示系统的当前时间和日期,使用timedatectl命令,如下: [root@rhel7 ~]# timedatectl Local time: Mon -- :: EDT Universal ...

  7. VC2012编译CEF3-转

    原文地址:http://blog.csdn.net/tiplip/article/details/42047815 下载 代码下载:http://cefbuilds.com/,CEF 3.2556.1 ...

  8. mysql中的order by

    一.order by的原理 1.利用索引的有序性获取有序数据 当查询语句的 order BY 条件和查询的执行计划中所利用的 Index 的索引键(或前面几个索引键)完全一致,且索引访问方式为 ran ...

  9. ios block常见的错误(二)——循环引用

    这篇博文继续block的常见错误——循环引用. 循环引用是很多初学者不能察觉的,其产生的原因,是block中的代码会对对象进行强引用. 读者请阅读示例代码1,并思考示例代码1所创建的对象能否被正常销毁 ...

  10. nyoj---快速查找素数

    快速查找素数 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 现在给你一个正整数N,要你快速的找出在2.....N这些数里面所有的素数.   输入 给出一个正整数数N ...