swing

swing

标签居中

public class JFrameDemo {
    //init();初始化
    public void init(){
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setBounds(200,100,200,100);
        frame.setVisible(true);
        //添加文字 Jlabel
        JLabel label = new JLabel("欢迎来到对抗路");
        frame.add(label);
        //让文本居中 设置水平对齐
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container container = frame.getContentPane();
        container.setBackground(Color.lightGray);
        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JFrameDemo().init();
​
    }
}

弹窗

JDialog 用来被弹出,默认就有关闭事件

public class DialogDemo extends JFrame {
    public DialogDemo(){
        this.setVisible(true);
        this.setSize(200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //一个容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        //按钮
        JButton button = new JButton("这是一个弹窗");
        button.setBounds(100,100,220,100);
        //写一个监听事件,点击这个按钮就会出现一个弹窗
        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.setBounds(10,20,30,40);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new Label("学习java"));
    }
}

标签

new JLabel("xxx");

图标 Icon

public class IconDemo extends JFrame implements Icon {
    public IconDemo(){};//无参构造
    private int width;
    private int height;
    public void  init(){
        IconDemo iconDemo = new IconDemo(15, 15);
        //图标放在标签上,也可以放在按钮上
        JLabel label = new JLabel("图标", iconDemo, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public IconDemo(int width,int height){
        this.width=width;
        this.height=height;
    }
    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;
    }
}

图片Icon

public class ImageIconDemo extends JFrame {
    public ImageIconDemo()  {
        //获取图片地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("x.JPG");
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        setVisible(true);
        setBounds(100,100,200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new ImageIconDemo();
    }
}

面板

public class TestJpanel extends JFrame {
    public TestJpanel(){
        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(2,3));
        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"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        container.add(panel1);
        container.add(panel2);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,200,200,200);
​
    }
​
    public static void main(String[] args) {
     new TestJpanel();
    }
​
}

JScrollPanel

public class TestJScroll extends JFrame {
    public TestJScroll(){
        Container container = this.getContentPane();
        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎学习java");
​
        //要把文本域放到滚动面板上
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);
        
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,200,200);
    }
​
    public static void main(String[] args) {
        new TestJScroll();
    }
}

图片按钮ImageIcon

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        Container container = this.getContentPane();
        //将一个图片变成图标
        URL resource = JButtonDemo01.class.getResource("x.JPG");
        Icon icon=new ImageIcon(resource);
        //把图标放在一个按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        //add
        container.add(button);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(100,200);
​
​
    }
    public static void main(String[] args) {
    new JButtonDemo01();
    }
​
}

单选按钮JRadioButton

public class JFrameDemo02 extends JFrame {
    public JFrameDemo02(){
        Container container = this.getContentPane();
        //将一个图片变成图标
        URL resource = JButtonDemo01.class.getResource("x.JPG");
        Icon icon=new ImageIcon(resource);
        JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
        //由于单选框只能选一个,所以分组,且一个组只能选一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);
        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.SOUTH);
        container.add(radioButton3,BorderLayout.NORTH);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(100,200);
​
    }
​
    public static void main(String[] args) {
        new JFrameDemo02();
    }
​
}

复选按钮 JCheckBox

public class JFrameDemo02 extends JFrame {
    public JFrameDemo02(){
        Container container = this.getContentPane();
        //将一个图片变成图标
        URL resource = JButtonDemo01.class.getResource("x.JPG");
        Icon icon=new ImageIcon(resource);
​
        JCheckBox checkBox1 = new JCheckBox("checkBox1");
        JCheckBox checkBox2 = new JCheckBox("checkBox2");
        container.add(checkBox1,BorderLayout.WEST);
        container.add(checkBox2,BorderLayout.EAST);
        
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(100,200);
​
    }
​
    public static void main(String[] args) {
        new JFrameDemo02();
    }
​
}

列表

下拉框 JComboBox

public class JFrameDemo02 extends JFrame {
    public JFrameDemo02(){
        Container container = this.getContentPane();
        JComboBox status  = new JComboBox<>();
        status.addItem(null);
        status.addItem("正在上映");
        status.addItem("即将");
        status.addItem("下架");
        container.add(status);;
        
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(100,200);
    }
​
    public static void main(String[] args) {
        new JFrameDemo02();
    }
​
}

列表框

public class JFrameDemo02 extends JFrame {
    public JFrameDemo02(){
        Container container = this.getContentPane();
        //生成列表的内容
        String[] contents ={"1","2","3"};   或者 //Vector contents = new Vector();
        //将内容放入列表中
        JList  list = new JList(contents);
        //contents.add("dsfdd");
        //contents.add("csgvasf");
        
        container.add(list);
        
​
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(100,200);
    }
    public static void main(String[] args) {
        new JFrameDemo02();
    }
​
}

文本框 JTextfiled

密码框 JPasswordField

public class TestTextDemo01 extends JFrame {
    public  TestTextDemo01 (){
        Container container = this.getContentPane();
        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');
        container.add(passwordField);
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(200,200);
​
    }
​
    public static void main(String[] args) {
     new TestTextDemo01();
    }
}
贪吃蛇

帧 如果时间片足够小,就是动画。 连起来就是动画,拆开就是静态图片

键盘监听

定时器 Timer

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值