簡體   English   中英

帶有大圓圈 Java 的單選按鈕組

[英]Radio Button Group With Big Circles Java

對於 Java Swing 中的應用程序(在 netbeans 中開發),我們需要像單選按鈕一樣創建大圓圈,這意味着我們有一組圓圈,每當用戶單擊一個圓圈時,它就會變為實心圓圈。 用戶只能選擇 1 個圓圈。

工作機制與單選按鈕組完全相同,只是我們需要有更大的圓圈。 知道我們該怎么做嗎?

  1. 使用 JRadioButtons
  2. 但是不要給他們任何文字(如果這是一個要求,....可能不是你的要求,我不知道)。
  3. 取而代之的是給他們兩個 ImageIcon,1 表示未選中,它是一個空圓圈,並使用setIcon(Icon icon)來執行此操作。
  4. 另一個用於選中的是一個實心圓圈的圖像,並使用setSelectedIcon(Icon icon)來執行此操作。
  5. 您可以通過在 BufferedImage 上繪圖輕松創建自己的圖像。

例如,下面的代碼創建:

在此處輸入圖像描述 …………動畫版

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();
         }
      });
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM