JFrame,JDialog,JScroll,文本框密码框文本域

接着上篇的内容继续 JFrame

1.JFrame

public class demo extends JFrame {
    public demo(){
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        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 mydemo();
            }
        });

        container.add(button);

    }
    public static void main(String[] args) {
        new demo();
    }
}
class mydemo extends JDialog{
    public mydemo(){
        this.setVisible(true);
        this.setBounds(100,100,500,500);

        Container container = this.getContentPane();
        container.setLayout(null);

        container.add(new Label("杨凯波宇宙无敌超级帅"));
    }
}

在这里插入图片描述

2.Icon标签


public class demo2 extends JFrame implements  Icon {
    private int width;
    private int heigh;

    public demo2(){}
    public demo2(int width,int heigh){
        this.width=width;
        this.heigh=heigh;
    }

    public void init(){
        demo2 demo2 = new demo2(15, 15);
        JLabel label = new JLabel("demo2", demo2, SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new demo2().init();

    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {

    }

    @Override
    public int getIconWidth() {
        return 0;
    }

    @Override
    public int getIconHeight() {
        return 0;
    }
}

在这里插入图片描述

3.在窗体里添加图片

public class demo3 extends JFrame {
    public demo3(){
        JLabel label = new JLabel("demo3");
        URL url = demo3.class.getResource("tx.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 demo3();
    }
}

4.JFrame对应的JLabel

public class demo4 {
    public void init(){
        JFrame jFrame = new JFrame("这是一个窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(100,100,200,200);
        jFrame.setBackground(Color.cyan);
        JLabel label = new JLabel("欢迎光临");
        jFrame.add(label);

        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        new demo4().init();
    }
}

在这里插入图片描述

5.JFrame对应的JButton

public class demo2 extends JFrame {
    public demo2(){
        Container container = this.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(3,2));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new demo2();
    }
}

在这里插入图片描述

发布了43 篇原创文章 · 获赞 7 · 访问量 1768

猜你喜欢

转载自blog.csdn.net/y18791050779/article/details/103404054