GUI编程—Swing
Swing
Swing是原来的AWT组件来自于java.awt包,在不同的开发平台上显示的时候会表现出不同的风格,但是Swing开发的程序可以同一组件的风格,不依赖操作系统。
Swing是完全由Java编写,是轻量级组件,不依赖操作系统。为解决 AWT 存在的问题而新开发的图形界面包。简单的说Swing是对AWT的改良和扩展。
Swing和AWT的区别和联系:
AWT和Swing的实现原理不同:
AWT的图形函数与操作系统提供的图形函数有着一一对应的关系。也就是说,当我们利用 AWT构件图形用户界面的时候,实际上是在利用操作系统的图形库。 不同的操作系统其图形库的功能可能不一样,在一个平台上存在的功能在另外一个平台上则可能不存在。为了实现Java语言所宣称的"一次编译,到处运行"的概念,AWT不得不通过牺牲功能来实现平台无关性。因此,AWT 的图形功能是各操作系统图形功能的“交集”。 因为AWT是依靠本地方法来实现功能的,所以AWT控件称为“重量级控件”。 而Swing ,不仅提供了AWT 的所有功能,还用纯粹的Java代码对AWT的功能进行了大幅度的扩充。 例如:并不是所有的操作系统都提供了对树形控件的支持, Swing则利用了AWT中所提供的基本作图方法模拟了一个树形控件。 由于 Swing是用纯粹的Java代码来实现的,因此Swing控件在各平台通用。 因为Swing不使用本地方法,故Swing控件称为“轻量级控件”。 AWT和Swing之间的区别: 1)AWT 是基于本地方法的C/C++程序,其运行速度比较快;Swing是基于AWT的Java程序,其运行速度比较慢。 2)AWT的控件在不同的平台可能表现不同,而Swing在所有平台表现一致。 在实际应用中,应该使用AWT还是Swing取决于应用程序所部署的平台类型。例如: 1)对于一个嵌入式应用,目标平台的硬件资源往往非常有限,而应用程序的运行速度又是项目中至关重要的因素。在这种矛盾的情况下,简单而高效的AWT当然成了嵌入式Java的第一选择。 2)在普通的基于PC或者是工作站的标准Java应用中,硬件资源对应用程序所造成的限制往往不是项目中的关键因素。所以在标准版的Java中则提倡使用Swing, 也就是通过牺牲速度来实现应用程序的功能。
在java中,AWT包的名称是java.awt
Swing包的名称是javax.swing
Swing组件按功能可分为如下几类:
1、顶层容器:JFrame, JApplet, JDialog和JWindow。
2、中间容器:JPanel, JScrollPane, JSplitPane, JTooIBar等。
3、特殊容器:在用户界面上具有特殊作用的中间容器,如JlnternalFrame、JRootPane、JLayeredPane和JDestopPane等。
4、基本组件:实现人机交互的组件,如Button、 JComboBox、Just, Menu、Mider等。
5、不可编辑信息的显示组件:向用户显示不可编辑信息的组件,如JLabel、JProgressBar和JTooITip等。
6、可编辑信息的显示组件:向用户显示能被编辑的格式化信息的组件,如JTable、JTextArea和JTextField等。
7、特殊对话框组件:可以直接产生特殊对话框的组件,如JColorChoosor和JFileChooser等。
Swing的4个顶层容器类直接继承了AWT组件,而不是从JComponent派生出来的,它们分别是:JFrame、JDialog、JApplet和JWindow。
顶层容器类并不是轻量级组件,而是重量级组件(需要部分委托给运行平台上GUI组件的对等体)。
顶层容器中:
1.JApplet可作为java小应用程序的窗体,但通常使用java.applet.Applet类来创建小应用程序。
2.JFrame集成自AWTFrame类,通常作为主窗体使用。
3.JDialog用于创建对话框的窗体。
4.JWindow与AWT中的Window相似,但几乎不用,因为没有太大的实用价值。
Swing组件的类名和对应AWT组件的类名基本一致,只要在原来的AWT组件类名前添加“J”即可,但有如下几个例外:
1、JComboBox:对应于AWT里的Choice组件,但比Choice组件功能更丰富。
2、JFileChooser:对位于AWT里的FileDialog组件。
3、JSrcoIIBar:对应AWT里的Scrollbar。注意两个组件类名中b字母的大小写差别。
4、JCheckBox:对应于AWT里的Checkbox。注意两个组件类名中b字母的大小写差别。
5、JCheckBoxMenuItem:对应于AWT里的CheckboxMenuItem,注意两个组件类名中b字母的大小写差别。
1.1、窗口、面板
package com.kuang.lesson04; import javax.swing.*; import java.awt.*; public class JFrameDemo { //init(); 初始化 public void init(){ //顶级窗口 JFrame jf = new JFrame("JFrame窗口"); jf.setVisible(true); jf.setBounds(100,100,200,200); jf.setBackground(Color.cyan); //设置文字 Jlabel JLabel label = new JLabel("好好学习,天天向上"); jf.add(label); //关闭事件 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { //建立一个窗口 new JFrameDemo().init(); } }
标签居中
public class JFrameDemo { public void init(){ JFrame jf = new JFrame("JFrame 窗口"); jf.setVisible(true);; jf.setBounds(100,100,200,300); jf.setBackground(Color.BLUE); JLabel label = new JLabel("好好学习,天天向上"); jf.add(label); label.setHorizontalAlignment(SwingConstants.CENTER); Container container = jf.getContentPane(); container.setBackground(Color.BLUE); } public static void main(String[] args) { new JFrameDemo().init(); } }
1.2、弹窗
JDialog,用来被弹出,默认就有关闭事件!
package com.java.demo.demo1; import javax.swing.*; ; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class DialogDemo extends JFrame { public DialogDemo() { this.setVisible(true); ; this.setBounds(100, 200, 300, 300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container container = this.getContentPane(); container.setLayout(null); JButton button = new JButton("点击弹出对话框"); button.setBounds(30,30,200,50); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new MyDialogDemo(); } }); container.add(button); } public static void main(String[] args) { new DialogDemo(); } } class MyDialogDemo extends JDialog { public MyDialogDemo() { this.setVisible(true); this.setBounds(100, 100, 500, 500); // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container container = this.getContentPane(); container.setBackground(Color.CYAN); JLabel label = new JLabel("好好学习java"); label.setHorizontalAlignment(SwingConstants.CENTER); container.add(label); } }
1.3、标签
label
new JLabel("xxx");
图标 ICON
package com.java.demo.demo1; import javax.swing.*; import java.awt.*; public class IconDemo extends JFrame implements Icon { private int width; private int height; public IconDemo() { } //无参构造 public IconDemo(int width, int height) { this.width = width; this.height = height; } public void init() { IconDemo iconDemo = new IconDemo(15, 15); JLabel label = new JLabel("icontext", iconDemo,SwingConstants.CENTER); Container container = getContentPane(); container.add(label); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new IconDemo().init(); } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillRect(x,y,width,height); } @Override public int getIconWidth() { return this.width; } @Override public int getIconHeight() { return this.height; } }
图片Icon
package com.java.demo.demo1; import javax.swing.*; import java.awt.*; import java.net.URL; public class ImageIconDemo extends JFrame { public ImageIconDemo(){ JLabel label = new JLabel("imageIcon"); URL url = ImageIconDemo.class.getResource("36.jpg"); ImageIcon imageIcon = new ImageIcon(url); label.setIcon(imageIcon); label.setHorizontalAlignment(SwingConstants.CENTER); Container container = getContentPane(); container.add(label); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,200,300,330); } public static void main(String[] args) { new ImageIconDemo(); } }
图片角标按钮
package com.kuang.lesson05; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo01 extends JFrame { public JButtonDemo01() { Container container = this.getContentPane(); //将一个图片变为图标 URL resource = JButtonDemo01.class.getResource("36.jpg"); Icon icon = new ImageIcon(resource); //把这个图标放在按钮上 JButton button = new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮"); //add container.add(button); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo01(); } }
1.4、面板
JPanel
package com.kuang.lesson05; import javax.swing.*; import java.awt.*; public class JPanelDemo extends JFrame { public JPanelDemo() { Container container = this.getContentPane(); container.setLayout(new GridLayout(2,1,10,10)); //后面的参数的意思,间距 JPanel panel1 = new JPanel(new GridLayout(1,3)); JPanel panel2 = new JPanel(new GridLayout(1,2)); JPanel panel3 = new JPanel(new GridLayout(2,1)); JPanel panel4 = new JPanel(new GridLayout(3,2)); panel1.add(new JButton("1")); panel1.add(new JButton("1")); panel1.add(new JButton("1")); panel2.add(new JButton("2")); panel2.add(new JButton("2")); panel3.add(new JButton("3")); panel3.add(new JButton("3")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); container.add(panel1); container.add(panel2); container.add(panel3); container.add(panel4); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JPanelDemo(); } }
JScrollPanel
package com.java.demo.demo1; import javax.swing.*; import java.awt.*; public class JpanlColl extends JFrame { public JpanlColl(){ //容器 Container container = getContentPane(); //文本域:设置行数和列数 JTextArea textArea = new JTextArea(20, 50); textArea.setText("好好学习");//设置文本内容 JScrollPane scrollPane = new JScrollPane(textArea);//将文本域放入滑动面板中 container.add(scrollPane);//将面板放入 this.setVisible(true); this.setBounds(100,223,400,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JpanlColl(); } }
1.5、按钮
图片按钮
package com.kuang.lesson05; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo01 extends JFrame { public JButtonDemo01() { Container container = this.getContentPane(); //将一个图片变为图标 URL resource = JButtonDemo01.class.getResource("36.jpg"); Icon icon = new ImageIcon(resource); //把这个图标放在按钮上 JButton button = new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮"); //add container.add(button); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo01(); } }
单选按钮
package com.kuang.lesson05; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo02 extends JFrame { public JButtonDemo02() { Container container = this.getContentPane(); //单选框 JRadioButton radioButton1 = new JRadioButton("JRadioButton01"); JRadioButton radioButton2 = new JRadioButton("JRadioButton02"); JRadioButton radioButton3 = new JRadioButton("JRadioButton03"); //由于单选框只能选择一个,分组,一个组中只能选择一个 ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); group.add(radioButton3); container.add(radioButton1,BorderLayout.CENTER); container.add(radioButton2,BorderLayout.NORTH); container.add(radioButton3,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo02(); } }
复选按钮
package com.kuang.lesson05; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo03 extends JFrame { public JButtonDemo03() { Container container = this.getContentPane(); //多选框 JCheckBox checkBox01 = new JCheckBox("checkBox01"); JCheckBox checkBox02 = new JCheckBox("checkBox02"); container.add(checkBox01,BorderLayout.NORTH); container.add(checkBox02,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo03(); } }
1.6、列表
- 下拉框
package com.java.demo.demo1; import javax.swing.*; import java.awt.*; public class ComboBoxDemo extends JFrame { public ComboBoxDemo(){ Container container = getContentPane(); JComboBox comboBox = new JComboBox(); comboBox.addItem(null); comboBox.addItem("优秀"); comboBox.addItem("良好"); comboBox.addItem("合格"); comboBox.addItem("不及格"); container.add(comboBox); setVisible(true); setBackground(Color.BLUE); pack(); } public static void main(String[] args) { new ComboBoxDemo(); } }
-
列表框
package com.kuang.lesson06; import javax.swing.*; import java.awt.*; import java.util.Vector; public class TestComboboxDemo02 extends JFrame { public TestComboboxDemo02() { Container container = this.getContentPane(); //生成列表的内容 //String[] contents = {"1","2","3"}; Vector contents = new Vector(); //列表中需要放入内容 JList jList = new JList(contents); contents.add("zhangsan"); contents.add("lisi"); contents.add("wangwu"); container.add(jList); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboboxDemo02(); } }
-
应用场景
选择地区,或者一些单个选项 - 列表,展示信息,一般是动态扩容!
1.7、文本框
-
文本框
package com.kuang.lesson06; import javax.swing.*; import java.awt.*; import java.util.Vector; public class TestTextDemo01 extends JFrame { public TestTextDemo01() { Container container = this.getContentPane(); JTextField textField = new JTextField("hello"); JTextField textField2 = new JTextField("world",20); container.add(textField,BorderLayout.NORTH); container.add(textField2,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo01(); } }
-
密码框
package com.kuang.lesson06; import javax.swing.*; import java.awt.*; public class TestTextDemo03 extends JFrame { public TestTextDemo03() { Container container = this.getContentPane(); //面板 JPasswordField passwordField = new JPasswordField(); //**** passwordField.setEchoChar('*'); container.add(passwordField); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo03(); } }
- 点赞
- 收藏
- 分享
- 文章举报
- Java开发GUI之绝对布局
- C++著名程序库的比较和学习经验(STL.Boost.GUI.XML.网络等等)
- pythonwin-win32gui 窗口查找和遍历
- python语言的GUI工具——pyqt
- 生成jar文件的源码工具(jd-gui)
- lfs support for git client gui -- sourcetree
- Rviz教程(十):Librviz: Incorporating RViz into a Custom GUI
- GUI系统之SurfaceFlinger(13)VSync信号的处理
- GUI数据传递01-全局变量法
- easygui安装的步骤
- 黑马程序员——15Java图形用户界面GUI
- 四则运算GUI版本功能展现
- 二.HighGUI图形用户界面初步(2.Trackbar)
- eclipse安装 JD-GUI插件的标准方法,亲测
- fedora 24 重新加载 pac 文件 (gui)
- 打字游戏和飞机大战线程GUI--第二十天
- GUI 中监听 文本框实时改变的实例
- JAVA(图形界面编程)GUI
- JavaSE_GUI编程
- 把KlayGE嵌入其他GUI框架