100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 用Java开发桌面应用程序(Swing开发GUI程序)

用Java开发桌面应用程序(Swing开发GUI程序)

时间:2020-01-29 09:29:47

相关推荐

用Java开发桌面应用程序(Swing开发GUI程序)

前面学过了面向对象基本概念、面向对象基本特征、Java基本包和API:异常、多线程、IO等。

基本概念

Swing:java中的一个包,负责开发GUI程序

GUI:图形用户界面,一般指可视化桌面系统中的应用程序

Windows:将应用程序从字符界面拓展到图形界面

常见图形用户界面的基本结构

使用的包:javax.swing包

界面(容器、窗口),提供整个界面的空间

例:在桌面出现一个界面,标题是:HelloWorld

显示:setVisible函数

Shows or hides this Window depending on the value of parameter b.

根据参数b的值显示或隐藏此窗口。

(1)框架界面: javax.swing.JFrame

import javax.swing.JFrame;class GUI1{public static void main(String[] args) throws Exception {JFrame jfm = new JFrame("HelloWorld");jfm.setSize(600,400);jfm.setLocation(300,200);jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );jfm.setVisible(true);}}

改进(1):

import javax.swing.JFrame;class GUI3{public static void main(String[] args) throws Exception {JFrame jfm = new JFrame("HelloWorld");jfm.setSize(600,400);jfm.setLocation(300,200);jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );jfm.setVisible(true);}}

改进(2):

import javax.swing.JFrame;class GUI4 extends JFrame{public GUI4(){super("HelloWorld");this.setSize(600,400);this.setLocation(300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );this.setVisible(true);}public static void main(String[] args) throws Exception {new GUI4();}}

(2)窗口界面: javax.swing.JWindow

import javax.swing.*;class GUI2{public static void main(String[] args) throws Exception {JWindow jwd = new JWindow();jwd.setSize(600,400);jwd.setLocation(300,200);jwd.setVisible(true);}}

(3)对话框界面: javax.swing.JDialog

一般掌握第(1)种即可

控件(界面上的组件,如按钮、文本框等)

注意:Java中,容器还有:面板 javax.swing.JPanel

一般我们在界面上添加一个面板,面板上增加控件

(1)按钮:javax.swing.JButton

(2)静态文本:javax.swing.JLabel

(3)文本框:javax.swing.JTextField

(4)密码框:javax.swing.JPasswordField

(5)多行文本框:javax.swing.JTextArea

(6)下拉菜单:javax.swing.JComboBox

(7)复选框:javax.swing.JCheckBox

实例:

import javax.swing.*;class GUI extends JFrame{private JLabel jlb = new JLabel("——————————欢迎来到社团学生注册系统——————————");private JButton jbt = new JButton("学生注册");private JLabel jlbAcc = new JLabel("请您输入账号");private JTextField jtfAcc = new JTextField(20);private JLabel jlbAdd = new JLabel("请您选择您的家乡");private JComboBox jcbAdd = new JComboBox();private JCheckBox jcbMember = new JCheckBox("是否立即成为会员",true);private JLabel jlbInfo = new JLabel("请您输入个人详细描述");private JTextArea jtaInfo = new JTextArea(20,30);private JPanel jpl = new JPanel();public GUI(){jpl.add(jlb);jpl.add(jbt);jpl.add(jlbAcc); jpl.add(jtfAcc);jpl.add(jlbAdd); jpl.add(jcbAdd); jpl.add(jlbInfo); jpl.add(jtaInfo);jcbAdd.addItem("湖南");jcbAdd.addItem("湖北");jcbAdd.addItem("河南");jcbAdd.addItem("河北");jpl.add(jcbMember);this.add(jpl);this.setSize(400,600);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );this.setVisible(true);}public static void main(String[] args) throws Exception {new GUI();}}

举一反三:1:知道一个控件,用哪个类?2、查文档看构造函数和其他重要函数;3、将控件实例化后加到容器中。

渲染(颜色、字体、图标)

任何界面和控件都可以设置背景颜色和前景颜色

setBackground(颜色) setForeground(颜色)

文档可查

凡是有字的控件,都有setFont函数(文档可查)

颜色:java.awt.Color

字体:java.awt.Font

图标:很多控件都可以设置图标,如按钮、静态文本等。但不是所有,比如文本框就不能。具体看文档。用的函数:setIcon函数

将按钮设置为图标形式

import javax.swing.*;import java.awt.*;class GUI6 extends JFrame{private JButton jbt = new JButton("学生注册");private JPanel jpl = new JPanel();public GUI6(){jpl.setBackground(new Color(0,255,255));jbt.setBackground(Color.blue);jbt.setForeground(Color.yellow);Font f = new Font("微软雅黑",Font.BOLD,50);jbt.setFont(f);jpl.add(jbt);this.add(jpl);this.setSize(400,600);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );this.setVisible(true);}public static void main(String[] args) throws Exception {new GUI6();}}

import javax.swing.*;import java.awt.*;class GUI7 extends JFrame{private JButton jbt = new JButton();private JPanel jpl = new JPanel();public GUI7(){ImageIcon icon = new ImageIcon("img.jpg");jbt.setIcon(icon);jbt.setText("这是一个按钮");jpl.add(jbt);this.add(jpl);this.setSize(600,800);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );this.setVisible(true);}public static void main(String[] args) throws Exception {new GUI7();}}

总结

javax.swing:界面(JFrame等),控件(按钮、文本框、复选框等)

java.awt:渲染(颜色:Color、字体:Font)

图标:Icon(注意,Icon和ImageIcon在Swing中)

控件.setBackground和setForeground(颜色)

控件.setFont(字体)

控件.setIcon(图标)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。