Java GUI编程(7)- Swing总结概述
2023-02-18 16:23:24 时间
awt使用的是系统自带的组件,swing是Java自己画出来的组件。awt依赖系统,swing可跨平台,并且比awt更美观
窗口
窗口常见四种关闭方式
- EXIT_ON_CLOSE :退出
- HIDE_ON_CLOSE :隐藏窗口
- DO_NOTHING_ON_CLOSE :关闭的窗口的时候不做任何操作
- DISPOSE_ON_CLOSE :最后一个窗口被关闭执行关闭操作
public class JFrameDemo { //init();初始化 public void init(){ JFrame jFrame = new JFrame("这是一个JFrame窗口"); jFrame.setVisible(true); jFrame.setBounds(1,1,200,200); //设置文字 JLabel jLabel = new JLabel("HelloWorld"); jFrame.add(jLabel); //关闭事件 jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { //建立一个窗口 new JFrameDemo().init(); } }
标签居中,添加颜色,获取容器
public class JFrameDemo02 { public static void main(String[] args) { new MyJframe().init(); } } class MyJframe extends JFrame{ public void init(){ this.setTitle("这是JFrame窗口"); this.setVisible(true); this.setBounds(1,1,200,200); JLabel jLabel = new JLabel("HelloWorld"); this.add(jLabel); //让文本标签居中 jLabel.setHorizontalAlignment(SwingConstants.CENTER); //获得一个容器 Container contentPane = this.getContentPane(); //设置颜色,颜色只有在容器上才会生效 contentPane.setBackground(Color.yellow); //关闭窗口 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
JDialog弹窗
1 //主窗口 2 public class DialogDemo extends JFrame { 3 public DialogDemo(){ 4 this.setVisible(true); 5 this.setSize(500,700); 6 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 7 8 //获得一个容器,放东西 9 Container contentPane = this.getContentPane(); 10 //绝对布局 11 contentPane.setLayout(null); 12 13 //按钮 14 JButton jButton = new JButton("危险"); 15 jButton.setSize(100,200); 16 17 //点击按钮的时候,弹出一个弹窗 18 jButton.addActionListener(new ActionListener() { 19 @Override 20 public void actionPerformed(ActionEvent e) { 21 new MyDialogDemo(); 22 } 23 }); 24 contentPane.add(jButton); 25 } 26 27 public static void main(String[] args) { 28 new DialogDemo(); 29 } 30 } 31 32 //弹窗的窗口 33 class MyDialogDemo extends JDialog{ 34 public MyDialogDemo(){ 35 this.setVisible(true); 36 this.setBounds(100,100,500,500); 37 // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);自带关闭事件,不需要添加 38 39 Container contentPane = this.getContentPane(); 40 contentPane.setLayout(null); 41 42 JLabel jLabel = new JLabel("好好学习,别开小差"); 43 jLabel.setSize(200,50); 44 contentPane.add(jLabel); 45 } 46 }
Icon、ImageIcon标签
1 //图标标签,需要实现类,Frame继承 2 public class IconDemo extends JFrame implements Icon { 3 private int width; 4 private int height; 5 6 public static void main(String[] args) { 7 new IconDemo().init(); 8 } 9 10 //无参构造 11 public IconDemo(){} 12 13 //有参构造 14 public IconDemo(int width,int height){ 15 this.width=width; 16 this.height=height; 17 } 18 19 //初始化代码 20 public void init(){ 21 IconDemo iconDemo = new IconDemo(15,15); 22 //图标放在标签,也可以放在按钮上 23 JLabel jLabel = new JLabel("icontest",iconDemo,SwingConstants.CENTER); 24 Container container = this.getContentPane(); 25 container.add(jLabel); 26 this.setVisible(true); 27 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 28 } 29 30 @Override 31 public void paintIcon(Component c, Graphics g, int x, int y) { 32 g.fillOval(x,y,width,height); 33 } 34 35 @Override 36 public int getIconWidth() { 37 return this.width; 38 } 39 40 @Override 41 public int getIconHeight() { 42 return this.height; 43 } 44 }
1 //图片标签 2 public class ImageIconDemo extends JFrame { 3 public static void main(String[] args) { 4 new ImageIconDemo(); 5 } 6 7 public ImageIconDemo(){ 8 //获取图片地址 9 URL resource = ImageIconDemo.class.getResource("1.png"); 10 JLabel jLabel = new JLabel("ImageIcon"); 11 12 ImageIcon imageIcon = new ImageIcon(resource); 13 jLabel.setIcon(imageIcon); 14 jLabel.setHorizontalAlignment(SwingConstants.CENTER); 15 16 Container container = this.getContentPane(); 17 container.add(jLabel); 18 19 setVisible(true); 20 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 21 setBounds(100,100,300,300); 22 } 23 }
文本域JScroll面板
1 public class JPanelDemo extends JFrame { 2 public static void main(String[] args) { 3 new JPanelDemo(); 4 } 5 6 public JPanelDemo(){ 7 Container container = this.getContentPane(); 8 container.setLayout(new GridLayout(2,1,10,10));//最后面的两个参数是设置间距的 9 10 JPanel jPanel = new JPanel(new GridLayout(1,3)); 11 JPanel jPane2 = new JPanel(new GridLayout(1,2)); 12 JPanel jPane3 = new JPanel(new GridLayout(2,1)); 13 JPanel jPane4 = new JPanel(new GridLayout(3,2)); 14 15 jPanel.add(new JButton("1")); 16 jPanel.add(new JButton("1")); 17 jPanel.add(new JButton("1")); 18 jPanel.add(new JButton("1")); 19 jPane2.add(new JButton("2")); 20 jPane2.add(new JButton("2")); 21 jPane3.add(new JButton("3")); 22 jPane3.add(new JButton("3")); 23 jPane4.add(new JButton("4")); 24 jPane4.add(new JButton("4")); 25 jPane4.add(new JButton("4")); 26 jPane4.add(new JButton("4")); 27 jPane4.add(new JButton("4")); 28 jPane4.add(new JButton("4")); 29 30 container.add(jPanel); 31 container.add(jPane2); 32 container.add(jPane3); 33 container.add(jPane4); 34 35 this.setVisible(true); 36 this.setSize(500,500); 37 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 38 } 39 }
1 public class JScrollDemo extends JFrame { 2 public static void main(String[] args) { 3 JScrollDemo jScrollDemo = new JScrollDemo(); 4 } 5 6 public JScrollDemo(){ 7 Container container = this.getContentPane(); 8 9 //文本域 10 JTextArea jTextArea = new JTextArea(20,50); 11 jTextArea.setText("HelloWorld"); 12 13 //SCroll面板 14 JScrollPane jScrollPane = new JScrollPane(jTextArea); 15 container.add(jScrollPane); 16 17 18 this.setVisible(true); 19 this.setSize(500,500); 20 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 21 } 22 }
图片按钮、单选框、多选框
1 //图片按钮 2 public class JButton01 extends JFrame{ 3 public static void main(String[] args) { 4 new JButton01(); 5 } 6 public JButton01(){ 7 Container container = this.getContentPane(); 8 //将一个图片变为图标 9 URL url = JButton01.class.getResource("3.png"); 10 Icon icon = new ImageIcon(url); 11 12 //将这个图标放在按钮上 13 JButton jButton = new JButton(); 14 jButton.setIcon(icon); 15 jButton.setToolTipText("图片按钮"); 16 17 //add 18 container.add(jButton); 19 20 this.setVisible(true); 21 this.setSize(100,100); 22 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 23 24 } 25 }
1 //单选框 2 public class JButtonDemo02 extends JFrame { 3 public static void main(String[] args) { 4 new JButtonDemo02(); 5 } 6 7 public JButtonDemo02(){ 8 Container container = this.getContentPane(); 9 //将一个图片变为图标 10 URL url = JButton01.class.getResource("3.png"); 11 Icon icon = new ImageIcon(url); 12 13 //单选框 14 JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1"); 15 JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2"); 16 JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3"); 17 18 //由于单选框只能选择一个,所以需要进行分组,在一个组中只能选择一个 19 ButtonGroup group = new ButtonGroup(); 20 group.add(jRadioButton1); 21 group.add(jRadioButton2); 22 group.add(jRadioButton3); 23 24 container.add(jRadioButton1,BorderLayout.CENTER); 25 container.add(jRadioButton2,BorderLayout.NORTH); 26 container.add(jRadioButton3,BorderLayout.SOUTH); 27 28 this.setVisible(true); 29 this.setSize(300,500); 30 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 31 } 32 }
1 //多选框 2 public class JButtonDemo03 extends JFrame{ 3 public static void main(String[] args) { 4 new JButtonDemo03(); 5 } 6 7 public JButtonDemo03(){ 8 Container container = this.getContentPane(); 9 //将一个图片变为图标 10 URL url = JButton01.class.getResource("3.png"); 11 Icon icon = new ImageIcon(url); 12 13 //多选框 14 JCheckBox jCheckBox1 = new JCheckBox("jCheckBox01"); 15 JCheckBox jCheckBox2 = new JCheckBox("jCheckBox02"); 16 17 container.add(jCheckBox1,BorderLayout.NORTH); 18 container.add(jCheckBox2,BorderLayout.SOUTH); 19 20 this.setVisible(true); 21 this.setSize(300,500); 22 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 23 } 24 }
下拉框,列表框
1 //下拉框 2 public class TestCombobxDemo01 extends JFrame { 3 public TestCombobxDemo01(){ 4 Container container = this.getContentPane(); 5 6 JComboBox status = new JComboBox(); 7 status.addItem(null); 8 status.addItem("正在上映"); 9 status.addItem("已下架"); 10 status.addItem("即将上映"); 11 12 container.add(status); 13 14 this.setVisible(true); 15 this.setSize(500,500); 16 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 17 } 18 19 public static void main(String[] args) { 20 new TestCombobxDemo01(); 21 } 22 }
1 //列表框 2 public class TestCombobxDemo02 extends JFrame { 3 public TestCombobxDemo02(){ 4 Container container = this.getContentPane(); 5 6 //生成列表的内容 7 String[] contents = {"1","2","3"}; 8 //列表中需要放入内容 9 JList jList = new JList(contents); 10 11 container.add(jList); 12 13 this.setVisible(true); 14 this.setSize(500,500); 15 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 16 } 17 18 public static void main(String[] args) { 19 new TestCombobxDemo02(); 20 } 21 }
文本框、密码框、文本域
1 //文本框 2 public class TestTextDeno01 extends JFrame { 3 public TestTextDeno01(){ 4 Container container = this.getContentPane(); 5 6 JTextField jTextField1 = new JTextField("Hello",20); 7 JTextField jTextField2 = new JTextField("World"); 8 9 container.add(jTextField1,BorderLayout.NORTH); 10 container.add(jTextField2,BorderLayout.SOUTH); 11 12 this.setVisible(true); 13 this.setSize(500,500); 14 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 15 } 16 17 public static void main(String[] args) { 18 new TestTextDeno01(); 19 } 20 }
1 //密码框 2 public class TestTextDeno02 extends JFrame { 3 public TestTextDeno02(){ 4 Container container = this.getContentPane(); 5 6 JPasswordField jPasswordField = new JPasswordField(); 7 jPasswordField.setEchoChar('*'); 8 container.add(jPasswordField); 9 10 this.setVisible(true); 11 this.setSize(500,500); 12 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 13 } 14 15 public static void main(String[] args) { 16 new TestTextDeno02(); 17 } 18 }
1 //文本域 2 public class TestTextDeno03 extends JFrame { 3 public static void main(String[] args) { 4 new TestTextDeno03(); 5 } 6 7 public TestTextDeno03(){ 8 Container container = this.getContentPane(); 9 10 //文本域 11 JTextArea jTextArea = new JTextArea(20,50); 12 jTextArea.setText("HelloWorld"); 13 14 //SCroll面板 15 JScrollPane jScrollPane = new JScrollPane(jTextArea); 16 container.add(jScrollPane); 17 18 this.setVisible(true); 19 this.setSize(500,500); 20 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 21 } 22 }
相关文章
- Java中多线程的ABA问题探讨
- 视频号小店是什么?如何开通视频号小店API?
- 正确理解和使用JAVA中的字符串常量池
- macos如何同时使用登陆多个微信
- 正则表达式清洗文本数据
- 整理文件神器——Q-Dir
- 无网线无显示器环境下配置树莓派连接Wi-Fi开启ssh
- curl 下载地址中有特殊字符解决方案
- 实时渲染对3D设计行业带来哪些改变?
- R语言读取txt,csv和xlsx文件
- 数据结构(1)序章
- 数据结构(2)线性表的顺序存储
- 数据结构(3)单链表
- 数据结构(4)双链表,循环链表,静态链表
- 数据结构(7)栈的应用——括号匹配问题
- 数据结构(8)栈的应用——求值表达式
- 数据结构(9)串的顺序存储结构
- 路由器交换机的命令行模式以及相关命令
- OS——死锁的避免与银行家算法
- 文档处理与查询设计