gpt4 book ai didi

java - 带大圆圈的单选按钮组 Java

转载 作者:行者123 更新时间:2023-12-01 17:02:23 25 4
gpt4 key购买 nike

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

工作机制与单选按钮组完全相同,只是我们需要有更大的圆圈。知道我们如何做到这一点吗?

最佳答案

  1. 使用 JRadioButton
  2. 但不要给他们任何文字(如果这是一项要求,......这可能不是您的要求,我不知道)。
  3. 而是给他们两个 ImageIcons,1 表示未选中,这是一个空圆圈,然后使用 setIcon(Icon icon) 来执行此操作。
  4. 另一个用于选择的是实心圆的图像,并使用 setSelectedIcon(Icon icon) 来执行此操作。
  5. 您可以通过在 BufferedImage 上绘图来轻松创建自己的图像。
<小时/>

例如,下面的代码创建:

enter image description here ...... ...... animated version

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

关于java - 带大圆圈的单选按钮组 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26832832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com