JPanel上的JButton JPanel上的JButton,能否有方法使JButton在没有把鼠标放上去时就没有JButton在的感觉只有那上面的图标在,当放上去时就明显的看见那JButton的轮廓。就是qq聊天窗口上的那些按钮一样效果。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用Java自带的UI(例如:UIManager)试试看! o ,我刚刚没学java多久,不是很懂啊 ,能明确点不。 public class PlatButtonDemo { public static void main(String[] args) { JFrame f = new JFrame(); final Container c = f.getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new FlowLayout()); final JButton btn = new JButton("Hello"); btn.setUI(new BasicButtonUI() { protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); FontMetrics fm = SwingUtilities2.getFontMetrics(c, g); int mnemonicIndex = b.getDisplayedMnemonicIndex(); /* Draw the Text */ if (model.isEnabled()) { /*** paint the text normally */ g.setColor(c.getForeground()); SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex, textRect.x + getTextShiftOffset(), textRect.y + fm.getAscent() + getTextShiftOffset()); } else { /*** paint the text disabled ***/ g.setColor(c.getBackground().brighter()); SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex, textRect.x, textRect.y + fm.getAscent()); g.setColor(b.getBackground().darker()); SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex, textRect.x - 1, textRect.y + fm.getAscent() - 1); } } }); btn.setBackground(c.getBackground()); final Border border = btn.getBorder(); btn.setBorder(BorderFactory.createEmptyBorder()); btn.setOpaque(false); btn.addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { btn.setBorder(border); } @Override public void mouseExited(MouseEvent e) { btn.setBorder(BorderFactory.createEmptyBorder()); } }); c.add(btn); f.setSize(200, 200); f.setVisible(true); }} 可以的,是开始时设置button的setEnable(false),真正用到得时候在设置为true,即setEnable(true)你试试这样 应该可以的,LZ你要实现什么没说清楚,焦点Focuse设置。 jdbc连接数据报错 有点晕 可以用两个JSplitPane把一个Jpanel分成三栏吗?我试了好多次都不可以,还有别的方法吗?知道的告诉下 谢谢了 【请教】如何避免控件获得焦点 急-----高分求算法 如何在application程序中播放声音剪辑? 如何让某个JTextField在Frame一加载的时候就获得鼠标焦点(光标) 怎样得到Jtree选中项(要String形式)? 路过的不要错过 看看有益处 JTable如何现实出列名呀!! 动态代理的问题 有会java 单字节加密解密的吗
JFrame f = new JFrame();
final Container c = f.getContentPane();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(new FlowLayout());
final JButton btn = new JButton("Hello");
btn.setUI(new BasicButtonUI() {
protected void paintText(Graphics g, JComponent c,
Rectangle textRect, String text) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
int mnemonicIndex = b.getDisplayedMnemonicIndex(); /* Draw the Text */
if (model.isEnabled()) {
/*** paint the text normally */
g.setColor(c.getForeground());
SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
mnemonicIndex, textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset());
} else {
/*** paint the text disabled ***/
g.setColor(c.getBackground().brighter());
SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
mnemonicIndex, textRect.x, textRect.y
+ fm.getAscent());
g.setColor(b.getBackground().darker());
SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
mnemonicIndex, textRect.x - 1, textRect.y
+ fm.getAscent() - 1);
}
}
});
btn.setBackground(c.getBackground());
final Border border = btn.getBorder();
btn.setBorder(BorderFactory.createEmptyBorder());
btn.setOpaque(false); btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
btn.setBorder(border);
} @Override
public void mouseExited(MouseEvent e) {
btn.setBorder(BorderFactory.createEmptyBorder());
}
}); c.add(btn);
f.setSize(200, 200);
f.setVisible(true);
}
}
你试试这样