swing 比如 一个JButton 如何让这个button的文本信息显示在添加的图片上,而不是图片的旁边 如题,求解,先谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 此回复为自动发出,仅用于显示而已,并无任何其他特殊作用楼主【qiumomo】截止到2008-07-17 22:51:21的历史汇总数据(不包括此帖):发帖的总数量:1 发帖的总分数:0 每贴平均分数:0 回帖的总数量:0 得分贴总数量:0 回帖的得分率:0% 结贴的总数量:0 结贴的总分数:0 无满意结贴数:0 无满意结贴分:0 未结的帖子数:1 未结的总分数:0 结贴的百分比:0.00 % 结分的百分比:---------------------无满意结贴率:---------------------无满意结分率:---------------------如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html 重写其public void paintComponents(Graphics g)方法,自己画 给你个现成的吧,没写注释,自己慢慢研究import java.awt.Color;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.event.HierarchyBoundsListener;import java.awt.event.HierarchyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.Action;import javax.swing.BorderFactory;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JButton;public class BButton extends JButton implements MouseListener, HierarchyBoundsListener{ private static final long serialVersionUID = -5980379874450229166L; private int rim = 2; private int alpha = 3; protected Icon currentIcon; protected boolean isEnter = false; protected boolean isPressed = false; private boolean defaultButton = false; public BButton() { this("", null); } public BButton(Icon icon) { this("", icon); } public BButton(String text) { this(text, null); } public BButton(String text, Icon icon) { super(text, icon); init(); } public BButton(Action action) { super(action); init(); } private void init() { this.addMouseListener(this); this.addHierarchyBoundsListener(this); this.setBorder(BorderFactory.createEmptyBorder()); } public void setIcon(Icon icon) { super.setIcon(icon); this.currentIcon = icon; } public boolean isDefaultButton() { return defaultButton; } public void setDefaultButton(boolean defaultButton) { this.defaultButton = defaultButton; } protected void paintComponent(Graphics g) { String text = this.getText(); Icon pressedIcon = this.getPressedIcon(); if(currentIcon != null) { g.drawImage(((ImageIcon)currentIcon).getImage(), 0, 0, this.getWidth(), this.getHeight(), this); } if(text != null && !text.trim().equals("")) { FontMetrics fm = getFontMetrics(getFont()); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = (getHeight() - fm.getHeight()) / 2 + 11; x = currentIcon == pressedIcon? x + 1: x; y = currentIcon == pressedIcon? y + 1: y; g.setFont(getFont()); g.setColor(isEnabled()? new Color(17, 64, 96): Color.GRAY); g.drawString(text, x, y); } if(isDefaultButton() && currentIcon != pressedIcon && isEnabled()) { g.setColor(new Color(235, 249, 8)); g.drawRoundRect(rim, rim, getWidth() - rim * 2, getHeight() - rim * 2, alpha, alpha); } } public void setEnabled(boolean b) { if(!b) { isEnter = false; isPressed = false; currentIcon = getIcon(); } super.setEnabled(b); } public void mouseEntered(MouseEvent e) { if(isEnabled()) { isEnter = true; currentIcon = isPressed? getPressedIcon(): getRolloverIcon(); } } public void mouseExited(MouseEvent e) { if(isEnabled()) { isEnter = false; currentIcon = isPressed? getPressedIcon(): getIcon(); repaint(); } } public void mousePressed(MouseEvent e) { if(isEnabled()) { isPressed = true; currentIcon = getPressedIcon(); } } public void mouseReleased(MouseEvent e) { if(isEnabled()) { isPressed = false; currentIcon = isEnter? getRolloverIcon(): getIcon(); } } public void ancestorMoved(HierarchyEvent e) { this.repaint(); } public void ancestorResized(HierarchyEvent e) {} public void mouseClicked(MouseEvent e) {}} 狂三对swing的研究真透彻啊,代码已经写的不能再清楚了,楼主,可以结贴散分了 哈哈,这个帖子没分的~~~~~~~~~~~~Working awesome and behaving low profile! 提取网页js的正则怎么写 Java 版本混乱 java问题,编译时不能带包? 编译问题 我在JAVA的UI上做了新尝试,不要再说JAVA做不出好看的界面了 散分!!!!!Java图形高手请进,在线等!!!!! 谁有这样的图标? 学Java:什么时候适合开始看源代码 急!求教关于库的jbuilder6中库的路径的问题! 请教:(static)静态变量和静态方法是什么意思? java与数据库 一段代码的小问题不懂,拿出来问一下大家
楼主【qiumomo】截止到2008-07-17 22:51:21的历史汇总数据(不包括此帖):
发帖的总数量:1 发帖的总分数:0 每贴平均分数:0
回帖的总数量:0 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:1 未结的总分数:0
结贴的百分比:0.00 % 结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;public class BButton extends JButton implements MouseListener, HierarchyBoundsListener
{
private static final long serialVersionUID = -5980379874450229166L;
private int rim = 2;
private int alpha = 3;
protected Icon currentIcon;
protected boolean isEnter = false;
protected boolean isPressed = false;
private boolean defaultButton = false;
public BButton()
{
this("", null);
}
public BButton(Icon icon)
{
this("", icon);
}
public BButton(String text)
{
this(text, null);
}
public BButton(String text, Icon icon)
{
super(text, icon);
init();
}
public BButton(Action action)
{
super(action);
init();
}
private void init()
{
this.addMouseListener(this);
this.addHierarchyBoundsListener(this);
this.setBorder(BorderFactory.createEmptyBorder());
}
public void setIcon(Icon icon)
{
super.setIcon(icon);
this.currentIcon = icon;
}
public boolean isDefaultButton()
{
return defaultButton;
} public void setDefaultButton(boolean defaultButton)
{
this.defaultButton = defaultButton;
} protected void paintComponent(Graphics g)
{
String text = this.getText();
Icon pressedIcon = this.getPressedIcon();
if(currentIcon != null)
{
g.drawImage(((ImageIcon)currentIcon).getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
}
if(text != null && !text.trim().equals(""))
{
FontMetrics fm = getFontMetrics(getFont());
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = (getHeight() - fm.getHeight()) / 2 + 11;
x = currentIcon == pressedIcon? x + 1: x;
y = currentIcon == pressedIcon? y + 1: y;
g.setFont(getFont());
g.setColor(isEnabled()? new Color(17, 64, 96): Color.GRAY);
g.drawString(text, x, y);
}
if(isDefaultButton() && currentIcon != pressedIcon && isEnabled())
{
g.setColor(new Color(235, 249, 8));
g.drawRoundRect(rim, rim, getWidth() - rim * 2, getHeight() - rim * 2, alpha, alpha);
}
}
public void setEnabled(boolean b)
{
if(!b)
{
isEnter = false;
isPressed = false;
currentIcon = getIcon();
}
super.setEnabled(b);
}
public void mouseEntered(MouseEvent e)
{
if(isEnabled())
{
isEnter = true;
currentIcon = isPressed? getPressedIcon(): getRolloverIcon();
}
} public void mouseExited(MouseEvent e)
{
if(isEnabled())
{
isEnter = false;
currentIcon = isPressed? getPressedIcon(): getIcon();
repaint();
}
} public void mousePressed(MouseEvent e)
{
if(isEnabled())
{
isPressed = true;
currentIcon = getPressedIcon();
}
} public void mouseReleased(MouseEvent e)
{
if(isEnabled())
{
isPressed = false;
currentIcon = isEnter? getRolloverIcon(): getIcon();
}
} public void ancestorMoved(HierarchyEvent e)
{
this.repaint();
} public void ancestorResized(HierarchyEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
}