Swing基础语法2

画图像图标

创建一个类实现Icon类
重写Icon的方法
画图像
@Override
public void paintIcon(Component c, Graphics g, int x, int y){
在方法体中调用g.方法可以画出不同的图像}
获取图像的宽度
@Override
public int getIconWidth()
获取图像的高度
@Override
public int getIconHeight()

  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("icno", iconDemo, SwingConstants.CENTER);
        Container content = getContentPane();
        content.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    @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;
    }
    public static void main(String[] args) {
        new IconDemo().init();
    }
}

图片图标的设置方法与jButton中设置图片南牛的方法类似


文本框 文本域

public class TestFieldDemo extends JFrame {
    public TestFieldDemo() {
        Container container = this.getContentPane();

        JTextField textField1 = new JTextField("文本框1");
        JTextField textField2 = new JTextField("文本框2");

        container.add(textField1,BorderLayout.EAST);
        container.add(textField2,BorderLayout.WEST);

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

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

文本域若超出范围会显示活动的条 与面板的活动条类似

public class TextAreaDemo extends JFrame {
    public  TextAreaDemo() {
        Container container = this.getContentPane();

        TextArea textArea = new TextArea(20,20);
        textArea.setText("文本域");

        JScrollPane jScrollPane = new JScrollPane(textArea);

        container.add(jScrollPane);

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

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

密码 后台显示密码 前台密码的显示用*(或其他自定义符号)

public class PassWordTest extends JFrame{
    public PassWordTest() {
        Container container = this.getContentPane();

        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar('*');//输入的内容界面上显示为*

        container.add(jPasswordField);

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

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

弹窗

设置监听器 监听某个按钮
再自定义一个类继承JDialog类(这个类就是弹窗类)
JDialog的子类中设置的标签的背景颜色可见
需要调用setOpaque(true)方法

public class Diolog01 extends JFrame {
    public Diolog01(){
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        //设置按钮
        JButton jButton = new JButton("创建一个弹窗");
        jButton.setBounds(30,30,200,20);//绝对定位后不用用add方法设置布局 setBounds方法设置位置 大小是相对Frame设置的
        //点击按钮时弹出弹窗
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
        container.add(jButton);

    }
    //按钮
    JButton button = new JButton("弹窗按钮");

    public static void main(String[] args) {
        new Diolog01();
    }
}
class MyDialog extends JDialog{
    public MyDialog(){
        this.setVisible(true);
        this.setBounds(200,200,400,400);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 会报错 不用写关闭操作 因为弹窗自带可以关闭
        Container container=this.getContentPane();
        container.setLayout(null);
        container.setBackground(Color.red);
        JLabel jLabel=new JLabel("新窗口");
        jLabel.setBackground(Color.CYAN);
        jLabel.setOpaque(true);//设置标签的背景颜色可见
        jLabel.setSize(100,100);
        container.add(jLabel);
    }
}

下拉框 列表框

public class ComboboxDemo01 extends JFrame {
    public ComboboxDemo01() {
        Container container = this.getContentPane();

        //创建一个面板存放下拉框
        JPanel jPanel = new JPanel();

        //下拉框
        JComboBox box = new JComboBox();
        box.addItem("正在热映");
        box.addItem("已下架");
        box.addItem("即将上映");

        jPanel.add(box);
        container.add(jPanel);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(400,400,400,400);
    }

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

/*
列表框,展示信息
 */
public class ComboboxDemo02 extends JFrame{
    public ComboboxDemo02() {
        Container container = this.getContentPane();

        //创建一个面板存放类表框
        JPanel jPanel = new JPanel();

        //创建一个数组存放要显示的数据
        String[] s={"1","2","3"};

        JList list = new JList(s);
        jPanel.add(list);

        container.add(jPanel);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(400,400,400,400);
    }

    public static void main(String[] args) {
        new ComboboxDemo02();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值