【问题标题】:Get the color from my JButtons从我的 JButtons 中获取颜色
【发布时间】:2012-02-21 22:22:37
【问题描述】:

我需要在底部的栏中制作一个带有一些按钮的框架。我所有的按钮都显示带有颜色的图像,例如黑色、灰色、白色等。我有一个面板,我可以用我选择的颜色在上面画画。问题是当我按下按钮时,我不知道如何制作一种方法来捕捉该颜色。

private JToolBar barreOutils;

// 
private JToggleButton[] btnTab = new JToggleButton[9];

// 
private String[] btnName = { "Couleur noire", "Couleur grise",
        "Couleur blanche", "Couleur rouge", "Couleur orange",
        "Couleur jaune", "Couleur verte", "Couleur cyan", "Couleur bleue" };

// 
private String[] btnColor = { "dark.gif", "gray.gif", "white.gif",
        "rouge.gif", "orange.gif", "yellow.gif", "vert.gif", "cyan.gif",
        "blue.gif" };

String[] colorTab = { "Color.DARK", "Color.GRAY", "Color.WHITE",
        "Color.RED", "Color.ORANGE", "Color.YELLOW", "Color.GREEN",
        "Color.CYAN", "Color.BLUE" };

// buttonGroup
private ButtonGroup groupeCouleurs;
// Notre panneau principal
private JPanel panneau;

public Fenetre() {

    // Organization
    setTitle("Application");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600, 600);
    setLocationRelativeTo(null);

    // Organization
    panneau = new JPanel();
    panneau.setBackground(Color.white);
    panneau.addMouseListener(new Dessiner());
    panneau.addMouseMotionListener(new Dessiner());
    getContentPane().add(panneau);

    // 
    barreOutils = createToolbar();
    getContentPane().add(barreOutils, BorderLayout.SOUTH);

}

private JToolBar createToolbar() {

    JLabel couleur = new JLabel("Couleurs : ");
    barreOutils = new JToolBar();


    groupeCouleurs = new ButtonGroup();
    barreOutils.add(couleur);


    createButton(btnTab, btnName, btnColor);

    return barreOutils;
}

private void createButton(JToggleButton[] btnTab, String[] btnName,
        String[] btnColor) {
    // TODO Auto-generated method stub

    // add the buttons on the bar at the bottom
    for (int indBtn = 0; indBtn < btnTab.length; indBtn++) {
        btnTab[indBtn] = new JToggleButton(new ImageIcon(
                Fenetre.class.getResource(btnColor[indBtn])));
        btnTab[indBtn].setToolTipText(btnName[indBtn]);
        groupeCouleurs.add(btnTab[indBtn]);
        barreOutils.add(btnTab[indBtn]);
    }
}

private class Dessiner extends MouseAdapter {

    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
        Graphics g = ((JComponent) e.getSource()).getGraphics();
        g.setColor(**???????????????????????**);
        g.drawOval(e.getX(), e.getY(), 1, 1);

    }

【问题讨论】:

    标签: java swing events colors get


    【解决方案1】:

    建议:

    • 通过btnTab[indBtn].setActionCommand(btnName[indBtn]);设置按钮的actionCommand
    • ButtonGroup 可以通过获取选择来告诉您选择了哪个按钮
    • 通过调用其getActionCommand() 方法,从上面的ButtonModel 中获取选定按钮的actionCommand 字符串。
    • 考虑使用HashMap&lt;String, Color&gt; 将actionCommand 字符串与其关联的颜色链接起来。

    还有

    • 不要通过 getGraphics 获取组件的图形。而是在 BufferedImage 中绘制,然后在 JComponent(或扩展 JComponent 的类,例如 JPanel)的 paintComponent 方法中绘制 BufferedImage。
    • 您可以通过在 BufferedImage 上调用 getGraphics() 来获取 Graphics 对象,但请务必在完成后删除 Graphics 对象。
    • 在 MouseListener 中,通过更改类字段来更改对象的状态,然后调用 repaint。

    【讨论】:

      【解决方案2】:

      您可以扩展 JToggleButton 并让该类包含您想要的任何信息。

      class ColorButton extends JToggleButton {
      
        private Color color;
      
        public ColorButton(Color c) {
          super();
          this.color = c;
        }
      
        public Color getColor() {
          return color;
        }
      }
      

      【讨论】:

      • 这也可以工作——1+。或者另一种解决方案是使用 HashMap 将 JToggleButton 的 actionCommand 字符串与 Color 匹配。
      【解决方案3】:

      我注意到按钮图像名称中有颜色名称,为什么不从那里获取它。

      【讨论】:

      • 因为我试图做一个动作事件来捕捉我点击的按钮的颜色,但它失败了。我不知道:(
      猜你喜欢
      • 2016-02-21
      • 2014-10-14
      • 2011-05-09
      • 2018-03-07
      • 2012-06-10
      • 2020-08-27
      • 2013-05-24
      相关资源
      最近更新 更多