java - 在 Java 类之间传递数据

标签 java colors paint

在从下拉列表中选择颜色后从 GUI 发送索引后,我无法从 Color 类中检索变量。我可以很好地发送索引并从 HashMap 中检索它,我知道这一点是因为我使用 System.out.println 来检查。基本上我的问题是,我哪里出错了?我需要记住什么才能确保我不会再遇到这个麻烦?编辑:忘记提及,发送索引的按钮位于单独的 JPanel 中,用于 UI 组件(按钮和组合框)。

//edit
class UIPanel extends JPanel{
   public MainPanel gpanel;
    public Integer data;
    public Color colval;
    public Colour col;
public UIPanel(MainPanel panel) {

        col = new Colour();

        gpanel = panel;


        Box btnBox = Box.createHorizontalBox();

        btnBox.add(setBtn = new JButton());
        btnBox.add(Box.createHorizontalGlue());
         JButton setBtn = new JButton("Set");

        final DefaultComboBoxModel colour = new DefaultComboBoxModel();
        colour.addElement("Red");
        final JComboBox colours = new JComboBox(colour);
        JScrollPane colourScroll = new JScrollPane(colours);

        btnBox.setSize(300, 100);
        btnBox.add(Box.createHorizontalGlue());

        add(btnBox, BorderLayout.NORTH);
//end of edit

Button to send Index from GUI class to Colour class    

    setBtn.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {

                data= colours.getSelectedIndex();

                col.setCol(data);    
            }
        });

颜色类,其中包含颜色列表的 HashMap 。

public class Colour{

    public Color colVal;

    HashMap<Integer, Color> map = new HashMap<Integer, Color>();

    public Colour() {
        map.put(0, Color.RED);
        map.put(1, Color.BLUE);
        map.put(2, Color.YELLOW);
        map.put(3, Color.GREEN);
    }

    public Color setCol(Integer data) {
       //Color colours;
        colVal = map.get(data);
        System.out.println("colour" + colVal);
        return colVal;
    }

    public Color getColVal() {
        return colVal;
    }

以及颜色将从颜色类发送到的 GUI 类上的绘制区域

class MainPanel extends JPanel{    
    //private Colour col;
    int px, py;
    //radius
    public Color colvals;

    public Colour col;

    public MainPanel() {  
        col = new Colour();

        this.addMouseMotionListener(new MouseMotionAdapter() {
            // store drag coordinates and repaint
                public void mouseDragged( MouseEvent event )
                {
                        px = event.getX();
                        py = event.getY();
                        repaint();
                }
                }); // end call to addMouseMotionListener
    }

    public void paint( Graphics g ) {   
        g.setColor(col.colVal);//This is where the colour value will be placed
        System.out.println(col.colVal);
        g.fillOval( px, py, 15, 15 );
    }
}

我可能遗漏了一些愚蠢的东西,但我似乎无法弄明白。

P.S:制作 Vignere Cipher 应用程序有多复杂?

最佳答案

JComboBox 可用于直接将任何对象用作项目,只需考虑一件小事:它将使用 toString 作为要显示的标签。 (参见 JComboBox javadoc - Providing a Custom Renderer)。

但不是使用自定义渲染器,我总是更喜欢一次编写一个非常简单的帮助程序类 - 我们称之为 ComboBoxItem - 它可重复用于任何类型的数据。

public class ComboBoxItem<T>
{
    private T value;
    private String label;

    public ComboBoxItem(T value, String label)
    {
        this.value = value;
        this.label = label;
    }

    public T getValue()
    {
        return this.value;
    }

    public String getLabel()
    {
        return this.label;
    }

    // important! since this is the workaround ;-)
    public String toString()
    {
        return this.label; // or whatever you like
    }
}

然后使用 ComboBoxItem 而不是 String 值填充 JComboBox:

在你的代码中而不是

final DefaultComboBoxModel colour = new DefaultComboBoxModel();
colour.addElement("Red");
colour.addElement("Blue");
colour.addElement("Yellow");
colour.addElement("Green");
colours = new JComboBox(colourValues);

...你会用到

final DefaultComboBoxModel colour = new DefaultComboBoxModel();
colour.addElement(new ComboBoxItem<Color>(Color.RED, "Red"));
colour.addElement(new ComboBoxItem<Color>(Color.BLUE, "Blue"));
colour.addElement(new ComboBoxItem<Color>(Color.YELLOW, "Yellow"));
colour.addElement(new ComboBoxItem<Color>(Color.GREEN, "Green"));
colours = new JComboBox(colourValues);

这将使选择包含 ComboBoxItem 作为值,您可以通过执行以下操作简单地访问这些值:

// instead of getSelectedIndex()
ComboBoxItem<Color> item = (ComboBoxItem) colours.getSelectedItem(); 
Color c = item.getValue();

然后可以将相同的过程重复用于任何其他类型的值 - 甚至是复杂的值。

注意:如果您有一个带有适当 toString() 表示的数据对象,您当然可以简单地将它用作选择的值。

注意 2: 如果字符串表示不够(例如,您想要显示颜色和名称),请查看 ListCellRenderer它能够以任何需要的方式显示项目(通过返回任意 JComponent)。

关于java - 在 Java 类之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27041010/

相关文章:

java - java中的类分离。例如。虚拟键盘(带有 activelisteners)和另一个从键盘类接收输入的类

android - Paint.measureText : Is the sum of the parts equal to the whole?

java - 如何为 JScrollPane 的视口(viewport)绘制引导标记

c++ - 使用 OpenCV 在网络摄像头上绘图

java - Set 中的每个元素如何成为 Map.Entry 对象?

java - FragmentTransaction Replace() 方法仅替换布局中的某些元素

java - 如何使用 Jersey 序列化为 Json? (我正在使用 Gradle )

python - 按图像中的颜色选择对象?

php - 使用 PHP 选择合适的背景/前景色

vim - 获取光标下单词的颜色