在JFrame GUI中绘制颜色方块

时间:2014-12-31 10:16:47

标签: java user-interface awt

如何使用GUI创建分布在3行3列网格中的9个不同的正方形 在一个框架上。用户只需按下右键即可更改任何部件的颜色,将显示一个菜单以选择所需的颜色

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;

public class Coloring extends JPanel
{
private JRadioButtonMenuItem items[];
private Color colorValue[]={Color.GREEN,Color.BLUE,Color.RED,Color.PINK,Color.GRAY,Color.YELLOW,Color.CYAN,Color.MAGENTA,Color.BLACK};

public Coloring()
{


    final JPopupMenu popupMenu=new JPopupMenu();
    ItemHandler handler =new ItemHandler();
    String colors[]={"Green","Blue","Red","Pink","Gray","Yellow","Cyan","Magenta","Black"};
    ButtonGroup colorGroup= new ButtonGroup();
    items=new JRadioButtonMenuItem[9];

    for(int i=0;i<items.length;i++)
    {
        items[i]=new JRadioButtonMenuItem(colors[i]);
        popupMenu.add(items[i]);
        colorGroup.add(items[i]);
        items[i].addActionListener(handler);
    }
    JFrame frame=new JFrame("Colored Frame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new Square());
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    addMouseListener(new MouseAdapter()
    {
        public void mousePressed(MouseEvent e)
        { checkForTrigerEvent(e); }

        public void mouseReleased(MouseEvent e)
        { checkForTrigerEvent(e); }

        public void checkForTrigerEvent(MouseEvent e)
        {
            if(e.isPopupTrigger())
                popupMenu.show(e.getComponent(),e.getX(),e.getY());
        }
    }
    );
}
private class ItemHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        for(int i=0;i<items.length;i++)
            if(e.getSource()==items[i])
            {
                getContentPane().setBackground(colorValue[i] );
                repaint();
                return;
            }
        }
    }
public void paintComponent (Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2d=(Graphics2D) g.create();

    int width=getWidth();
    int height=getHeight();

    int cellWidth= width/3;
    int cellHeight=height/3;

    int xOffset=(width - (3*cellWidth))/2;
    int yOffset=(height -(3*cellHeight))/2;

}
public static void main(String args[])
{
    new Coloring();
}
}

**现在我不知道如何添加将在右键单击时改变颜色的方块**

1 个答案:

答案 0 :(得分:0)

这听起来像你需要把它分解成几个较小的部分,就像Arvind在评论中解释的那样。

在框架上使用GridLayout,添加9个首选大小的JComponents,现在将JPopupMenu添加到这些jcomponents中。

我建议查看以下内容

GridLayout http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

JComponents http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html

就像人们在上面所说的那样,这个网站是为了帮助解决不为你工作的问题。祝你好运。

更新

有很多不同的方法可以做到这一点,但我建议你将BorderLayout更改为GridLayout(0,3),这将自动定位JComponents,使它们均匀地定位在JFrame中。然后我建议你添加9个JComponents或(我更喜欢使用JPanels)到JFrame,而不是添加Square时你正在做的任何事情。然后,您将要向每个JComponent添加Action侦听器,这是一个如何使用动作侦听器来检测右键单击的示例。

    panel.addMouseListener(new MouseAdapter()
    {
       public void mouseReleased(MouseEvent e)
       {
        if(SwingUtilities.isRightMouseButton(e)) //this checks for a right mouse click
        popupMenu.show(e.getComponent(),e.getX(),e.getY());
       }

       public void checkForTrigerEvent(MouseEvent e)
       {
       }
   }
   );