3、Swing
3.1、窗口、面板
JFrameDemo:
package com.longge.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
public class JFrameDemo extends Frame{
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
//init();初始化
public void init() {
//JFrame是顶级窗口
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
//frame.setBackground(Color.black);这句代码不能执行
frame.setBounds(100,200,400,400);
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置文字 Jlabel,设置文字居中
JLabel label = new JLabel("欢迎来到龙哥的公司",SwingConstants.CENTER);
frame.add(label);
//frame.getContentPane().setBackground(Color.yellow);
}
}
operation result:
JFrameDemo02:
package com.longge.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrame02 {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
//设置标题 !!!
this.setTitle("java");
setVisible(true);
setBounds(100,100,500,500);
//获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.yellow);
JLabel label = new JLabel("欢迎来到北京!");
//设置水平居中
label.setHorizontalAlignment(SwingConstants.CENTER);
this.add(label);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
operation result:
3.2、弹窗
package com.longge.lesson04;
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.setSize(500,700);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西 放到容器里
Container container = this.getContentPane();
//绝对布局 绝对定位
container.setLayout(null);
//按钮
JButton button = new JButton("点击会弹出来一个框");
button.setBounds(20,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.add(new JLabel("学习Java"));
}
}
Operation result:
3.3、标签
new Label("XXX");
图标标签 ICON
package com.longge.lesson04;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类 JFrame继承
public class IconDemo extends JFrame implements Icon{
private int width;
private int height;
public IconDemo() {}//无参构造
public IconDemo(int width,int height){
this.height = height;
this.width = width;
}
public void init(){
IconDemo iconDemo = new IconDemo(15,15);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("IconTest", iconDemo, SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
}
operation result:
图片标签ICON
package com.longge.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
//获取图片的地址
URL resource = ImageIconDemo.class.getResource("R-C.jpg");
Icon imageIcon = new ImageIcon(resource);
//将图片添加到这个标签旁
JLabel label = new JLabel("ImageIcon");
label.setIcon(imageIcon);
//添加到容器里
Container container = this.getContentPane();
container.add(label);
this.setVisible(true);
this.setBounds(100,100,1200,900);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
operation result:
3.4、面板
JPanel
package com.longge.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(1,1,10,10));//后面的意思是间距
JPanel panel1 = new JPanel(new GridLayout(2,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(1,1));
panel1.add(new JButton("A"));
panel1.add(new JButton("B"));
panel1.add(new JButton("C"));
panel1.add(new JButton("D"));
panel1.add(new JButton("E"));
panel1.add(new JButton("F"));
panel2.add(new JButton("G"));
panel2.add(new JButton("H"));
panel3.add(new JButton("I"));
panel3.add(new JButton("J"));
panel4.add(new JButton("K"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(600,600);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
operation result:
JScrollPanel
package com.longge.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
//文本域
JTextArea textArea = new JTextArea(30,50);
textArea.setText("欢迎大家观看狂神说Java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
//add
Container container = this.getContentPane();
container.add(scrollPane);
this.setBounds(200,200,400,400);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
operation result:
3.5、按钮
图片按钮
package com.longge.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo extends JFrame {
public JButtonDemo() {
URL resource = JButtonDemo.class.getResource("belle.jpg");
ImageIcon icon = new ImageIcon(resource);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
//add
Container container = this.getContentPane();
container.add(button);
this.setVisible(true);
this.setSize(900,1000);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
- 单选按钮
package com.longge.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
URL resource = JButtonDemo02.class.getResource("girl.jpg");
ImageIcon icon = new ImageIcon(resource);
//单选框
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);
//add
Container container = this.getContentPane();
container.add(radioButton1,BorderLayout.NORTH);
container.add(radioButton2,BorderLayout.CENTER);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(900,1000);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
operation result:
- 复选按钮
package com.longge.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
//获取图片的地址
URL resource = JButtonDemo02.class.getResource("girl.jpg");
ImageIcon icon = new ImageIcon(resource);
//多选框
Checkbox checkbox01 = new Checkbox("checkbox01");
Checkbox checkbox02 = new Checkbox("checkbox02");
Container container = this.getContentPane();
container.add(checkbox01,BorderLayout.NORTH);
container.add(checkbox02,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(600,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
operation result:
3.6、列表
- 下拉框
package com.longge.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestComboBoxDemo01 extends JFrame {
public TestComboBoxDemo01() {
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
Container container = this.getContentPane();
container.add(status);
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo01();
}
}
operation result:
- 列表框
package com.longge.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboBoxDemo02 extends JFrame {
public TestComboBoxDemo02() {
//生成列表的内容
//String [] contents = {"列表1","列表2","列表3"};
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("小学");
contents.add("初中");
contents.add("高中");
contents.add("大学");
contents.add("研究生");
Container container = this.getContentPane();
container.add(jList);
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo02();
}
}
operation result:
3.7、文本框
- 文本框
package com.longge.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world",10);
container.add(textField1,BorderLayout.SOUTH);
container.add(textField2,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(600,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
operation result:
- 密码框
package com.longge.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = this.getContentPane();
//尽量在面板中
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');//***
container.add(passwordField);
this.setVisible(true);
this.setSize(600,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
operation result:
- 文本域
package com.longge.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(30,50);
textArea.setText("欢迎大家观看狂神说Java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setBounds(200,200,400,400);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}