三、Swing
1.窗口、面板JFrame
package com.wang.lesson4; import javax.swing.*; import java.awt.*; public class JFrameDemo { //init();初始化 public void init(){ //顶级窗口 JFrame jf = new JFrame("这是一个JFrame窗口"); jf.setBounds(100,100,400,300); //设置文字Label->JLabel jf.setBackground(Color.BLUE); JLabel jl = new JLabel("JJJJJ"); jf.add(jl); //让文本标签居中 jl.setHorizontalAlignment(SwingConstants.CENTER); //容器实例化 jf.getContentPane().setBackground(Color.red); jf.setVisible(true); //关闭事件 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { //建立一个窗口 new JFrameDemo().init(); } }
2.弹窗
package com.wang.lesson4; 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, 100, 400, 400); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //Jframe 放东西,容器 Container contentPane = this.getContentPane(); //绝对布局 contentPane.setLayout(null); //设置背景 contentPane.setBackground(Color.BLUE); //按钮 JButton jButton = new JButton("点击弹出一个对话框"); jButton.setBounds(30, 30, 200, 50); //点击按钮弹出弹框 jButton.addActionListener(new ActionListener() {//监听器 @Override public void actionPerformed(ActionEvent e) { //弹窗 new MyDialog(); } }); contentPane.add(jButton); } public static void main(String[] args) { new DialogDemo(); } } //弹窗的窗口 class MyDialog extends JDialog { public MyDialog() { this.setVisible(true); this.setBounds(100, 100, 500, 500); // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //JDialog退出只能是D0_ONTHING,HIDE,DISPOSE这三个中的一种 //应该是默认就有关闭事件 this.setTitle("这是一个弹窗"); Container contentPane = this.getContentPane(); contentPane.setLayout(null); contentPane.setBackground(Color.ORANGE); JLabel jjj = new JLabel("学习学习"); contentPane.add(jjj); jjj.setBounds(20,20,50,50); } }
3.标签
label
new JLabel("xxx");
图标Icon
package com.wang.lesson4; import javax.swing.*; import java.awt.*; //图标,需要实现类,Frame继承 public class IconDemo extends JFrame implements Icon { private int width; private int hight; public IconDemo(){};//无参构造 //有参构造 public IconDemo(int width,int hight){ this.width = width; this.hight = hight; }; public void init(){ IconDemo iconDemo = new IconDemo(15, 15); //图标可以放在标签,也可以放在按钮上! JLabel jLabel = new JLabel("标签",iconDemo,SwingConstants.CENTER); Container contentPane = getContentPane(); contentPane.add(jLabel); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillOval(x,y,width,hight); } @Override public int getIconWidth() { return this.width; } @Override public int getIconHeight() { return this.hight; } public static void main(String[] args) { new IconDemo().init(); } }
图片
package com.wang.lesson4; import javax.swing.*; import java.awt.*; import java.net.URL; public class ImageIconDemo extends JFrame { public ImageIconDemo(){ JLabel jLabel = new JLabel("图片"); URL resource = ImageIconDemo.class.getResource("4.png"); ImageIcon imageIcon = new ImageIcon(resource); jLabel.setIcon(imageIcon); jLabel.setHorizontalAlignment(SwingConstants.CENTER); Container contentPane = getContentPane(); contentPane.add(jLabel); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,100,800,800); } public static void main(String[] args) { new ImageIconDemo(); } }
4.面板
JPanel
package com.wang.lesson5; import javax.swing.*; import java.awt.*; public class JPanelDemo extends JFrame { public JPanelDemo(){ Container contentPane = this.getContentPane(); contentPane.setLayout(new GridLayout(2,1,10,10));//后边两个是间距 JPanel jPanel = new JPanel(new GridLayout(1, 3)); JPanel jPane2 = new JPanel(new GridLayout(1, 2)); JPanel jPane3 = new JPanel(new GridLayout(1, 1)); jPanel.add(new JButton("aaa")); jPanel.add(new JButton("bbb")); jPanel.add(new JButton("ccc")); jPane2.add(new JButton("111")); jPane2.add(new JButton("222")); jPane3.add(new JButton("---")); setBounds(100,100,500,400); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); contentPane.add(jPanel); contentPane.add(jPane2); contentPane.add(jPane3); setVisible(true); contentPane.setBackground(Color.YELLOW); } public static void main(String[] args) { new JPanelDemo(); } }
JScrollPanel
package com.wang.lesson4; import javax.swing.*; import java.awt.*; public class JScrollPanelDemo extends JFrame { public JScrollPanelDemo(){ Container contentPane = this.getContentPane(); //文本域 JTextArea jTextArea = new JTextArea(20, 50); jTextArea.setText("学习学习"); //面板 并添加到contentpane contentPane.add(new JScrollPane(jTextArea)); this.setVisible(true); this.setBounds(100,100,400,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); contentPane.setBackground(Color.BLUE); } public static void main(String[] args) { new JScrollPanelDemo(); } }
5.按钮
图片按钮
package com.wang.lesson5; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo01 extends JFrame { public JButtonDemo01(){ Container contentPane = this.getContentPane(); //图片变为图标 URL resource = JButtonDemo01.class.getResource("4.png"); Icon icon = new ImageIcon(resource); JButton jButton = new JButton(); jButton.setIcon(icon); //悬浮框 jButton.setToolTipText("这是一个图片按钮"); contentPane.add(jButton); this.setVisible(true); this.setBounds(100,100,400,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo01(); } }
单选按钮
package com.wang.lesson5; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo02 extends JFrame { public JButtonDemo02(){ Container contentPane = this.getContentPane(); //图片变为图标 URL resource = JButtonDemo01.class.getResource("4.png"); Icon icon = new ImageIcon(resource); //单选框 JRadioButton jrb01 = new JRadioButton("jrb01"); JRadioButton jrb02 = new JRadioButton("jrb02"); JRadioButton jrb03 = new JRadioButton("jrb03"); //由于单选框只能选择一个,分组 ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(jrb01); buttonGroup.add(jrb02); buttonGroup.add(jrb03); contentPane.add(jrb01,BorderLayout.CENTER); contentPane.add(jrb02,BorderLayout.NORTH); contentPane.add(jrb03,BorderLayout.SOUTH); this.setVisible(true); this.setBounds(100,100,400,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo02(); } }
复选按钮
package com.wang.lesson5; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo03 extends JFrame { public JButtonDemo03(){ Container contentPane = this.getContentPane(); //图片变为图标 URL resource = JButtonDemo01.class.getResource("4.png"); Icon icon = new ImageIcon(resource); //多选框 JCheckBox jcb1 = new JCheckBox("jcb1"); JCheckBox jcb2 = new JCheckBox("jcb2"); JCheckBox jcb3 = new JCheckBox("jcb3"); //流式布局 contentPane.setLayout(new FlowLayout(FlowLayout.LEFT)); contentPane.add(jcb1); contentPane.add(jcb2); contentPane.add(jcb3); //东西南北中布局 /* contentPane.add(jcb1,BorderLayout.NORTH); contentPane.add(jcb2,BorderLayout.CENTER); contentPane.add(jcb3,BorderLayout.SOUTH); */ this.setVisible(true); this.setBounds(100,100,400,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo03(); } }
6.列表
下拉框
package com.wang.lesson6; import javax.swing.*; import java.awt.*; public class TestComboboxDemo01 extends JFrame { public TestComboboxDemo01(){ Container container = this.getContentPane(); JComboBox status = new JComboBox(); status.addItem("未上映"); status.addItem("正在热映"); status.addItem("已下架"); container.add(status); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setBounds(100,100,500,400); } public static void main(String[] args) { new TestComboboxDemo01(); } }
列表框
应用场景:
选择地区,或者一些单个选项
列表,展示信息,一般是动态扩展
package com.wang.lesson6; 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); JList jList1 = new JList(contents); contents.add("2222"); contents.add("333"); container.add(jList); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setBounds(100,100,500,400); } public static void main(String[] args) { new TestComboboxDemo02(); } }
7.文本框
文本框
package com.wang.lesson6; import javax.swing.*; import java.awt.*; public class TestTextDemo01 extends JFrame { public TestTextDemo01(){ Container container = this.getContentPane(); //不布局只会出现WORLD,且位置不对 this.setLayout(new FlowLayout(FlowLayout.RIGHT)); JTextField jTextField1 = new JTextField("HELLO"); JTextField jTextField2 = new JTextField("WORLD",20); container.add(jTextField1); container.add(jTextField2); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setBounds(100,100,400,300); } public static void main(String[] args) { new TestTextDemo01(); } }
密码框
package com.wang.lesson6; import javax.swing.*; import java.awt.*; public class TestTextDemo02 extends JFrame { public TestTextDemo02(){ Container container = this.getContentPane(); JPasswordField jPasswordField = new JPasswordField();//--- jPasswordField.setEchoChar('-'); container.add(jPasswordField); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setBounds(100,100,400,300); } public static void main(String[] args) { new TestTextDemo02(); } }
文本域
//文本域 JTextArea jTextArea = new JTextArea(20, 50); jTextArea.setText("学习学习"); //面板 并添加到contentpane contentPane.add(new JScrollPane(jTextArea));
后记
Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~