package com.duo.lesson04;
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo() {} //无参构造
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init() {
IconDemo iconDemo1 = new IconDemo(20, 20);
//图标可以放在标签,按钮上
JLabel jLabel = new JLabel("icon_1", iconDemo1, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setTitle("图标测试窗口");
setSize(650, 500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
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;
}
}
运行结果:

package com.duo.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame{
public ImageIconDemo() {
//获取图片地址
URL url = ImageIconDemo.class.getResource("通用头像.jpg");
JLabel jLabel = new JLabel("ImageIcon");
assert url != null;
ImageIcon imageIcon = new ImageIcon(url); //注意类名不要与内置类ImageIcon重名
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setTitle("图片图标测试窗口");
setSize(650, 500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
运行结果:

package com.duo.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(2, 1, 10, 10)); //hgap和vgap代表面板之间的间距
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
jPanel1.add(new JButton("One"));
jPanel1.add(new JButton("One"));
jPanel1.add(new JButton("One"));
JPanel jPanel2 = new JPanel(new GridLayout(2, 1));
jPanel2.add(new JButton("Two"));
jPanel2.add(new JButton("Two"));
JPanel jPanel3 = new JPanel(new GridLayout(3, 2));
jPanel3.add(new JButton("Three"));
jPanel3.add(new JButton("Three"));
jPanel3.add(new JButton("Three"));
jPanel3.add(new JButton("Three"));
jPanel3.add(new JButton("Three"));
jPanel3.add(new JButton("Three"));
JPanel jPanel4 = new JPanel(new GridLayout(1, 2));
jPanel4.add(new JButton("Four"));
jPanel4.add(new JButton("Four"));
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
contentPane.add(jPanel4);
setTitle("面板测试窗口");
setSize(650, 500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
运行结果:

JScroll是带有滚轮可上下左右滑动的面板。
package com.duo.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container contentPane = getContentPane();
JTextArea jTextArea = new JTextArea("你好", 40, 100);
//jTextArea.setText("你好");
jTextArea.setFont(new Font("宋体", Font.PLAIN, 20));
//Scroll
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
setVisible(true);
setSize(650, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
运行结果:
