2016.3.16(Java图形用户界面)
边界布局
public class BorderLayoutTest extends JFrame{
public BorderLayoutTest(){
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("边界布局");
this.setLocationRelativeTo(null);//设置窗体居中
this.addContent();
this.setVisible(true);
}
public void addContent(){
Container con = this.getContentPane();
con.setLayout(new BorderLayout());
JButton btn1 = new JButton("确定1");
JButton btn2 = new JButton("确定2");
JButton btn3 = new JButton("确定3");
JButton btn4 = new JButton("确定4");
JButton btn5 = new JButton("确定5");
con.add(btn1, BorderLayout.EAST);
con.add(btn2, BorderLayout.WEST);
con.add(btn3, BorderLayout.NORTH);
con.add(btn4, BorderLayout.SOUTH);
con.add(btn5, BorderLayout.CENTER);
}
public static void main(String[] args) {
new BorderLayoutTest();
}
}
流布局
public class FlowLayoutTest extends JFrame{
public FlowLayoutTest(){
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("流布局");
this.setLocationRelativeTo(null);//设置窗体居中
this.addContent();
this.setVisible(true);
}
public void addContent(){
Container con = this.getContentPane();
con.setLayout(new FlowLayout());
JLabel label = new JLabel("Test");
JButton btn = new JButton("根据元素内容确定大小");
JButton btn2 = new JButton("确定");
JButton btn3 = new JButton("确定");
JButton btn4 = new JButton("确定");
JButton btn5 = new JButton("确定5");
con.add(label);
con.add(btn);
con.add(btn2);
con.add(btn3);
con.add(btn4);
con.add(btn5);
}
public static void main(String[] args) {
new FlowLayoutTest();
}
}
网络布局
public class GridLayoutTest extends JFrame {
public GridLayoutTest(){
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("网格布局");
this.setLocationRelativeTo(null);//设置窗体居中
this.addContent();
this.setVisible(true);
}
public void addContent(){
Container con = this.getContentPane();
con.setLayout(new GridLayout(2,3));
JButton btn1 = new JButton("确定1");
JButton btn2 = new JButton("确定2");
JButton btn3 = new JButton("确定3");
JButton btn4 = new JButton("确定4");
JButton btn5 = new JButton("确定5");
JButton btn6 = new JButton("确定6");
JButton btn7 = new JButton("确定7");
JButton btn8 = new JButton("确定8");
JButton btn9 = new JButton("确定9");
con.add(btn1);
con.add(btn2);
con.add(btn3);
con.add(btn4);
con.add(btn5);
con.add(btn6);
con.add(btn7);
con.add(btn8);
con.add(btn9);
}
public static void main(String[] args) {
new GridLayoutTest();
}
}
事件:
public class ActionListenerTest extends JFrame {
public ActionListenerTest(){
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("边界布局");
this.setLocationRelativeTo(null);//设置窗体居中
this.addContent();
this.setVisible(true);
}
private void addContent() {
Container con = this.getContentPane();
con.setLayout(new FlowLayout());
JButton btn1 = new JButton("红色");
JButton btn2 = new JButton("绿色");
// ButtonListener btLis = new ButtonListener(this);
// btn1.addActionListener(btLis);
// btn2.addActionListener(btLis);
btn1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(Color.RED);
}});
btn2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(Color.GREEN);
}});
con.add(btn1);
con.add(btn2);
}
// @Override
// public void actionPerformed(ActionEvent e) {
// System.out.println("进入");
// Object obj = e.getSource();
// JButton btn = (JButton)obj;
// String text = btn.getText();
//
// if(text.equals("红色")){
// this.getContentPane().setBackground(Color.RED);
// }
//
// if(text.equals("绿色")){
// this.getContentPane().setBackground(Color.GREEN);
// }
// }
public static void main(String[] args) {
new ActionListenerTest();
}
}
另一种方法添加事件 实现ActionListener
public class ButtonListener implements ActionListener {
private JFrame jf;
public ButtonListener() {
}
public ButtonListener(JFrame jf) {
this.jf = jf;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("进入123");
Object obj = e.getSource();
JButton btn = (JButton) obj;
String text = btn.getText();
if (text.equals("红色")) {
this.jf.getContentPane().setBackground(Color.RED);
}
if (text.equals("绿色")) {
this.jf.getContentPane().setBackground(Color.GREEN);
}
}
}
转载于:https://www.cnblogs.com/CMCM/p/5285682.html
2016.3.16(Java图形用户界面)相关推荐
- Java图形用户界面设计音乐播放器
Java图形用户界面设计音乐播放器 设计思路 音乐播放器的基础是可以播放音乐,所以要有一个实现播放音乐功能的类audioplay,图形界面就是通过动作调用audioplay里面的函数.关于实现播放器图 ...
- java界面设计素材_9.java图形用户界面设计.ppt
9.java图形用户界面设计.ppt 还剩 150页未读, 继续阅读 下载文档到电脑,马上远离加班熬夜! 亲,很抱歉,此页已超出免费预览范围啦! 如果喜欢就下载吧,价低环保! 内容要点: 151v 程 ...
- java图形用户界面实验_java图形用户界面实验教程
java图形用户界面实验教程 实 验 五 图 形 用 户 界 面实 验 目 的 1. 了 解 图 形 用 户 界 面 的 概 念 :2. 了 解 AWT 和 Swing 的 基 本 体 系 结 构 : ...
- java图形用户界面概述_Java中图形用户界面概述
Java中图形用户界面概述,转型或者高薪就业,java工程师是不错的选择,而且java是目前乃至未来都主流的程序语言.Java中图形用户界面概述,一起来学习下java方面的知识吧. 1.1.组件 组件 ...
- JavaGUI——Java图形用户界面
1.Java GUI 概述 GUI(Graphical User Interface,简称 GUI,图形用户界面)是指采用图形方式显示的计算机操作用户界面,与早期计算机使用的命令行界面相比,图形界面对 ...
- Java图形用户界面设计之概述
文章目录 前言 一.GUI概述 1.创建 GUI 的一般步骤: (1)Component类 (2)Container类 (3)Window类 (4)Frame 类 例:建立一个Frame对象,并使之可 ...
- java图形用户界面控件介绍,GUI(图形用户界面)详解
一.GUI(图形用户界面) GUI Graphical User Interface(图形用户接口) 用图形的方式,来显示计算机操作的界面,这样更方便更直观 CLI Command line User ...
- java 图形用户界面
目录 Swing与AWT概述 Swing概述--组件显示 框架与窗体 创建框架对象 框架Frame类结构 框架对象的创建及常用方法 创建Swing窗体对象 Swing窗体JFrame 类结构 Swin ...
- Java 图形用户界面 复习题
题目: 编写一个包含主方法main的公共类(访问权限为public的类),该类继承自窗体类JFrame,并且,该类实现了接口ActionListener(实现接口ActionListener的方法ac ...
最新文章
- 19年NAACL纪实:自然语言处理的实用性见解 | CSDN博文精选
- python写网站 linux_linux下利用nginx部署python网站
- C语言编程序输出SCHAR_MAX的,运用堆栈把十进制变换成二进制
- android 短信 aapp,谈谈App的统一跳转和ARouter
- debian下tomcat8环境搭建
- SAP License:SAP 清帐函数示例
- 使用windows2008R2自带磁盘管理进行分区
- Vijos P1786 质因数分解【质因数分解】
- leetcode-896-单调数列
- Java读取配置文件yml中属性的方法
- 剑指offer刷题记录
- few-shot vid2vid部署安装及测试
- ORAN专题系列-21:主要的玩家(设备商)以及他们各自的态度、擅长领域
- Cesium 关于Bently模型的处理(dgn格式)
- 水下传感器网络研究——第5章 路由协议
- MyEclipse快捷键大全(转自豆瓣)
- TI毫米波FMCW雷达信号(距离、速度)的处理
- 使用Java 编写将阿拉伯数字转为中文数字的代码
- Python爬虫入门 | 4 爬取豆瓣TOP250图书信息
- NOTES常见问题及解决方法