java图形界面设计
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); } }
转自:http://blog.sina.com.cn/s/blog_616e189f0100ne1t.html
java图形界面设计的更多相关文章
- 【转】java图形界面设计(AWT)
[转自]http://blog.sina.com.cn/s/blog_616e189f0100ne1t.html 1. 基本的java Frame操作. Java的图形界面的类主要包括AW ...
- Java图形界面设计——substance皮肤
http://jianweili007-163-com.iteye.com/blog/1141358 ————————————————————————————————————————————————— ...
- Java图形界面学习---------简易登录界面
/** * @author Administrator * Java图形界面学习---------简易登录界面 * date:2015/10/31 */ import java.awt.BorderL ...
- Java 图形界面开发--图文并茂建立学生管理系统
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客) 图形用户界面(Graphics U ...
- Java图形界面GUI
Java图形界面GUI 设置窗体JFrame对象 package com.Aha.Best; import javax.swing.ImageIcon; import javax.swing.JFra ...
- MATLAB图形界面设计(下)
文章参考Blue Mountain https://www.cnblogs.com/BlueMountain-HaggenDazs/p/4307777.html 一.菜单设计 1.建立菜单项 (1)建 ...
- Sprint 2 : ios图形界面设计与代码整合
这周我们主要focus在personal photo experience 项目的ios图形界面设计与代码整合工作上. 工作进度: 1. 图形界面设计方面:兆阳和敏龙基本已经将ios手机客户端的雏形界 ...
- python的re模块一些方法 && Tkinter图形界面设计 && 终止python运行函数 && python读写文件 && python一旦给字符串赋值就不能单独改变某个字符,除非重新给变量赋值
Tkinter图形界面设计见:https://www.cnblogs.com/pywjh/p/9527828.html#radiobutton 终止python运行函数: 采用sys.exit(0)正 ...
- Java第5次实验提纲(Java图形界面编程)
1. Swing与NetBeans 使用NetBeans编写简单界面.见GUI实验参考文件中的0.第06次实验(图形程序设计.事件处理与Swing).doc 题目1: Swing用户界面组件与事件处理 ...
随机推荐
- Git可视化极简易教程 — Git GUI使用方法
Git可视化极简易教程 — Git GUI使用方法 学习了:http://www.runoob.com/w3cnote/git-gui-window.html
- 控制面板的cpl程序列表
控制面板的cpl程序列表 学习了:https://zhidao.baidu.com/question/2141898537654796628.html 最近用来sysdm.cpl: 辅助功能选项:ac ...
- Batch update returned unexpected row count from update [0];
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested ...
- lnmp环境网页访问慢排查思路
1.首先看每个服务器的负载情况 2.若各个服务器负载不高 首先查看是不是负载均衡服务器问题相接访问web服务看是否慢,若也慢则查看是不是访问动态页面慢,创建一个静态页面访问试试,若不慢则是动态页面问题 ...
- PocketBeagle 初高级设置
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正.本文使用markdown标记语言写成,为获得最好的阅读体验,请访问我的博客原文. 1. PocketBeagle Summary ...
- Spring 小记
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 使用STS新建spr ...
- iOS中UDP的使用
// // ViewController.m // UDPDemo // // Created by qianfeng01 on 15-8-13. // Copyright (c) 2015年 ...
- 一起学android之怎样设置TextView中不同字段的字体颜色(22)
在这里先看看效果图: OK,有时候,在我们的项目中会要求TextView中文本有一部分的字体颜色不一样.这时我们应该使用 SpannableStringBuilder这个工具类,当然这个类的功能非常强 ...
- scrapy递归抓取网页数据
scrapy spider的parse方法能够返回两种值:BaseItem.或者Request.通过Request能够实现递归抓取. 假设要抓取的数据在当前页,能够直接解析返回item(代码中带**凝 ...
- keras----resnet-vgg-xception-inception
来源: https://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/ classi ...