首页 > 代码库 > Java中的图形界面编程
Java中的图形界面编程
前言
正文
Java中的图形界面编程
AWT/Swing
AWT(Abstract Window ToolKits,抽象窗口工具集)
1.容器类:用来存储组件,实现容器布局
2.组件类:实现界面的一些特定功能
一个容器可以包涵多个组件,组件必须存放在容器中
3.布局管理器:实现容器的布局设置
4.图形类:包括一些基本图形
Swing是AWT的一个轻量级框架
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
5.Swing中常用的容器
JFrame 窗口
JPanel 面板
JTabbedPane 标签面板
6.组件
1) JLabel 标签
2)JTextField 文本输入框
3)JPasswordField 密码框
4)JCheckBox 多选框
5)JRadiobutton 单选框
6)JButton 提交按钮
7)JComboBox 下拉框
8)JTable 表格
9)JScrollPane 可滚面板
TestJFrameDemo 窗口
TestJPanelDemo 面板
TestJTabbedPane 标签面板
TestJLabelDemo 标签
TestTextDemo 文本输入框
TestCheckBox 多选框
<script src="https://code.csdn.net/snippets/342449.js" type="text/javascript"></script>
TestJTableDemo 表格
<script src="https://code.csdn.net/snippets/342457.js" type="text/javascript"></script> TestUNEditeTable 不可编辑的表格
表格代码示例
流式布局 FlowLayout
默认Jpanel采用的流式布局,水平方向上排列,如果一行排不下就自动换行显示;当只有一个组将时,默认水平方向居中。
jp.setLayOut(new FlowLayout());;设置当前面板的布局
边界布局 BorderLayout
1.Container getContentPane()
Returns the contentPane object for this frame.
Container c=jf.getContenPane();//获得jf的默认面板
2.Container的api
1)void setLayout(LayoutManager mgr)
Sets the layout manager for this container.
c.setLayout(new BorderLayout);//边框布局将面板分成五部分
2)Component add(Component comp, int index)
Adds the specified component to this container at the given position.
c.add(new JButton("button1"),BorderLayout.NORTH);
1))The code for this applet is as follows:
--------------------------------------------------------------------------------
import java.awt.*;
import java.applet.Applet;
public class buttonDir extends Applet {
public void init() {
setLayout(new BorderLayout());
add(new Button("North"), BorderLayout.NORTH);
add(new Button("South"), BorderLayout.SOUTH);
add(new Button("East"), BorderLayout.EAST);
add(new Button("West"), BorderLayout.WEST);
add(new Button("Center"), BorderLayout.CENTER);
}
}
网格布局 GridLayout
1. For example, the following is an applet that lays out six buttons into three rows and two columns:
--------------------------------------------------------------------------------
import java.awt.*;
import java.applet.Applet;
public class ButtonGrid extends Applet {
public void init() {
setLayout(new GridLayout(3,2));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
}
}
2.GridLayout的api
GridLayout(int rows, int cols)
Creates a grid layout with the specified number of rows and columns.
GridLayout(int rows, int cols, int hgap, int vgap)
Creates a grid layout with the specified number of rows and columns. 第一个参数是行数,第二个参数是列数,第三个参数是行间距,第四个参数是列间距
箱式布局 Box
A lightweight container that uses a BoxLayout object as its layout manager。
1.BOX的api
1).static Box createHorizontalBox()
Creates a Box that displays its components from left to right. 水平方向排列
2)static Box createVerticalBox()
Creates a Box that displays its components from top to bottom 竖直方向排列
3)示例
Box b=Box createHorizontalBox() ;
b.add(new JButton("button1"))
jf.add(b);
事件
采用了观察者模式;事件有三要素是事件源、监听器和事件。
1.swing中的事件监听接口
1)java.awt.event
Interface MouseListener
The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component
2)java.awt.event
Interface KeyListener
The listener interface for receiving keyboard events (keystrokes).
3)java.awt.event
Interface ActionListener
The listener interface for receiving action events
适配器模式
final JLabel j1=new JLabel("测试");
JButton jb=new JButton("测试事件监听");
jb.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
j1.setText("鼠标按下");/j1必须用final修饰。
}
});