package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
public class JframeDemo02 {
public static void main(String[] args) {
new MyJframe02().init();
}
}
class MyJframe02 extends Jframe {
public void init() {
this.setBounds(200, 200, 500, 500);
//获得一个容器,只有这样设置颜色才会生效
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.CYAN);
//设置文字
JLabel jLabel = new JLabel("akita");
this.add(jLabel);
//让文本标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
setVisible(true);
}
}
弹窗
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends Jframe {
public DialogDemo() {
this.setSize(500, 350);
this.setLocation(300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Jframe 放东西容器
Container contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
//按钮
JButton jButton = new JButton("点击弹出对话框");
jButton.setBounds(30, 30, 200, 50);
//点击这个按钮弹出一个弹窗
jButton.addActionListener(new ActionListener() { //监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDD();
}
});
contentPane.add(jButton);
this.setVisible(true);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDD extends JDialog {
public MyDialogDD() {
this.setBounds(100, 100, 500, 500);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
Label akita = new Label("akita");
akita.setSize(50, 50);
contentPane.add(akita);
this.setVisible(true);
}
}
标签
JLabel
JLabel jLabel = new JLabel("标签");
Icon
package com.akita.lesson03;
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.width = width;
this.height = height;
}
public void init() {
IconDemo iconDemo = new IconDemo(15, 15);
//图标要放在标签上
JLabel jLabel = new JLabel("icon_test", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
this.setBounds(200,200,400,340);
this.setVisible(true);
this.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 this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
ImageIcon
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends Jframe {
public ImageIconDemo() {
JLabel akita = new JLabel("akita");
//获取图片地址
URL url = ImageIconDemo.class.getResource("icon.png");
ImageIcon imageIcon = new ImageIcon(url);
akita.setIcon(imageIcon);
akita.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(akita);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(200, 200, 500, 500);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
JPanel
package com.akita.lesson04;
import com.akita.lesson03.JframeDemo;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends Jframe {
public JPanelDemo() {
this.setBounds(100, 100, 400, 400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 2, 10, 10));
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
JPanel jPanel2 = new JPanel(new GridLayout(1, 2));
JPanel jPanel3 = new JPanel(new GridLayout(2, 1));
JPanel jPanel4 = new JPanel(new GridLayout(3, 2));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);
this.setVisible(true);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
运行结果
package com.akita.lesson04;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends Jframe {
public JScrollDemo() {
Container contentPane = this.getContentPane();
//文本域
Jtextarea jtextarea = new Jtextarea(20, 50);
jtextarea.setText("akita");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jtextarea);
jScrollPane.setBackground(new Color(1, 1, 1));
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(100, 100, 300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
运行效果
package com.akita.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends Jframe {
public JButtonDemo01() {
Container container = this.getContentPane();
//把图片转化成图标
URL url = JButtonDemo01.class.getResource("icon.png");
ImageIcon icon = new ImageIcon(url);
//把这个图标放置在按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("akita");
//add
container.add(jButton);
setVisible(true);
setBounds(100, 100, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
运行效果
package com.akita.lesson04;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo02 extends Jframe {
public JButtonDemo02() {
Container container = this.getContentPane();
//单选框
JRadioButton radioButton1 = new JRadioButton("radioButton1");
JRadioButton radioButton2 = new JRadioButton("radioButton2");
JRadioButton radioButton3 = new JRadioButton("radioButton3");
//由于单选框只能选择一个,所以我们通常对他们进行分组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
buttonGroup.add(radioButton3);
//add
container.add(radioButton1, BorderLayout.CENTER);
container.add(radioButton2, BorderLayout.NORTH);
container.add(radioButton3, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(200, 200, 500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
运行效果
package com.akita.lesson04;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 extends Jframe {
public JButtonDemo03() {
Container container = this.getContentPane();
//多选框
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
container.add(jCheckBox1, BorderLayout.NORTH);
container.add(jCheckBox2, BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200, 200, 400, 250);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
运行效果



