JAVA-JButton 窗口中添加按钮

image

package com.itheima;

import javax.swing.*;

public class JFrame03 {
    public static void main(String[] args) {
        JFrame jf=new JFrame();
        jf.setTitle("窗口中添加按钮");
        jf.setSize(400,300);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);

        jf.setLayout(null);        //取消窗体的默认布局

        //JButton(String text):创建一个带文本的按钮

        JButton button = new JButton("我是按钮");

//        //void setSize() 设置按钮大小
//        button.setSize(100,20);
//
//        button.setLocation(100,100); //设置按钮位置

        button.setBounds(100,100,100,20); //同时设置按钮大小和位置

        JButton button1=new JButton("我是按钮2");

        button1.setBounds(100,120,100,20);



        //添加按钮到窗体中
        jf.add(button);
        jf.add(button1);



        jf.setVisible(true);



    }
}

posted @ 2022-10-29 21:26  NiceTwocu  阅读(156)  评论(0编辑  收藏  举报