1 package com.yycome.Swing_1; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 ? 6 //Swing只是设计的更好一点,它本质上继承了awt 7 public class TestJFrameDemo { 8 public static void main(String[] args) { 9 new MyJFrame().init(); 10 } 11 } 12 class MyJFrame{ 13 //在开发中,我们一般设置一个初始化的方法init()!就舍弃了继承! 14 public void init(){ 15 JFrame jFrame = new JFrame("我的第一个JFrame窗口"); 16 jFrame.setVisible(true); 17 jFrame.setBounds(2,2,500,400); 18 //swing封装了关闭窗口的方法,可以直接调用 19 jFrame.setDefaultCloseOperation(3); 20 //重点来了!! 21 //swing里面有一个容器的概念(窗口也是个容器),首先要获得一个容器,才能在容器上设置 22 //背景这些东西 23 Container contentPane = jFrame.getContentPane(); 24 JLabel label = new JLabel("第一个标签!"); 25 contentPane.add(label); 26 contentPane.setBackground(Color.yellow); 27 //设置标签居中对齐 28 label.setHorizontalAlignment(0); 29 } 30 }
?
1 package com.yycome.Swing_1; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 ? 8 //设置弹窗!很重要!JFrame默认带有窗口的关闭!但是默认的是隐藏! 9 // 所以还是要加上关闭方法 10 public class TestJDialog { 11 public static void main(String[] args) { 12 new MyDialog().init(); 13 } 14 } 15 class MyDialog extends JFrame{ 16 public void init(){ 17 this.setBounds(20,20,500,400); 18 this.setVisible(true); 19 this.setDefaultCloseOperation(3); 20 ? 21 JButton button = new JButton("点我提示!"); 22 Container container = this.getContentPane(); 23 //绝对布局,就是自己用坐标设置布局! 24 container.setLayout(null); 25 button.setBounds(20,20,100,50); 26 container.add(button); 27 button.addActionListener(new ActionListener() { 28 @Override 29 public void actionPerformed(ActionEvent e) { 30 //弹窗 31 new MyDialogDemo(); 32 } 33 }); 34 } 35 } 36 //弹窗类 37 class MyDialogDemo extends JDialog{ 38 public MyDialogDemo() { 39 this.setBounds(50,50,400,300); 40 this.setVisible(true); 41 Container pane = this.getContentPane(); 42 JLabel label = new JLabel("我会弹窗了~"); 43 label.setSize(100,50); 44 label.setHorizontalAlignment(0); 45 pane.add(label); 46 } 47 }
1.JLabel
2.图标Icon:(就是图片标签)
自己画一个logo:
1 package com.yycome.Swing_1; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 ? 6 //画出图标,实际就是在label上画图形或者添加图片! 7 public class TestIcon { 8 public static void main(String[] args) { 9 new MyIcon().init(); 10 } 11 } 12 //一个类既继承了一个类又实现了一个接口 13 class MyIcon extends JFrame implements Icon{ 14 //属性 15 int width,height; 16 //方法 17 public MyIcon() { 18 } 19 ? 20 public MyIcon(int width, int height) { 21 this.width = width; 22 this.height = height; 23 } 24 public void init(){ 25 this.setBounds(100,100,500,400); 26 this.setVisible(true); 27 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 28 MyIcon icon = new MyIcon(20, 20); 29 JLabel label = new JLabel("logo", icon, 0); 30 Container pane = this.getContentPane(); 31 pane.add(label); 32 } 33 ? 34 //画出图标 35 @Override 36 public void paintIcon(Component c, Graphics g, int x, int y) { 37 //画一个圆形图标 38 g.setColor(Color.yellow); 39 g.fillOval(x,y,width,height);//这里有一个int类型的x,y! 40 g.setColor(Color.black); 41 } 42 //获得图标的宽 43 @Override 44 public int getIconWidth() { 45 return width; 46 } 47 //获得图标的高 48 @Override 49 public int getIconHeight() { 50 return height; 51 } 52 } 53 ?
设置图片标签:
1 package com.yycome.Swing_1; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 import java.net.URL; 6 ? 7 //绘制图片标签: 8 public class TestImageIcon { 9 public static void main(String[] args) { 10 new MyImageIcon().init(); 11 } 12 } 13 class MyImageIcon extends JFrame{ 14 ? 15 public void init(){ 16 this.setBounds(100,100,500,400); 17 this.setVisible(true); 18 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 19 ? 20 JLabel label = new JLabel("imageIcon"); 21 label.setHorizontalAlignment(0); 22 //加/为根目录,以后都加上吧 23 URL url = MyImageIcon.class.getResource("/image/4.jpg"); 24 ? 25 //url转为icon,这个方法默认是从根目录加载的!!所以url要从工程的根目录出发 26 ImageIcon icon = new ImageIcon(url); 27 label.setIcon(icon); 28 ? 29 Container pane = this.getContentPane(); 30 pane.add(label); 31 } 32 }
JPanel:普通面板
1 package com.yycome.Swing_2; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 ? 6 ? 7 //普通面板 8 public class TestJPanel { 9 public static void main(String[] args) { 10 new MyJPanel().init(); 11 } 12 } 13 class MyJPanel extends JFrame { 14 public void init(){ 15 this.setBounds(100,100,500,400); 16 this.setVisible(true); 17 this.setDefaultCloseOperation(3); 18 Container container = this.getContentPane(); 19 container.setLayout(new GridLayout(1,2)); 20 ? 21 JPanel panel1 = new JPanel(); 22 JPanel panel2 = new JPanel(); 23 panel1.setLayout(new GridLayout(2,1)); 24 panel2.setLayout(new GridLayout(1,2)); 25 ? 26 JButton button1 = new JButton("1"); 27 JButton button2 = new JButton("1"); 28 JButton button3 = new JButton("2"); 29 JButton button4 = new JButton("2"); 30 ? 31 panel1.add(button1); 32 panel1.add(button2); 33 panel2.add(button4); 34 panel2.add(button3); 35 ? 36 container.add(panel1); 37 container.add(panel2); 38 } 39 } 40 JscrollPane:带滚动条的面板 41 42 package com.yycome.Swing_2; 43 ? 44 import javax.swing.*; 45 import java.awt.*; 46 //带滚动条的面板 47 public class TestJScrollPane { 48 public static void main(String[] args) { 49 new MyScrollPane().init(); 50 } 51 } 52 class MyScrollPane extends JFrame{ 53 public void init(){ 54 this.setBounds(100,100,300,200); 55 this.setVisible(true); 56 this.setDefaultCloseOperation(3); 57 Container container = this.getContentPane(); 58 ? 59 JTextArea jTextArea = new JTextArea(50,10); 60 jTextArea.setText("hello"); 61 JScrollPane jScrollPane = new JScrollPane(jTextArea); 62 //以下两行代码是有问题的! 63 //JScrollPane不能用add方法增加组件!!显示不出来!只能在定义的时候加组件!! 64 //!!难道??!!!在swing中,只有容器才能add!!面板和窗口都不能add!! 65 //但是,panel面板可以add啊!很迷! 66 // JScrollPane jScrollPane = new JScrollPane(); 67 // jScrollPane.add(jTextArea); 68 container.add(jScrollPane); 69 } 70 } 71
图片按钮:
1 package com.yycome.Swing_2; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 import java.net.URL; 6 ? 7 //图片按钮 8 public class TestJButton01 { 9 public static void main(String[] args) { 10 new MyJButton01().init(); 11 } 12 } 13 class MyJButton01 extends JFrame{ 14 public void init(){ 15 setDefaultCloseOperation(3); 16 setBounds(100,100,300,300); 17 setVisible(true); 18 ? 19 Container container = this.getContentPane(); 20 JButton button = new JButton(); 21 button.setText("图片按钮"); 22 button.setToolTipText("这是一个图片按钮!");//鼠标悬停提示! 23 URL url = MyJButton01.class.getResource("/image/3.jpg"); 24 //把图片转换为图标 25 ImageIcon icon = new ImageIcon(url); 26 button.setIcon(icon); 27 ? 28 container.add(button); 29 ? 30 } 31 }
单选按钮:JRadioButton
1 package com.yycome.Swing_2; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 ? 6 //单选按钮 7 public class TestJButton02 { 8 public static void main(String[] args) { 9 new MyJRadioButton().init(); 10 } 11 } 12 class MyJRadioButton extends JFrame{ 13 public void init(){ 14 setDefaultCloseOperation(3); 15 setBounds(100,100,300,300); 16 setVisible(true); 17 ? 18 Container container = this.getContentPane(); 19 ? 20 //创建单选按钮 21 JRadioButton button1 = new JRadioButton("button1"); 22 JRadioButton button2 = new JRadioButton("button2"); 23 JRadioButton button3 = new JRadioButton("button3"); 24 ? 25 //把单选按钮分组!!这是灵魂所在!如果不分组,则类似多选框! 26 ButtonGroup group = new ButtonGroup(); 27 group.add(button1); 28 group.add(button2); 29 group.add(button3); 30 //可以直接设置布局! 31 container.add(button1,BorderLayout.NORTH); 32 container.add(button2,BorderLayout.CENTER); 33 container.add(button3,BorderLayout.SOUTH); 34 } 35 }
多选按钮:JCheckBOx
1 package com.yycome.Swing_2; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 ? 6 //多选框 7 public class TestJButton03 { 8 public static void main(String[] args) { 9 new MyJCheckBox().init(); 10 } 11 } 12 class MyJCheckBox extends JFrame{ 13 public void init(){ 14 setDefaultCloseOperation(3); 15 setBounds(100,100,300,300); 16 setVisible(true); 17 ? 18 Container container = this.getContentPane(); 19 ? 20 JCheckBox button1 = new JCheckBox("button1"); 21 JCheckBox button2 = new JCheckBox("button2"); 22 JCheckBox button3 = new JCheckBox("button3"); 23 ? 24 container.add(button1,BorderLayout.NORTH); 25 container.add(button2,BorderLayout.CENTER); 26 container.add(button3,BorderLayout.SOUTH); 27 } 28 } 29
下拉框:JComboBox
1 package com.yycome.Swing_3; 2 ? 3 import javafx.scene.control.ComboBox; 4 ? 5 import javax.swing.*; 6 import java.awt.*; 7 ? 8 //下拉框 9 public class TestComboBox01 { 10 public static void main(String[] args) { 11 new MyComboBox().init(); 12 } 13 } 14 class MyComboBox extends JFrame{ 15 public void init(){ 16 setDefaultCloseOperation(3); 17 setBounds(100,100,300,300); 18 setVisible(true); 19 ? 20 Container container = this.getContentPane(); 21 ? 22 // JComboBox<Object> comboBox = new JComboBox<>();//泛型,删去也不报错 23 JComboBox comboBox = new JComboBox(); 24 comboBox.addItem(null);//默认 25 comboBox.addItem("语文"); 26 comboBox.addItem("数学"); 27 comboBox.addItem("英语"); 28 container.add(comboBox); 29 ? 30 } 31 } 32 33 34 列表框:JList 35 36 package com.yycome.Swing_3; 37 ? 38 import javax.swing.*; 39 import java.awt.*; 40 import java.util.Vector; 41 ? 42 //列表框,就是一次陈列所有东西的一个列表 43 public class TestJList { 44 public static void main(String[] args) { 45 new MyJList().init(); 46 } 47 } 48 class MyJList extends JFrame{ 49 public void init(){ 50 setBounds(100,100,500,400); 51 setVisible(true); 52 setDefaultCloseOperation(3); 53 ? 54 Container container = this.getContentPane(); 55 ? 56 //需要一个存放列表内容的盒子!可以是String静态数组,也可以是Vector动态数组! 57 // String[] contents = {"1","2","3","4"};//静态数组初始化 58 Vector contents = new Vector();//动态的数据类型!可以动态加入数据! 59 //动态数据类型初始化 60 contents.add("1"); 61 contents.add("2"); 62 contents.add("3"); 63 contents.add("4"); 64 ? 65 JList jList = new JList(contents); 66 container.add(jList); 67 } 68 } 69 ?
文本框:JTextField
1 package com.yycome.Swing_3; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 ? 6 ? 7 //文本框 8 public class TestJTextField { 9 public static void main(String[] args) { 10 new MyText().init(); 11 } 12 } 13 class MyText extends JFrame { 14 public void init(){ 15 setBounds(100,100,500,400); 16 setVisible(true); 17 setDefaultCloseOperation(3); 18 ? 19 Container container = this.getContentPane(); 20 ? 21 JTextField textField1 = new JTextField("hello"); 22 JTextField textField2 = new JTextField("world!"); 23 container.add(textField2,BorderLayout.SOUTH); 24 container.add(textField1,BorderLayout.NORTH); 25 } 26 }
密码框:JPasswordField
package com.yycome.Swing_3; ? import javax.swing.*; import java.awt.*; ? //密码框 public class TestJPasswordField { public static void main(String[] args) { new MyJPasswordField().init(); } } class MyJPasswordField extends JFrame{ public void init(){ setBounds(100,100,500,400); setVisible(true); setDefaultCloseOperation(3); ? Container container = this.getContentPane(); ? JPasswordField passwordField = new JPasswordField(); passwordField.setEchoChar(‘*‘); container.add(passwordField); } }
?
文本域:JTextArea,一般配合滑动条面板使用,见JScrollPane
1 package com.yycome.Swing_2; 2 ? 3 import javax.swing.*; 4 import java.awt.*; 5 //带滚动条的面板 6 public class TestJScrollPane { 7 public static void main(String[] args) { 8 new MyScrollPane().init(); 9 } 10 } 11 class MyScrollPane extends JFrame{ 12 public void init(){ 13 this.setBounds(100,100,300,200); 14 this.setVisible(true); 15 this.setDefaultCloseOperation(3); 16 Container container = this.getContentPane(); 17 ? 18 JTextArea jTextArea = new JTextArea(50,10); 19 jTextArea.setText("hello"); 20 JScrollPane jScrollPane = new JScrollPane(jTextArea); 21 //以下两行代码是有问题的! 22 //JScrollPane不能用add方法增加组件!!显示不出来!只能在定义的时候加组件!! 23 //!!难道??!!!在swing中,只有容器才能add!!面板和窗口都不能add!! 24 //但是,panel面板可以add啊!很迷! 25 // JScrollPane jScrollPane = new JScrollPane(); 26 // jScrollPane.add(jTextArea); 27 container.add(jScrollPane); 28 } 29 }
原文:https://www.cnblogs.com/zyyComeOn/p/14347467.html