GUI
Posted 薰衣草
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GUI相关的知识,希望对你有一定的参考价值。
容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排序
Panel默认的布局管理器
BorderLayout(边界布局管理器)
东、南、西、北、中
Frame默认的布局管理器
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵
事件监听机制的特点:
1。事件源
2.事件
3.监听器
4.事件处理
事件源:就是awt包或swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以出发某一个事件的动作(不止一个动作)都已经封装到了监听器中
以上三者在Java中都已经定义好了。直接获取其对象来用就好了
我们要做的事情是对产生的动作进行处理。
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class InputWorkerInf
public static void main(String[] args)
Frame f = new Frame("my awt");
f.setSize(500,400);
f.setLocation(300,200);
f.setLayout(new FlowLayout());
Button button = new Button("我是一个按钮");
f.add(button);
//窗体事件
//f.addWindowListener(new MyWindowListener());
//或者定义一个匿名内部类
f.addWindowListener(new MyWindowListener()
public void windowClosing(WindowEvent e)
System.exit(0);
);
f.setVisible(true);
//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口
//并覆盖了其中的方法,所以只要继承至WindowAdapter覆盖需要的方法即可
class MyWindowListener extends WindowAdapter
public void windowClosing(WindowEvent e)
//System.out.println("window closing");
System.exit(0);
public void windowActivated(WindowEvent e)
System.out.println("窗体前置");
public void windowOpened(WindowEvent e)
System.out.println("窗体打开");
窗体设置
创建图形化界面:
1.创建Frame窗体
2.对窗体进行基本设置
比如大小、位置、布局
3.定义组件
4.将组件通过窗体的add方法添加到窗体中
5.让窗体显示,通过setVisible(true)
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
public class AwtDemo
public static void main(String[] args)
Frame frame = new Frame("my awt");
frame.setSize(500,400);//设置窗体大小
frame.setLocation(300,200);//设置位置
frame.setLayout(new FlowLayout());//设置流式布局
Button button = new Button("我是一按钮");//添加按钮组件
frame.add(button);
frame.setVisible(true);
System.out.println("Hello");
//action事件
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FrameDemo
private Frame f;
private Button button;
public FrameDemo()
init();
public void init()
f = new Frame("my frame");
//对frame进行基本设置
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
button = new Button("my button");
//将组件添加到frame中
f.add(button);
//加载一下窗体上的事件
myEvent();
//显示窗体
f.setVisible(true);
private void myEvent()
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
);
//给窗体添加按钮,让按钮具备退出程序的功能。
/*
按钮就是事件源。
选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。
需要查看该组件对象的功能。
通过查阅button的描述,发现按钮支持一个特有的监听addActionListener
*/
button.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
System.out.println("按钮执行退出");
System.exit(0);
);
public static void main(String[] args)
new FrameDemo();
//鼠标 键盘
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import org.omg.CORBA.PUBLIC_MEMBER;
public class MouseAndKeyEvent
private Frame f;
private Button button;
private TextField tf;
public MouseAndKeyEvent()
init();
public void init()
f = new Frame("my frame");
//对frame进行基本设置
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
tf = new TextField(20);
button = new Button("my button");
f.add(tf);
//将组件添加到frame中
f.add(button);
//加载一下窗体上的事件
myEvent();
//显示窗体
f.setVisible(true);
private void myEvent()
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
);
tf.addKeyListener(new KeyAdapter()
public void keyPressed(KeyEvent e)
int code = e.getKeyCode();
if(!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9))
System.out.println(code + "...是非法的");
e.consume(); //不按默认方式处理
);
//给button添加一个键盘监听
button.addKeyListener(new KeyAdapter()
public void keyPressed(KeyEvent e)
//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());
//点击ESC时退出
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
//组合键 例如ctrl+Enter
if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER)
System.out.println("ctrl+enter is run");
//System.exit(0);
);
/*
//给button添加一个鼠标监听
button.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
System.out.println("action ok");
);
//鼠标进入事件
button.addMouseListener(new MouseAdapter()
private int count = 1;
private int clickCount = 1;
public void mouseEntered(MouseEvent e)
//处理方式
System.out.println("鼠标进入到该组件"+count++);
public void mouseClicked(MouseEvent e)
if(e.getClickCount() == 2)
System.out.println("双击动作"+clickCount++);
);
*/
public static void main(String[] args)
new MouseAndKeyEvent();
//菜单
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MyMenuDemo
private Frame frame;
private MenuBar menuBar;
private Menu menu,subMenu;
private MenuItem closeItem,subItem;
MyMenuDemo()
init();
public void init()
frame = new Frame("my window");
frame.setBounds(300, 100, 500, 600);
frame.setLayout(new FlowLayout());
menuBar = new MenuBar();
menu = new Menu("文件");
subMenu = new Menu("子菜单");
closeItem = new MenuItem("退出");
subItem = new MenuItem("子条目");
subMenu.add(subItem);
menu.add(subMenu);
menu.add(closeItem);
menuBar.add(menu);
frame.setMenuBar(menuBar);
myEvent();
frame.setVisible(true);
private void myEvent()
//点“退出”可以退出,closeItem成为事件源
closeItem.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
System.exit(0);
);
frame.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
);
public static void main(String[] args)
new MyMenuDemo();
//练习
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class MyWindowDemo
private Frame frame;
private TextField tfField;
private Button button;
private TextArea taArea;
//添加对话框
private Dialog dialog;
private Label label;
private Button okButton;
public MyWindowDemo()
init();
public void init()
frame = new Frame("my window");
frame.setBounds(300, 100, 600, 500);
frame.setLayout(new FlowLayout());
tfField = new TextField(60);
button = new Button("转到");
taArea = new TextArea(25,70);
dialog = new Dialog(frame,"提示信息——self",true);
dialog.setBounds(400, 200, 240, 150);
dialog.setLayout(new FlowLayout());
label = new Label();
okButton = new Button("确定");
dialog.add(label);
dialog.add(okButton);
frame.add(tfField);
frame.add(button);
frame.add(taArea);
myEvent();
frame.setVisible(true);
private void myEvent()
dialog.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
dialog.setVisible(false);
);
okButton.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
dialog.setVisible(false);
);
//按回车键直接转到所在目录或蹦出错误提示框
tfField.addKeyListener(new KeyAdapter()
public void keyPressed(KeyEvent e)
if(e.getKeyCode() == KeyEvent.VK_ENTER)
showDir();
);
button.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
String dirPath = tfField.getText();
File dirFile = new File(dirPath);
if(dirFile.exists() && dirFile.isDirectory())
taArea.setText("");//每次都清空文本区
String[] names = dirFile.list();
for(String name : names)
taArea.append(name+"\\r\\n");
else
String info = "你输入的信息: "+dirPath+"是错误的,请重新输入!";
label.setText(info);
dialog.setVisible(true);
tfField.setText("");
);
frame.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
);
private void showDir()
String dirPath = tfField.getText();
File dirFile = new File(dirPath);
if(dirFile.exists() && dirFile.isDirectory())
taArea.setText("");//每次都清空文本区
String[] names = dirFile.list();
for(String name : names)
taArea.append(name+"\\r\\n");
else
String info = "你输入的信息: "+dirPath+"是错误的,请重新输入!";
label.setText(info);
dialog.setVisible(true);
public static void main(String[] args)
new MyWindowDemo();
以上是关于GUI的主要内容,如果未能解决你的问题,请参考以下文章