Java Swing JButton

I have created customized Button. For that i have overrided paintComponenet method. How can I set Button Text on such button? I tried doing it using drawString method. But which x,y values should i give? (g.drawString("button text",x,y)). Please till me if anyone has handled this. @Override public void paintComponent(Graphics g) { super.paintComponent(g); DefaultButtonModel bmodel = (DefaultButtonModel) super.getModel(); Image im = (new ImageIcon("image")).getImage(); System.out.println("im is "+im.getSource()); System.out.println("widthis" + im.getWidth(this)); int imageX = (getWidth() - im.getWidth(this)) /2; int imageY = (getHeight() - im.getHeight(this)) / 2; if(!super.isEnabled()) { System.out.println("in disabled"); g.drawImage(disabled, imageX, imageY, this); g.drawString( super.getText(), super.getX(),(int) (super.getY() / (1.9))); } else { if(bmodel.isPressed()) { System.out.println("in pressed"); g.drawImage(down, imageX, imageY, this); } else if(bmodel.isRollover()) { System.out.println("in roll overed"); g.drawImage(highlight, imageX, imageY, this); } else if(bmodel.isEnabled()) { System.out.println("in enabled"); g.drawImage(normal, imageX, imageY, this); } else { System.out.println("in else"); g.drawImage(normal, imageX, imageY, this); } g.drawString( super.getText(), super.getX(),(int) (super.getY() / (2.5))); } }
Why do you take so much trouble? Why don't you use an IDE (NetBeans, Eclipse etc)?

以上就是Java Swing JButton的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏