【问题标题】:Draw in another component绘制另一个组件
【发布时间】:2015-07-28 06:43:47
【问题描述】:

我有作业要做,但没有足够的信息,我现在卡住了……问题是这样,我有三门课:

  • "ChoicePanel" 扩展 JPanel 并添加按钮以使用 JComboBox 选择颜色

    public class ChoicePanel extends JPanel{
    
        Draw dessin;
    
        public ChoicePanel(Draw df) {
            dessin = df;
            ...
            // couleurs
            final JComboBox<String> couleurs = new JComboBox<>(new String[] {"bleu", "jaune", "rouge"});
            add(couleurs);
        }
    
        /** Know what color is selected */
        private Color determineCouleur(int indice){
            switch (indice) {
            case 0:
                return Color.BLUE;
            case 1:
                return Color.YELLOW;
            case 2:
                return Color.RED;
            default:
                return Color.BLACK;
            }
        }
    }
    
  • “Draw”扩展 JPanel 并存储所有轮廓并绘制它们

  • “Main”创建包含这些类的框架

我必须将 Draw 设置为 MouseMotionListener,但我无法在 ChoixePanel 中选择颜色,因为 JComobox 是在构造函数中创建的,我无法将其设置为字段。 那么如何从 Draw 中检查 ChoicePanel 的按钮值呢?

每个答案都会很有帮助!

【问题讨论】:

  • 您将 Draw 传递给 ChoicePanel 并将其作为字段保存。该字段已经可以访问ChoicePanel 的变量。
  • 另外,在发布硬件问题时,最好说明您可以在代码中更改哪些内容,哪些不能更改,以及是否有不允许使用的类。
  • 对于example
  • 我不知道,那么我怎样才能从 Draw 获得 "couleurs" var 呢?对于第二条评论:我无法在 Draw 中添加诸如颜色之类的字段。此外,determinerCouleur 必须保持私密
  • couleursChoicePanel 中,您不会“从 Draw”中得到它。或许贴出Draw的相关部分。

标签: java swing jpanel components


【解决方案1】:

本练习的目的可能是帮助您了解 Java 中的 scope and access。让我们重构 here 中看到的示例以满足您的要求。

  • ChoicePanel 需要一种方法来更新Draw 面板的实例;您可以将引用作为参数传递给面板的构造函数或如下所示的工厂方法。

    public static JPanel create(Draw draw) {…}
    
  • 在组合的动作监听器中设置draw的颜色;如果更改不是bound property(例如背景颜色),则可选择调用draw.repaint()

    Hue h = (Hue) colors.getSelectedItem();
    draw.setBackground(h.getColor());
    //draw.repaint();
    
  • 由于Draw 可能不包含任何组件,请覆盖getPreferredSize(),如here 及以下所示。

  • 我无法创建私人课程……

    为了方便运行下面的示例,ChoicePanelDraw 包含在 private static 成员中。只需将每个类移到其自己的文件中,无需 modifiers,即可从 Main 获得具有 package-private 访问权限的独立类。

    JFrame f = new JFrame("Main");
    Draw d = new Draw();
    f.add(d, BorderLayout.CENTER);
    f.add(ChoicePanel.create(d), BorderLayout.SOUTH);
    

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/a/5663782/230513
 */
public class Main {

    private static class ChoicePanel {

        public static JPanel create(Draw draw) {
            JPanel p = new JPanel();
            final JComboBox colors = new JComboBox();
            for (Hue h : Hue.values()) {
                colors.addItem(h);
            }
            colors.addActionListener((ActionEvent e) -> {
                Hue h = (Hue) colors.getSelectedItem();
                draw.setBackground(h.getColor());
            });
            p.add(colors);
            return p;
        }
    }

    private static class Draw extends JPanel {

        public Draw() {
            this.setBackground(Hue.values()[0].getColor());
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }
    }

    public enum Hue {

        Cyan(Color.cyan), Magenta(Color.magenta), Yellow(Color.yellow),
        Red(Color.red), Green(Color.green), Blue(Color.blue);

        private final Color color;

        private Hue(Color color) {
            this.color = color;
        }

        public Color getColor() {
            return color;
        }
    }

    private void display() {
        JFrame f = new JFrame("Main");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Draw d = new Draw();
        f.add(d, BorderLayout.CENTER);
        f.add(ChoicePanel.create(d), BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new Main().display();
        });
    }
}

【讨论】:

  • 谢谢@trahgod,我要试试这个:)
猜你喜欢
  • 2011-02-25
  • 2012-01-09
  • 2010-12-29
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多