绘制另一个组件

时间:2015-05-16 10:57:51

标签: java swing jpanel components

我有功课要做,但没有足够的信息,我现在被卡住了...这是问题,我有三个班级:

  • “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;
            }
        }
    }
    
  • “绘制”扩展JPanel并存储所有轮廓并绘制它们

  • “Main”使用包含的类

  • 创建框架

我必须将Draw设置为MouseMotionListener,但我无法在ChoixePanel中选择颜色,因为JComobox是在construcor中创建的,我无法将其设置为字段。 那么如何从Draw?

中检查ChoicePanel的按钮值

每个答案都非常有用!

1 个答案:

答案 0 :(得分: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可能不包含任何组件,因此如here及以下所示覆盖getPreferredSize()

  •   

    我无法创建私人课程......

    为方便运行以下示例,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);
    

image

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();
        });
    }
}
相关问题