不想学习的小狐狸

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

窗口面板

import javax.swing.*;
import java.awt.*;

public class JFrameDemo {
    public void init(){
        //顶级窗口
       JFrame jf= new JFrame("这是一个JFrame窗口");
       jf.setVisible(true);
       jf.setBounds(100,100,200,200);
       jf.setBackground(Color.blue);
       //设置文字
        JLabel label=new JLabel("哈哈哈");
        jf.add(label);
        //容器实例化

       //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}
import javax.swing.*;
import java.awt.*;

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJframe2().init();
    }
}
class MyJframe2 extends JFrame{
    public void init(){
        this.setVisible(true);
        this.setBounds(10,10,200,300);
        JLabel label=new JLabel("哈哈哈");
        this.add(label);
        //让文本标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.blue);
    }
}

弹窗

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(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JFrame 放东西,容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        //按钮
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(30,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);
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new Label("gggggg"));
    }
}

标签

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 iconDemo=new IconDemo(15,15);
        //图标放在标签、按钮上
        JLabel label=new JLabel("icontest",iconDemo,SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        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;
    }


}
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//图标Icon
public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("test.jpg");
        ImageIcon imageIcon=new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,200,200);
    }

    public static void main(String[] args) {
       new ImageIconDemo();
    }
}

面板

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
    public JPanelDemo()  {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));//后面两个参数是间距
        JPanel panel1 = new JPanel(new GridLayout(1,3));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        container.add(panel1);
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JPanelDemo();
    }
}
import javax.swing.*;
import java.awt.*;
//滚动面板
public class JScrollDemo extends JFrame {
    public JScrollDemo()  {
        Container container = this.getContentPane();
        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("我的笔记");
        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);
        this.setVisible(true);
        this.setBounds(100,100,300,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

图片按钮、单选框、多选框

import javax.swing.*;
import java.awt.*;
import java.net.URL;
//图片按钮
public class JButtonDemo01 extends JFrame {
    public JButtonDemo01()  {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo01.class.getResource("test2.jpg");
        Icon icon = new ImageIcon(resource);
        //把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        container.add(button);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
       new JButtonDemo01();
    }
}
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//单选按钮
public class JButtonDemo02 extends JFrame {
    public JButtonDemo02()  {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo02.class.getResource("test2.jpg");
        Icon icon = new ImageIcon(resource);
        //单选框
        JRadioButton radioButton01 = new JRadioButton("RadioButton01");
        JRadioButton radioButton02 = new JRadioButton("RadioButton02");
        JRadioButton radioButton03 = new JRadioButton("RadioButton03");
        //由于单选框只能选一个,分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton01);
        group.add(radioButton02);
        group.add(radioButton03);
        container.add(radioButton01,BorderLayout.CENTER);
        container.add(radioButton02,BorderLayout.NORTH);
        container.add(radioButton03,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
       new JButtonDemo02();
    }
}
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//多选框
public class JButtonDemo03 extends JFrame {
    public JButtonDemo03()  {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo03.class.getResource("test2.jpg");
        Icon icon = new ImageIcon(resource);
        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.SOUTH);
        
        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
       new JButtonDemo03();
    }
}

列表

import javax.swing.*;
import java.awt.*;
//下拉框
public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01()  {
        Container container = this.getContentPane();
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正在热映");
        status.addItem("已下架");
        status.addItem("即将上映");
        container.add(status);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
      new TestComboboxDemo01();
    }
}
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
//列表框
public class TestComboboxDemo02 extends JFrame {
    public TestComboboxDemo02()  {
        Container container = this.getContentPane();
        //生成列表的内容
        //String[] contents = {"1","2","3"};
        Vector contents = new Vector();
        //列表中需要内容
        JList jList = new JList(contents);
        contents.add("xiaohong");
        contents.add("xiaoming");
        contents.add("xiaohuang");
        container.add(jList);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
      new TestComboboxDemo02();
    }
}

应用场景:

下拉框:选择地区或者一些单个选项;

列表:展示信息,一般是动态扩容的

文本框、文本域、密码框

import javax.swing.*;
import java.awt.*;
//文本框
public class TestTextDemo01 extends JFrame {
    public TestTextDemo01()  {
        Container container = this.getContentPane();
        JTextField textField = new JTextField("hello");
        JTextField textField1 = new JTextField("world",20);
        container.add(textField,BorderLayout.NORTH);
        container.add(textField1,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}
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(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}
posted on 2021-05-18 19:54  一只小狐狸kis  阅读(39)  评论(0编辑  收藏  举报