import javax.swing.*;
import java.awt.*;
public class JFrameTest2 {
public static void main(String[] args) {
new MyJframe2().init();
}
}
class MyJframe2 extends JFrame{
//init() 初始化
public void init(){
//顶级窗体
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//设置文字 JLabel
JLabel jLabel = new JLabel("一段文字");
add(jLabel);
//让文本标签居中,设置水平对齐
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化,获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.blue);
}
}
JDialog
默认有关闭事件
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗体
public class DialogTest extends JFrame {
public DialogTest(){
this.setBounds(200,200,300,300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//获取窗体的容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
Button button = new Button("Dialog");
button.setBounds(30,30,200,100);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogTest();
}
}
//弹窗的窗体
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(200,200,300,300);
//弹窗默认有关闭方式
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JLabel jLabel = new JLabel("这是一个弹窗");
jLabel.setBounds(100,100,200,200);
container.add(jLabel);
}
}
JLabel
new JLabel("");
图标Icon
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconTest extends JFrame{
public ImageIconTest(){
//获取图片的地址
JLabel label = new JLabel();
URL url = ImageIconTest.class.getResource("图片.jpg"); //获取本类同级目录下的资源
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(500,500,300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconTest();
}
}
JPanel
import javax.swing.*;
import java.awt.*;
public class JPanelTest extends JFrame {
public JPanelTest() {
Container container = getContentPane();
container.setLayout(new GridLayout(2,1,10,10)); //后面的两个参数为间距
JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(2,2));
panel1.add(new Button("1"));
panel1.add(new Button("1"));
panel1.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("2"));
panel3.add(new Button("3"));
panel3.add(new Button("3"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
panel4.add(new Button("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelTest();
}
}
JScrollPanel
import javax.swing.*;
import java.awt.*;
public class JScrollPanelTest extends JFrame {
public JScrollPanelTest(){
Container container = getContentPane();
//文本域
JTextArea textArea = new JTextArea();
textArea.setText("这是一个文本域");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(200,300,300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPanelTest();
}
}
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonTest01 extends JFrame {
public JButtonTest01(){
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonTest01.class.getResource("图片02.jpg");
Icon icon = new ImageIcon(url);
//把这个图片添加到按钮
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setBounds(200,200,300,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest01();
}
}
RadioButton
import javax.swing.*;
import java.awt.*;
public class JButtonTest02 extends JFrame {
public JButtonTest02(){
Container container = this.getContentPane();
//单选框
JRadioButton radioButton1 = new JRadioButton("01");
JRadioButton radioButton2 = new JRadioButton("02");
JRadioButton radioButton3 = new JRadioButton("03");
//由于单选框只能选一个,分组,一个组只能选一个
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(200,200,300,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest02();
}
}
CheckBox
import javax.swing.*;
import java.awt.*;
public class JButtonTest03 extends JFrame {
public JButtonTest03(){
Container container = this.getContentPane();
JCheckBox checkBox1 = new JCheckBox("01");
JCheckBox checkBox2 = new JCheckBox("02");
container.add(checkBox1, BorderLayout.NORTH);
container.add(checkBox2, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(200,200,300,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest03();
}
}
JComboBox
import javax.swing.*;
import java.awt.*;
public class ComboboxTest extends JFrame {
public ComboboxTest(){
Container container = this.getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem(null);
comboBox.addItem("正在热映");
comboBox.addItem("即将上映");
comboBox.addItem("已下架");
container.add(comboBox);
this.setVisible(true);
this.setBounds(200,200,300,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ComboboxTest();
}
}
JList
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class ListTest extends JFrame{
public ListTest(){
Container container = this.getContentPane();
//生成列表的内容
//String[] contents = {"1","2","3","4","5"}; //数组的静态初始化
Vector contents = new Vector(); //动态添加内容
contents.add("1");
contents.add("2");
contents.add("3");
contents.add("4");
contents.add("5");
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setBounds(200,200,300,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ListTest();
}
}
JTextField
import javax.swing.*;
import java.awt.*;
public class TextTest extends JFrame {
public TextTest(){
Container container = this.getContentPane();
JTextField field1 = new JTextField("Hello");
JTextField field2 = new JTextField("World");
container.add(field1, BorderLayout.NORTH);
container.add(field2, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(300,300,500,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextTest();
}
}
JPasswordField
import javax.swing.*;
import java.awt.*;
public class PasswordFieldTest extends JFrame {
public PasswordFieldTest(){
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setBounds(300,300,500,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new PasswordFieldTest();
}
}
JTextArea
import javax.swing.*;
import java.awt.*;
public class JScrollPanelTest extends JFrame {
public JScrollPanelTest(){
Container container = getContentPane();
//文本域
JTextArea textArea = new JTextArea();
textArea.setText("这是一个文本域");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(200,300,300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPanelTest();
}
}