※ 引述《lovesneakers (sneakers)》之銘言:
: 推 willieliao:err..有一點搞不懂你的意思,可不可以說一下你要這樣的 04/25 04:44
: 推 willieliao:目的是? 04/25 04:46
: 我想這麼做的目的是
: JButton 上面的 Graphics 想由別隻程式來提供
: 假如我想產生一個 button 上面畫了魚
: 或是想產生一個 button 上面畫了圓形之類的
用你之前給的程式改的,NewButton 是需要的 button,
type 是需要的圖形樣式,當然你可以自己改成其他你想要的格式。
不知道這樣是不是可以滿足你的需求?
public class NewBtn extends JFrame {
public NewBtn(){
setSize(200, 200);
setLayout(new GridLayout(2,1));
JButton btn = NewJButton(0);
JButton btn2 = NewJButton(1);
add(new JPanel().add(btn));
add(new JPanel().add(btn2));
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new NewBtn();
}
/* 應該不需要註解吧... */
private JButton NewJButton(int temp){
final int type = temp;
return new JButton(){
public void paintComponent(Graphics g){
super.paintComponent(g);
setG(g , type);
}
public void setG(Graphics g , int type){
if(type == 0){
g.setColor(Color.BLUE);
g.fillOval(0, 0, 100, 100);
}else{
g.setColor(Color.RED);
g.fillOval(0, 0, 100, 100);
}
}
};
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.209.196.59