?? roundbuttondemo.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RoundButtonDemo extends JFrame{
private int clickCount=0; //記錄安鈕的點擊次數
private JButton button1;
private JButton button2;
public RoundButtonDemo()
{
button1 = new RoundButton("這是一個圓形按鈕"); //初始化按鈕一
Dimension dim=button1.getPreferredSize(); //得到按鈕一的最佳尺寸
double maxsize=Math.max(dim.getHeight(),dim.getWidth()); //得到長寬中的最大值
dim.setSize(maxsize,maxsize); //更改長寬為長寬中的最大值
button1.setPreferredSize(dim); //設置最佳尺寸
button2 = new RoundButton("點擊了: "+clickCount+" 次"); //初始化按鈕二
button1.setBackground(Color.blue); //設置按鈕的背景顏色
button2.setBackground(Color.pink);
getContentPane().add(button1); //增加組件
getContentPane().add(button2);
getContentPane().setLayout(new FlowLayout()); //設置布局管理器
button2.addActionListener(new ActionListener(){ //銨鈕二的事件處理
public void actionPerformed(ActionEvent e){
clickCount++; //增加一次點擊數
button2.setText("點擊了: "+clickCount+" 次"); //重新設置按鈕二的標簽
}
});
setSize(300, 200); //設置窗口尺寸
setVisible(true); //設置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉窗口時退出程序
}
public static void main(String[] args) {
new RoundButtonDemo();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -