本文介绍了Swing JToolbarButton 按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 JToolbarButton 按钮,我想让它在我点击时被按下",就像 JButton 工作一样.我该怎么做?请帮忙!谢谢.
I use JToolbarButton button, I want to make it be "pressed" when I click on it, just like JButton works.How can I do this?Please help!Thanks.
推荐答案
正如 Costis 的回复中提到的,您可能正在寻找 JToggleButton
.可能还需要抑制边框的绘制,如本示例中的第二个工具栏.
As mentioned in Costis' reply, you are probably after a JToggleButton
. It might also be necessary to suppress the painting of the border, as in the 2nd tool bar in this example.
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
class ToggleBar {
public static JToggleButton getButton(
Image selected,
Image unselected,
boolean decorated) {
JToggleButton b = new JToggleButton();
b.setSelectedIcon(new ImageIcon(selected));
b.setIcon(new ImageIcon(unselected));
b.setBorderPainted(decorated);
return b;
}
public static Image getCircleImage(Color c) {
BufferedImage bi = new BufferedImage(
32,32,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setColor(c);
g.fillOval(0,0,32,32);
g.dispose();
return bi;
}
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
Image red = getCircleImage(Color.RED);
Image green = getCircleImage(Color.GREEN);
JPanel p = new JPanel(new GridLayout(0,1));
JToolBar tb1 = new JToolBar();
for (int ii=0; ii<5; ii++) {
tb1.add( getButton(red, green, true) );
}
p.add(tb1);
JToolBar tb2 = new JToolBar();
for (int ii=0; ii<5; ii++) {
tb2.add( getButton(red, green, false) );
}
p.add(tb2);
JOptionPane.showMessageDialog(null, p);
}
});
}
}
这篇关于Swing JToolbarButton 按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!