JButton图片和文字同是显示,求各位帮忙看下

黄昏在美终要天黑 2012-05-28 06:32:43
private JButton getLoginButton() {
if (loginButton == null) {
img= new ImageIcon(getClass().getResource("/image/18.png"));
loginButton = new JButton("登录",img);
loginButton.setBounds(new Rectangle(109, 114, 68, 25));
loginButton.setMargin(new Insets(2,2,2,2));

// 添加一个监听
loginButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "登录成功。");
}
});
}
return loginButton;
}


只能显示图片,不能显示文字
...全文
401 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ykd359103923 2013-11-16
  • 打赏
  • 举报
回复

JButton jbt=getJButton("xxxx.png","aa");
private JButton getJButton(String img, String text) {
	ImageIcon ico = new ImageIcon(img); // 图片 
    	JButton button = new JButton(text, ico);  
    	button.setIconTextGap(2);  
    	button.setHorizontalTextPosition(JButton.CENTER); 
    	button.setVerticalTextPosition(JButton.BOTTOM);  
    	return button; 		
			}
虽然时间比较久了.不过看到还是回复下.这是图片在上面文字在下面的
J帝 2013-10-04
  • 打赏
  • 举报
回复
我有同样的问题
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

可以显示啊,只是图片和文字排列显示
[/Quote]

怎么排列 给个例子
古布 2012-05-28
  • 打赏
  • 举报
回复
下面两行删了,就能显示。应该是边距大小的问题
loginButton.setBounds(new Rectangle(109, 114, 68, 25));
loginButton.setMargin(new Insets(2,2,2,2));
古市轩 2012-05-28
  • 打赏
  • 举报
回复
可以显示啊,只是图片和文字排列显示

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧