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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 @   逐梦武威  阅读(500)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示