作者janyfor (你哪位ㄚ)
看板java
标题Re: [问题] JButton getGraphics
时间Sat Apr 26 03:26:43 2008
※ 引述《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