java,在枯燥终于遇见了美丽的JFrame(Jbtton篇)

package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {
    public Main(){
        setBounds(100,100,500,300);//设置窗体坐标和大小
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container c=getContentPane();
        c.setLayout(new GridLayout(3,2,5,5));
        JButton btn[]=new JButton[6];
        for(int i=0;i<btn.length;i++){
            btn[i]=new JButton();
            c.add(btn[i]);
        }
        btn[0].setText("不可用");//设置文本
        btn[0].setEnabled(false);//设置组建不可用

        btn[1].setText("有背景色");
        btn[1].setBackground(Color.YELLOW);

        btn[2].setText("不显示边框");
        btn[2].setBorderPainted(false);//不显示边框

        btn[3].setText("有边框");
        btn[3].setBorder(BorderFactory.createLineBorder(Color.RED));//设置边框
        ImageIcon cion=new ImageIcon("src/com/company/jbutton.PNG");//获取按钮
        btn[4].setIcon(cion);//给按钮设置图片
        btn[4].setToolTipText("我 喜 欢 你");//鼠标悬停提示

        btn[5].setText("可点击");
        btn[5].addActionListener(new ActionListener() {//添加事件监听
            @Override
            public void actionPerformed(ActionEvent e) {//监听触发事件
                JOptionPane.showMessageDialog(Main.this,"我喜欢你");//弹出小对话框
            }
        });

        setVisible(true);//设置窗体为可见
    }

    public static void main(String[] args) {
        new Main();
	// write your code here
    }
}

 

 

posted @ 2018-12-10 22:31  逐梦武威  阅读(497)  评论(0编辑  收藏  举报