首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >具有大圆圈Java的无线按钮群

具有大圆圈Java的无线按钮群
EN

Stack Overflow用户
提问于 2014-11-09 20:19:50
回答 1查看 1.1K关注 0票数 0

对于Java Swing中的应用程序(在netbeans中开发),我们需要创建一个很大的圆圈,就像单选按钮一样,这意味着我们有一组圆圈,每当用户单击其中一个圆圈时,它就会变成一个填充的圆圈。用户只能选择一个圆圈。

其工作机理与无线电按钮组完全相似,只需有较大的圆圈即可。知道我们该怎么做吗?

EN

Stack Overflow用户

发布于 2014-11-09 20:41:49

  1. 使用JRadioButtons
  2. 但是不要给他们发短信(如果这是要求的话,.这可能不是你的要求,我不知道)。
  3. 相反,给他们两个ImageIcons,1表示未选择的(这是一个空的圆圈),并使用setIcon(Icon icon)来完成这个任务。
  4. 另一个用于选择,这是一个填充的圆圈的图像,并使用setSelectedIcon(Icon icon)进行此操作。
  5. 您可以通过在BufferedImage上绘图轻松地创建自己的图像。

例如,下面的代码创建:

代码语言:javascript
运行
复制
.....   ...... 

代码语言:javascript
运行
复制
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class CircleIconEg extends JPanel {
   public static final String[] PLAYER_NAMES = {"John", "Bill", "Frank", "Andy"};
   private static final int BI_WIDTH = 40;
   private ButtonGroup btnGrp = new ButtonGroup();
   private static Icon emptyIcon;
   private static Icon selectedIcon;

   // create our Circle ImageIcons
   static {
      // first the empty circle
      BufferedImage img = new BufferedImage(BI_WIDTH, BI_WIDTH, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = img.createGraphics();
      g2.setStroke(new BasicStroke(4f));
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      int x = 4;
      int y = x;
      int width = BI_WIDTH - 2 * x;
      int height = width;
      g2.setColor(Color.black);
      g2.drawOval(x, y, width, height);
      g2.dispose();

      emptyIcon = new ImageIcon(img);

      // next the filled circle
      img = new BufferedImage(BI_WIDTH, BI_WIDTH, BufferedImage.TYPE_INT_ARGB);
      g2 = img.createGraphics();
      g2.setStroke(new BasicStroke(4f));
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

      g2.setColor(Color.red);
      g2.fillOval(x, y, width, height);
      g2.setColor(Color.black);
      g2.drawOval(x, y, width, height);
      g2.dispose();

      selectedIcon = new ImageIcon(img);
   }

   public CircleIconEg() {
      setLayout(new GridLayout(0, 1, 0, 4));
      for (String playerName : PLAYER_NAMES) {
         JRadioButton radioBtn = createRadioButton(playerName);
         btnGrp.add(radioBtn);;
         add(radioBtn);
      }
   }

   private JRadioButton createRadioButton(String playerName) {
      JRadioButton rBtn = new JRadioButton(playerName, emptyIcon);
      rBtn.setSelectedIcon(selectedIcon);
      return rBtn;
   }

   private static void createAndShowGui() {
      CircleIconEg mainPanel = new CircleIconEg();

      JFrame frame = new JFrame("CircleIconEg");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
票数 5
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26832832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档