1

我希望我的 JToggleButton 在它被选中时不要重新绘制。我用一对单词(“check/next”)表示状态变化。标准行为是蓝光,但我想禁用它。

4

2 回答 2

3

见: AbstractButton.setContentAreaFilled(false)

但请注意,用户通常更喜欢遵循“最不意外路径”的 GUI 元素。这种类型的渲染可能更好地描述为在路径旁边的灌木丛中发生一点碰撞。

于 2012-09-01T12:27:06.530 回答
3

也许您可以在 ImageIcons 上显示这些文字。例如:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class ToggleFun {
   private static final Color BACKGROUND_COLOR = new Color(200, 200, 255);

   public static void main(String[] args) {
      int biWidth = 60;
      int biHeight = 30;
      BufferedImage checkImg = new BufferedImage(biWidth, biHeight, BufferedImage.TYPE_INT_RGB);
      BufferedImage nextImg = new BufferedImage(biWidth, biHeight, BufferedImage.TYPE_INT_RGB);

      Graphics2D g2 = checkImg.createGraphics();
      g2.setColor(BACKGROUND_COLOR);
      g2.fillRect(0, 0, biWidth, biHeight);
      g2.setColor(Color.black);
      g2.drawString("Check", 10, 20);
      g2.dispose();

      g2 = nextImg.createGraphics();
      g2.setColor(BACKGROUND_COLOR);
      g2.fillRect(0, 0, biWidth, biHeight);
      g2.setColor(Color.black);
      g2.drawString("Next", 15, 20);
      g2.dispose();

      ImageIcon checkIcon = new ImageIcon(checkImg);
      ImageIcon nextIcon = new ImageIcon(nextImg);

      JToggleButton toggleBtn = new JToggleButton(checkIcon);
      toggleBtn.setSelectedIcon(nextIcon);
      toggleBtn.setContentAreaFilled(false);
      toggleBtn.setBorder(BorderFactory.createLineBorder(Color.black));

      JPanel panel = new JPanel();
      panel.add(toggleBtn);
      JOptionPane.showMessageDialog(null, panel);

   }
}
于 2012-09-01T12:08:14.877 回答