其他分享
首页 > 其他分享> > GUI按钮,下拉框,列表框

GUI按钮,下拉框,列表框

作者:互联网

单选框

package com.hai.week2;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.security.acl.Group;

public class Demo15 extends JFrame {
    public Demo15(){
        Container contentPane = this.getContentPane();
        
        JRadioButton jRadioButton1 = new JRadioButton("1");
        JRadioButton jRadioButton2 = new JRadioButton("2");
        JRadioButton jRadioButton3 = new JRadioButton("3");

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);

        contentPane.add(jRadioButton1,BorderLayout.NORTH);
        contentPane.add(jRadioButton2,BorderLayout.CENTER);
        contentPane.add(jRadioButton3,BorderLayout.SOUTH);

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

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

 图片按钮

package com.hai.week2;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.security.acl.Group;

public class Demo14 extends JFrame {
    public Demo14(){
        Container contentPane = this.getContentPane();

        JButton jButton = new JButton();
        URL resource = Demo14.class.getResource("tx.jpg");
        ImageIcon imageIcon = new ImageIcon(resource);
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("图片按钮");
        contentPane.add(jButton);

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

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

  复选框

package com.hai.week2;

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

public class Demo16 extends JFrame {
    public Demo16(){
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new FlowLayout());

        Checkbox checkbox1 = new Checkbox("1");
        Checkbox checkbox2 = new Checkbox("2");
        Checkbox checkbox3 = new Checkbox("3");

        contentPane.add(checkbox1);
        contentPane.add(checkbox2);
        contentPane.add(checkbox3);

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

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

  下拉框

package com.hai.week2;

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

public class Demo17 extends JFrame {
    public Demo17(){
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new FlowLayout());

        JComboBox jComboBox = new JComboBox();

        jComboBox.addItem("");
        jComboBox.addItem("1");
        jComboBox.addItem("2");
        jComboBox.addItem("3");

        contentPane.add(jComboBox);

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

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

  列表框

package com.hai.week2;

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

public class Demo18 extends JFrame {
    public Demo18(){
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new FlowLayout());
        String str[] = {"1","2","3"};
        JList list = new JList(str);
        contentPane.add(list);

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

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

  

标签:列表框,GUI,add,public,contentPane,import,new,下拉框,500
来源: https://www.cnblogs.com/lf-ancer/p/16368995.html