资源描述
图形界面编程技术手册
编写:Kende_zhu
第一节 基本容器JFrame
1、第一个JFrame窗体程序
public class JFrameDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("这是我的第一个swing窗体");
Dimension d = new Dimension(300, 200);
jf.setSize设置窗体大小
(d);
Point p = new Point(500, 400);
jf.setLocation设置窗体位置
(p);
jf.setVisible设置窗体可见
(true);
}
}
第二节 标签组件JLabel
1、定义标签对象
public class JLabelDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("这是我的第一个swing窗体");
JLabel lab = new JLabel("KENDE", JLabel.CENTER);//实例化标签对象
jf.add(lab);将Label组件添加到JFrame面板中
//将Label组件添加到JFrame面板中
Dimension d = new Dimension(300, 200);
jf.setSize(d);
Point p = new Point(500, 400);设置坐标位置
jf.setLocation(p);
jf.setVisible(true);
}
}
2、定义标签字体
标签可以设置字体,包括什么字体,字体的颜色,大小,是否斜体等
public class JLabelDemo2 {
public static void main(String[] args) {
JFrame jf = new JFrame("这是我的第一个swing窗体");
JLabel lab = new JLabel("KENDE", JLabel.CENTER);//实例化标签对象
Font font = new Font("Dialog", Font.ITALIC + Font.BOLD, 30);
lab.setFont(font);定义标签字体
jf.add(lab);//将组件添加到面板中
Dimension d = new Dimension(300, 200);
jf.setSize(d);
Point p = new Point(500, 400);
jf.setLocation(p);
jf.setVisible(true);
}
}
3、得到本机中所有的字体
public class GetAllFonts {
public static void main(String[] args) {
GraphicsEnvironment g = GraphicsEnvironment
.getLocalGraphicsEnvironment();
String fonts[] = g.getAvailableFontFamilyNames();
for (String name: fonts) {
System.out.println(name);
}
}
}
4、在JLabel中设置显示的图片
public class JLabelDemo3 {
public static void main(String[] args) {
JFrame jf = new JFrame("这是我的第一个swing窗体");
Icon icon = new ImageIcon("E:\\picBackGroud\\野生动物王国.jpg");
JLabel lab = new JLabel("KENDE", iconLabel中加入图片
, JLabel.CENTER);// 实例化标签对象
Font font = new Font("Serif", Font.ITALIC + Font.BOLD, 30);
lab.setFont(font);
jf.add(lab)将标签组件添加到JFrame面板中
;// 将组件添加到面板中
Dimension d = new Dimension(300, 200);
jf.setSize(d);
Point p = new Point(500, 400);
jf.setLocation(p);
jf.setVisible(true);
}
}
第三节 按钮组件JButton
1、在JFrame中增加按钮
public class JButtonDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to Kende's home!");
JButton bt = new JButton("按钮");定义JButton实例
frame.add(bt);往JFrame中添加按钮
frame.setSize(300,200);
frame.setLocation(400, 300);
frame.setVisible(true);
}
}
2、往JButton中添加图片
public class JButtonDemo02 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to Kende's home!");
String picPath = "E:\\picBackGroud\\a.jpg";
Icon icon = new ImageIcon(picPath);
JButton bt = new JButton(icon);往JButton中设置图片
frame.add(bt);
frame.setSize(800,500);
frame.setLocation(400, 300);
frame.setVisible(true);
}
}
第三节 菜单栏组件
public class IsMenuItem extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JMenuItem miBlue = new JMenuItem("Blue");
JMenuItem miGreen = new JMenuItem("Green");
JMenuItem miRed = new JMenuItem("Red");
JTextField tfMain = new JTextField(20);
public IsMenuItem() {
this.setSize(600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menuColor = new JMenu("Color");
menuBar.add(menuColor);
menuColor.add(miBlue);
menuColor.add(miGreen);
menuColor.add(miRed);
this.getContentPane().add(menuBar, BorderLayout.NORTH);
JPanel panel = new JPanel();
this.getContentPane().add(panel, BorderLayout.CENTER);
panel.add(tfMain);
tfMain.setBorder(BorderFactory.createLoweredBevelBorder());
miBlue.addActionListener(this);
miGreen.addActionListener(this);
miRed.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == miBlue) {
tfMain.setBackground(Color.BLUE);
} else if (e.getSource() == miGreen) {
tfMain.setBackground(Color.GREEN);
} else if (e.getSource() == miRed) {
tfMain.setBackground(Color.RED);
}
}
public static void main(String[] args) {
new IsMenuItem();
}
}
第四节 布局管理器
1、FlowLayout布局管理器
FlowLayout属于流式布局管理器,使用此种布局方式,所有的组件会像流水一样依次排列。
public class FlowLayoutDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
frame.setLayout设置JFrame容器的流式布局管理器
(new FlowLayout(FlowLayout.CENTER, 20组件的水平间距
, 20组件的垂直间距
));
JButton button = null;
for(int i = 0; i < 9; i ++){
button = new JButton("按钮-" + i);
frame.add(button);
}
frame.setSize(800,500);
frame.setLocation(400, 300);
frame.setVisible(true);
}
}
效果图:
2、BorderLayout布局管理器
BorderLayout将一个窗体的版面划成东,西,南,北,中五个区域,可以直接将需要的组件放到五个区域即可
public class BorderLayoutDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
frame.setLayout设置JFrame容器的Border布局管理器(东西南北中)
(new BorderLayout(20, 20));
frame.add(new JButton("东"), BorderLayout.EAST将按钮组件放在BorderLayout的东部
);
frame.add(new JButton("西"), BorderLayout.WEST);
frame.add(new JButton("南"), BorderLayout.SOUTH);
frame.add(new JButton("北"), BorderLayout.NORTH);
frame.add(new JButton("中"), BorderLayout.CENTER);
frame.setSize(500,300);
frame.setLocation(400, 300);
frame.setVisible(true);
}
}
3、GridLayout布局管理器
GridLayout布局管理器是以表格的形式进行管理的,在使用此布局管理器的时候必须设置显示的行数和列数。
public class GridLayoutDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
frame.setLayout(new GridLayout(3, 5表示3行5列
, 3, 3水平间距和垂直间距
))添加表格形式的布局管理器
;
JButton button = null;
for(int i = 0; i < 13; i ++){
button = new JButton("按钮-" + i);
frame.add(button);
}
frame.setSize(500,300);
frame.setLocation(400, 300);
frame.setVisible(true);
}
}
效果图:
4、CardLayout布局管理器
CardLayout就是将一组组件彼此重叠的进行布局,就像一张张卡片一样,这样每次只会展现一个界面
public class CardLayoutDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
CardLayout card = new CardLayout();
frame.setLayout(card);将JFrame容器设置为CardLayout布局方式
Container con = frame.getContentPane()得到JFrame窗体的contentPane对象
;
con.add将指定的组件添加到此JFrame容器的尾部
(new JLabel("标签-A", JLabel.CENTER), "first");
con.add(new JLabel("标签-B", JLabel.CENTER), "second");
con.add(new JLabel("标签-C", JLabel.CENTER), "third");
con.add(new JLabel("标签-D", JLabel.CENTER), "fourth");
con.add(new JLabel("标签-E", JLabel.CENTER), "fifth");
frame.pack()自动调整窗体的大小
;
frame.setVisible(true);
card.show(con, "third");翻转到具有指定name的组件:比如翻转到指定name为third的组件,这个时候对应的组件为标签 – C
for (int i = 0; i < 5; i++) {
card.next(con);翻转到指定容器比如con的下一张卡片,比如现在当前卡片为third,则此时显示fourth卡片
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
5、绝对定位
以上的布局管理器都是依靠专门的工具完成,在java中也可以通过绝对定位的方式完成
public class AbsoluteLayoutDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
frame.setLayout(null);对容器不使用任何的布局方式
JLabel title = new JLabel("");
JButton enter = new JButton("进入");
JButton help = new JButton("帮助");
frame.setSize(500, 400);
frame.setLocation(300, 200);
title.setBounds(45, 5, 150, 20);
enter.setBounds(280, 30, 100, 80);
help.setBounds(100, 50, 200, 100);
分别设置3个组件的位置,坐标X,Y以及宽度和高度
frame.add(title);
frame.add(enter);
frame.add(help);
将这3个组件添加到JFrame容器中
frame.setVisible(true);
}
}
第五节 其他容器
1、Jpanel
最早是在JFrame中加入组件,但是现在可以将组件加入到Jpanel中,在Jpanel中完成各个组件的排列,之后再将所有独立的Jpanel直接放在JFrame之中,以完成复杂的图形显示。
public class JPanelDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
JPanel panel = new JPanel();
panel.add(new JLabel("标签-1"));
panel.add(new JLabel("标签-2"));
panel.add(new JLabel("标签-3"));
panel.add(new JButton("按钮-X"));
panel.add(new JButton("按钮-Y"));
panel.add(new JButton("按钮-Z"));
往Jpanel面板中加入多个组件
frame.add(panel);将面板Jpanel(小容器)加入到JFrame容器中
frame.pack();自动调整窗体大小
frame.setLocation(300, 200);设置窗体的位置
frame.setVisible(true);
}
}
在开发中可以使用Jpanel完成一些复杂的界面设计。
2、JSplitPane
JSplitPane的主要作用是分割面板,可以将一个窗体分为两个子窗体,可以是水平排列也可以是垂直排列
public class JSplitPaneDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
Container cont = frame.getContentPane();
JSplitPane lfsplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT水平分割
,
new JLabel("标签-左"), new JLabel("标签-右"));
JSplitPane tpsplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT垂直分割
, lfsplit,
new JLabel("标签-下"));
lfsplit.setDividerSize(30);设置左右分割条的分割线大小
tpsplit.setDividerSize(20);设置垂直分割的分割线大小
tpsplit.setOneTouchExpandable在JSplitPane分隔条上提供一个UI小部件来快速展开/折叠分隔条
(true);
//frame.add(tpsplit);
cont.add(tpsplit);将垂直分割的JSplitPane面板添加到JFrame容器中
frame.pack();
frame.setLocation(300, 200);
frame.setVisible(true);
}
}
3、JTabbedPane
public class JTabbedPaneDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
Container cont = frame.getContentPane();
JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP设置标签在顶部显示
);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.add(new JButton("按钮"));
panel2.add(new JLabel("标签"));
tab.addTab("图片选项", new ImageIcon("d:\\oracle.jpg"), panel1, "图片");将按钮设置为图片选项卡
tab.addTab("文字选项", panel2);将标签设置为文字选项卡
cont.add(tab);将JTabbedPane面板添加到JFrame容器中
frame.pack();
frame.setLocation(300, 200);
frame.setVisible(true);
}
}
4、JScrollPane
在一般的图形界面中如果显示的区域不够大,往往会出现滚动条以方便用户浏览,在Swing中JScrollPane的主要功能就是为显示的内容加入水平滚动条。
JScrollPane主要由JViewPort和JScrollBar两部分组成,前者主要是显示一个矩形的区域让用户浏览,而后者主要是形成水平或者垂直的滚动条。
public class JScrollPaneDemo01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to kende's home");
Container cont = frame.getContentPane();
String picPath = "d:" + File.separator + "oracle.jpg";
JLabel lab = new JLabel(new ImageIcon(picPath));往标签中放入图片
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
定义一个带有垂直滚动条和水平滚动条的面板
panel.add(lab);将标签添加到面板中
cont.add(scrollPane);将滚动条面板添加到JFrame容器中
frame.pack();
frame.setLocation(300, 200);
frame.setVisible(true);
}
}
第六节 事件处理
Java事件处理流程:
1、关闭窗口
在此之前的事件是无效的,窗体关闭也是无效的,如果现在要变得有效,必须加入事件处理机制
class MyWindowEventHandle implements WindowListener{
public void windowActivated(WindowEvent e){
System.out.println("windowActivated --> 窗口被选中") ;
}
public void windowClosed(WindowEvent e){
System.out.println("windowClosed --> 窗口被关闭") ;
}
public void windowClosing(WindowEvent e){
System.out.println("windowClosing --> 窗口关闭") ;
System.exit(1) ;
}
public void windowDeactivated(WindowEvent e){
System.out.println("windowDeactivated --> 取消窗口选中") ;
}
public void windowDeiconified(WindowEvent e){
System.out.println("windowDeiconified --> 窗口从最小化恢复") ;
}
public void windowIconified(WindowEvent e){
System.out.println("windowIconified --> 窗口最小化") ;
}
public void windowOpened(WindowEvent e){
System.out.println("windowOpened --> 窗口被打开") ;
}
};
public class MyEventWindowEventJFrame01{
public static void main(String args[]){
JFrame frame = new JFrame("Welcome To kende's home") ;
frame.addWindowListener(new MyWindowEventHandle()) ; // 加入事件
frame.setSize(300,150) ;
frame.setBackground(Color.WHITE) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
}
注意:发现以上程序虽然实现了窗口关闭,但是没有必要实现那么多的方法,可以如何做呢,其实在事件中提供了很多的适配器
class MyWindowEventHandle2 extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.out.println("windowClosing --> 窗口关闭") ;
System.exit(1) ;
}
};
public class MyEventWindowEventJFrame02{
public static void main(String args[]){
JFrame frame = new JFrame("Welcome To kende’s home") ;
frame.addWindowListener(new MyWindowEventHandle2()) ; // 加入事件
frame.setSize(300,150) ;
frame.setBackground(Color.WHITE) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
}
现在只要求一个关闭,资源太浪费了,可以使用匿名内部类的做法减少代码量
public class MyEventWindowEventJFrame03{
public static void main(String args[]){
JFrame frame = new JFrame("Welcome To kende’s home") ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.out.println("windowClosing --> 窗口关闭") ;
System.exit(1) ;
}
}) ; // 加入事件
frame.setSize(300,150) ;
frame.setBackground(Color.WHITE) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
}
2、按钮事件
如果要使得按钮有效,那么只需要使用ActionListener接口
class ActionHandle{
private JFrame frame = new JFrame("Welcome To kende's home") ;
private JButton but = new JButton("显示");
private JLabel lab = new JLabel() ;
private JTextField text = new JTextField(10) ;
private JPanel pan = new JPanel() ;
public ActionHandle(){
Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,28) ;
lab.setFont(fnt) ; // 设置标签的显示文字
lab.setText("等待用户输入信息!") ;
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// if(e.getSource() instanceof JButton){} // 判断是否是按钮
if(e.getSource()==but){
lab.setText(text.getText()) ;
}
}
}) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}) ; // 加入事件
frame.setLayout(new GridLayout(2,1)) ;
pan.setLayout(new GridLayout(1,2)) ;
pan.add(text);
pan.add(but) ;
frame.add(pan) ;
frame.add(lab) ;
frame.pack() ;
frame.setBackground(Color.WHITE) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
};
public class MyActionEventDemo01{
public static void main(String args[]){
new ActionHandle() ;
}
}
完成一个简单的用户登录,用户名是kende,密码是123
class LoginCheck{
private String name ;
private String password ;
public LoginCheck(String name,String password){
this.name = name ;
this.password = password ;
}
public boolean validate(){
if("kende".equals(name)&&"123".equals(password)){
return true ;
}else{
return false ;
}
}
};
class ActionHandle2{
private JFrame frame = new JFrame("Welcom
展开阅读全文