java - 将 JRadioButton 添加到图形对象

标签 java swing graphics jpanel jradiobutton

我有一个现有的图形对象,我正在尝试在其顶部添加一个 JRadioButton。程序运行后,按钮不会显示,我认为这是因为没有办法将 JPanel 添加到 Graphics 对象。我将 JRadiobutton 添加到其相应的 ButtonGroup,然后将按钮添加到 JPanel,但我还没有看到任何在图形对象顶部添加按钮的方法。

有没有办法向图形对象添加单选按钮?继续使用这个图形对象很重要。让我知道查看代码是否有帮助,我想我只是需要一种更好的方法来解决这个问题。

private void redrawTitle(Graphics gc) {
    gc.setColor(Color.yellow);
    gc.fillRect(0, 0, view_width, view_height);
    gc.setFont(largeBannerFont);
    FontMetrics fm = gc.getFontMetrics();
    gc.setColor(Color.red);
    centerString(gc, fm, "Start", 100);
    gc.setColor(Color.blue);
    gc.setFont(smallBannerFont);
    fm = gc.getFontMetrics();
    centerString(gc, fm, "by DavidVee", 160);
    centerString(gc, fm, "a;lskdf", 190);
    gc.setColor(Color.black);
    centerString(gc, fm, "To start, select a skill level.", 250);

    JRadioButton kruskalButton = new JRadioButton("Kruskal");
    ButtonGroup group = new ButtonGroup();
    group.add(kruskalButton);
    JPanel panel = new JPanel();
    panel.add(kruskalButton);

    centerString(gc, fm, "(Press a number from 0 to 9,", 300);
    centerString(gc, fm, "or a letter from A to F)", 320);
    centerString(gc, fm, "v1.2", 350);
}

最佳答案

此方法会将组件“绘制”到提供的图形上下文上,它只不过是组件的“橡皮图章”/“快照”,不可能进行交互(无需您自己编码)...

public class PaintControls {

    public static void main(String[] args) {
        new PaintControls();
    }

    public PaintControls() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintPane());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        private JRadioButton radioButton = new JRadioButton("I'm painted...");

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();

            Dimension dim = radioButton.getPreferredSize();
            int x = (getWidth() - dim.width) / 2;
            int y = (getHeight() - dim.height) / 2;

            radioButton.setBounds(x, y, dim.width, dim.height);

            g2d.translate(x, y);
            radioButton.printAll(g);
            g2d.translate(-x, -y);

            g2d.dispose();
        }
    }
}

要将新内容添加到父 Pane ,请使用容器的“add”方法,但是,不要在 paintXxx 方法中执行此操作...

关于java - 将 JRadioButton 添加到图形对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12888364/

相关文章:

java - 同一个 tomcat 实例上的多个 Web 应用程序是好是坏?

java - logback 每个记录器配置不起作用

c++ - 什么时候画画合适?

java - 在两个 Activity 中加载 'raw' MediaPlayer 资源时出现空点异常

java - 当参数为零时,如何通过模拟编写单元测试 :constructors

java - 复合 JTree 节点允许事件传递到下面的对象

java - 图形未绘制/出现

java - 在 netbeans 中着色 jtable 行

python - pygame - 移动图形( Actor )

algorithm - 计算RGBA色差最准确的算法是什么?