【问题标题】:Adding a JRadioButton to a Graphics Object将 JRadioButton 添加到图形对象
【发布时间】:2012-10-15 01:58:21
【问题描述】:

我有一个现有的图形对象,我正在尝试在它上面添加一个 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);
}

【问题讨论】:

  • 我刚刚在编辑中为您添加了它。
  • 这样做意味着您将获得单选按钮的“快照”,它不会是一个工作组件。这不是通常将组件添加到容器中的方式

标签: java swing graphics jpanel jradiobutton


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢。这将创建一个位于单独窗口中的按钮。有没有办法创建一个实际上是由图形创建的屏幕的一部分的按钮?或者这不是图形的使用方式?
  • 仅为每个顶级容器创建一个图形上下文。你想做什么?
  • 我正在尝试在由图形对象生成的屏幕中添加一个按钮(最好是单选按钮)。
  • 所以,从我展示的paintComponent 中获取代码并在您的代码中使用它来呈现它。诀窍是,您需要告诉组件它应该是什么大小以及需要在哪里渲染。
  • 抱歉,我对 Java 和 GUI 还很陌生。我不确定如何使用此代码。我尝试在我的代码中使用您的 paintComponent 方法(在将整个方法放入我的 redrawTitle() 方法所在的同一个类之后),但它说“paintComponent 对于图形类型是未定义的。”
猜你喜欢
  • 2018-02-19
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
相关资源
最近更新 更多