简体   繁体   English

绘制另一个组件

[英]Draw in another component

I have a homework to do but there is not enough information and I'm stuck right now... Here is the issue, I have three classes : 我有作业要做,但信息不足,现在我被困住了……这是问题,我有三个课程:

  • "ChoicePanel" extends JPanel and add buttons to select a color with a JComboBox “ 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" extends JPanel and stock all outline and paint them “绘制”扩展了JPanel并存储所有轮廓并绘制它们

  • "Main" create the frame with those classes included “主”创建包含这些类的框架

I have to set Draw as a MouseMotionListener but i can't get the color selected in ChoixePanel because the JComobox is created in the construcor and I can't set it to a field. 我必须将Draw设置为MouseMotionListener,但是我无法在ChoixePanel中选择颜色,因为JComobox是在构造函数中创建的,因此无法将其设置为字段。 So how can I check ChoicePanel's buttons values from Draw ? 那么如何从Draw中检查ChoicePanel的按钮值?

Every answer will be very helpfull ! 每个答案将非常有帮助!

The goal of the exercise may be to help you learn about scope and access in Java. 练习的目的可能是帮助您了解Java的范围和访问 Let's refactor the example seen here to meet your requirements. 让我们重构此处显示的示例以满足您的要求。

  • A ChoicePanel needs a way to update an instance of the Draw panel; ChoicePanel需要一种方法来更新“ Draw面板的实例。 you can pass a reference as a parameter to the panel's constructor or the factory method shown below. 您可以将引用作为参数传递给面板的构造函数或如下所示的factory方法。

     public static JPanel create(Draw draw) {…} 
  • In the combo's action listener set the color on draw ; 在组合的动作侦听器中,将颜色设置为draw optionally invoke draw.repaint() if the change is not to a bound property such as background color. 如果更改不是针对绑定属性(例如背景色draw.repaint()则可以选择调用draw.repaint()

     Hue h = (Hue) colors.getSelectedItem(); draw.setBackground(h.getColor()); //draw.repaint(); 
  • Because Draw may contain no components, override getPreferredSize() as shown here and below. 因为Draw可以不包含组件,覆盖getPreferredSize()如图这里和下面。

  • I can't create a private class… 我无法创建私人课程…

    For convenience in running the example below, ChoicePanel and Draw are included as private static members. 为了方便运行以下示例, ChoicePanelDraw作为private static成员包含在内。 Simply move each to its own file, absent the modifiers , to get stand-alone classes having package-private access from Main . 只需将它们移动到其自己的文件(不带修饰符) ,即可从Main获得具有包私有访问权限的独立类。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM