代码:
package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 上午 08:31
* @desc Swing
**/
public class JFrameDemo1 {
public static void main(String[] args) {
new JFrameDemo1().init();
}
// init()初始化。
public void init(){
JFrame jFrame = new JFrame("这是我们的JFrame窗口。");
jFrame.setVisible(true);
jFrame.setBackground(Color.BLUE);
jFrame.setBounds(377, 377, 277, 277);
// 设置文字。 JLabel
JLabel jLabel = new JLabel("Welcome to EdwinD's Way.");
jFrame.add(jLabel);
// 关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
框框2:标签居中。
package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 上午 09:05
* @desc Swing
**/
public class JFrameDemo2 {
public static void main(String[] args) {
MyJFrame2 myJFrame2 = new MyJFrame2();
myJFrame2.setTitle("这是我们的JFrame窗口2.0");
myJFrame2.init();
}
}
class MyJFrame2 extends JFrame {
public void init(){
setBounds(377, 377, 277, 277);
setVisible(true);
JLabel jLabel = new JLabel("Welcome to EdwinD's Way-2.0.");
this.add(jLabel);
// label居中,设置水平对齐
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 获得一个容器,
Container contentPane = this.getContentPane();
contentPane.setBackground(new Color(77,77,77));
// 关闭事件
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
代码:
package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author EdwinD
* @create 2020.08.19 上午 09:46
* @desc 弹窗
**/
//主窗口
public class DialogDemo1 extends JFrame {
public static void main(String[] args) {
new DialogDemo1();
}
public DialogDemo1() {
this.setVisible(true);
this.setBounds(277, 277, 577, 577);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// JFrame 存放东西,容器
Container container = this.getContentPane();
// 绝对布局
container.setLayout(null);
// 按钮
JButton jButton = new JButton("弹窗显灵!");
jButton.setBounds(100, 100, 100, 70);
// 点击此按钮,会有弹窗。
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 弹窗
new MyDialogDemo();
}
});
container.add(jButton);
}
}
//弹窗
class MyDialogDemo extends JDialog {
public MyDialogDemo() {
this.setVisible(true);
this.setBackground(Color.BLACK);
this.setBounds(100, 100, 300, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
contentPane.add(new Label("跟着老秦学Java"));
}
}
输出:
Label
new JLabel("……");
图标’s Icon
代码:
package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 上午 10:48
* @desc 标签。图标需要实现类,Frame继承等。
**/
public class IconDemo1 extends JFrame implements Icon {
private int width;
private int height;
public static void main(String[] args) {
new IconDemo1().init();
}
//无参构造
public IconDemo1() {
}
//有参构造
public IconDemo1(int width, int height) {
this.width = width;
this.height = height;
}
public void init() {
IconDemo1 iconDemo1 = new IconDemo1(17, 17);
JLabel label = new JLabel("icontest", iconDemo1, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setBounds(277, 277, 677, 677);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
输出:
图中的小点点,就是我们创造的图标。
2.0
图片’s Icon
代码:
package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @author EdwinD
* @create 2020.08.19 上午 11:17
* @desc 图片显示
**/
public class ImageIconDemo extends JFrame{
public static void main(String[] args) {
new ImageIconDemo();
}
public ImageIconDemo() {
// 获取图片的地址
URL url = ImageIconDemo.class.getResource("Study.jpg");
// label作为承载图片的载体。
JLabel label = new JLabel("图片旁边的小标签");
// 注意命名,不要和关键词重合。
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(277, 277, 877, 877);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
注:
由于IDEA存在缓存机制,有可能在第一次的编译过程中会出现空指针异常现象(ImageIcon imageIcon = new ImageIcon(url);),再更改了代码后依旧是无法正常运转,建议此时进行重启。!!!。!!!
JPanel
试用Scroll面板,带滚轮,可伸缩。
代码:
package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 下午 05:29
* @desc
**/
public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo() {
Container container = this.getContentPane();
// 文本域
JTextArea textArea = new JTextArea();
textArea.setText("Welcome to EdwinD's Java way.It's really a long and hard way for us to go." +
"But we must hand on it and try our best to do.");
// Scroll面板,带滚轮,可伸缩。
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(277, 277, 300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
图片按钮:
将按钮的内容设置为一个图片,点击后是按钮的效果:
package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @author EdwinD
* @create 2020.08.19 下午 05:47
* @desc 按钮
**/
public class JButton1 extends JFrame {
public static void main(String[] args) {
new JButton1();
}
public JButton1() {
Container container = this.getContentPane();
// 将一个图片,变为图标
URL url = JButton1.class.getResource("Study.jpg");
Icon icon = new ImageIcon(url);
// 把图片放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
// add
container.add(button);
this.setVisible(true);
this.setBounds(277, 277, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
多个选项只能选取一个,按组进行分类,一个组选出来一个。
代码:
package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @author EdwinD
* @create 2020.08.19 下午 06:05
* @desc 单选框按钮
**/
public class JButton2 extends JFrame {
public static void main(String[] args) {
new JButton2();
}
public JButton2() {
Container container = this.getContentPane();
// 单选框
JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
JRadioButton radioButton2 = new JRadioButton("JRadioButton2");
JRadioButton radioButton3 = new JRadioButton("JRadioButton3");
// 由于单选框只能选择一个,所以说我们采取“分组”的方法,一个组内只有一个可以被选中
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1, BorderLayout.NORTH);
container.add(radioButton2, BorderLayout.CENTER);
container.add(radioButton3, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(277, 277, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
每个选项单独成组,可以一次性选择多个按钮内容。
代码:
package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 下午 06:10
* @desc 多选框
**/
public class JButton3 extends JFrame {
public static void main(String[] args) {
new JButton3();
}
public JButton3() {
Container container = this.getContentPane();
// 多选框
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2 = new JCheckBox("checkBox2");
container.add(checkBox1, BorderLayout.NORTH);
container.add(checkBox2, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(277, 277, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
代码:
package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 下午 06:27
* @desc 下拉框
**/
public class TextCombobox1 extends JFrame {
public static void main(String[] args) {
new TextCombobox1();
}
public TextCombobox1() {
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem("起始默认字");
status.addItem("正在热映");
status.addItem("即将上映");
status.addItem("已下架");
container.add(status);
this.setVisible(true);
this.setBounds(277, 277, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
应用场景:
- 填写信息时选择地区,或者一些单选项目。
- 展示列表信息,一般是动态扩容的。
代码:
package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
/**
* @author EdwinD
* @create 2020.08.19 下午 06:27
* @desc 下拉框
**/
public class TextCombobox1 extends JFrame {
public static void main(String[] args) {
new TextCombobox1();
}
public TextCombobox1() {
Container container = this.getContentPane();
// 生成列表内容
String[] content = {"7", "77", "777"};
Vector vector = new Vector();
vector.add("Alita");
vector.add("Jack");
vector.add("Toney");
// 列表中需要放入的内容
JList jList1 = new JList(content);
JList jList2 = new JList(vector);
container.add(jList1);
container.add(jList2);
this.setVisible(true);
this.setBounds(277, 277, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
代码:
package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 下午 07:15
* @desc 文本框
**/
public class TextText1 extends JFrame {
public static void main(String[] args) {
new TextText1();
}
public TextText1() {
Container container = this.getContentPane();
JTextField textField1 = new JTextField("Hello");
JTextField textField2 = new JTextField("World!",20);//规定最多20个字符。
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(277, 277, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
代码:
package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 下午 07:20
* @desc 密码框
**/
public class TextPassword1 extends JFrame {
public static void main(String[] args) {
new TextPassword1();
}
public TextPassword1() {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setBounds(277, 277, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
同Lesson5里面的文本域
代码:
package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
/**
* @author EdwinD
* @create 2020.08.19 下午 05:29
* @desc
**/
public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo() {
Container container = this.getContentPane();
// 文本域
JTextArea textArea = new JTextArea();
textArea.setText("Welcome to EdwinD's Java way.It's really a long and hard way for us to go." +
"But we must hand on it and try our best to do.");
// Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(277, 277, 300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
输出:
路漫漫其修远兮,吾将上下而求索。
《【狂神说Java】GUI编程入门到游戏实战》
视频链接
2020.08.19