1

我目前正在寻找一种方法来保持专注于我的窗口。我使用控制器/视图设计模式。

所以这是我的窗口: 我们的应用程序的预览 使用一些键,我们可以对我们的形状进行一些更改:第一次地面,第二次地面,删除,... JmenuBar“矩形”,“圆形”用于创建新形状。当您单击它时,它会打开一个 JColorChooser 对话框。

当我点击矩形时会出现什么

ISSUE : - 当我们按下一个键然后我们按下矩形来创建一个矩形时,矩形不会出现。- 当我们按下矩形时,它会在我们的窗口上创建一个形状,但我们不能再使用按键了。

我尝试了一些在堆栈上发布的解决方案,但我没有发现任何工作。

public class Editor extends JFrame
{
    ShapesView sview;
    SCollection model=new SCollection();
    ColorChooser cc = new ColorChooser();


    public Editor()
    {   
        super("Shapes Editor");



        this.addWindowStateListener(new WindowStateListener() {


            public void windowStateChanged(WindowEvent e) {

                System.out.println("windowState.newState = " + e.getNewState());
            }
        });


        this.addWindowListener(new java.awt.event.WindowAdapter()
        {
            public void windowClosing(WindowEvent evt)
            {
                System.exit(0);
            }


        });

        this.buildModel();

        this.sview = new ShapesView(this.model);
        this.sview.setPreferredSize(new Dimension(800,800));
        this.getContentPane().add(this.sview, java.awt.BorderLayout.CENTER);

        ImageIcon image = new ImageIcon("..\\img\\APP1.png");
        this.setIconImage(image.getImage());
        createMenuBar();  
    }   


    private void buildModel()
    {
        this.model = new SCollection();
        this.model.addAttributes(new SelectionAttributes());
        SRectangle r = new SRectangle(new Point(50,170),40,30);
        r.addAttributes(new ColorAttributes(true,false,Color.YELLOW,Color.RED));
        r.addAttributes(new SelectionAttributes());
        this.model.add(r);

        SRectangle r2 = new SRectangle(new Point(150,100),190,140);
        r2.addAttributes(new ColorAttributes(true,true,Color.GREEN,Color.GREEN));
        r2.addAttributes(new SelectionAttributes());
        this.model.add(r2);

        SCircle c = new SCircle(new Point(400,400),20);
        c.addAttributes(new ColorAttributes(false,true,Color.BLUE,Color.BLUE));
        c.addAttributes(new SelectionAttributes());
        this.model.add(c);

        SText t= new SText(new Point(100,200),"hello");
        t.addAttributes(new ColorAttributes(true,true,Color.YELLOW,Color.BLUE));
        t.addAttributes(new FontAttributes());
        t.addAttributes(new SelectionAttributes());
        this.model.add(t);

        SCollection sc = new SCollection();
        sc.addAttributes(new SelectionAttributes());
        r= new SRectangle(new Point(20,30),30,30);
        r.addAttributes(new ColorAttributes(true,false,Color.MAGENTA,Color.BLUE));
        r.addAttributes(new SelectionAttributes());
        sc.add(r);
        c = new SCircle(new Point(150,100),40);
        c.addAttributes(new ColorAttributes(false,true,Color.BLUE,Color.DARK_GRAY));
        c.addAttributes(new SelectionAttributes());
        sc.add(c);
        this.model.add(sc);
    }

    public static void main(String[] args)
    {
        Editor frame = new Editor();

        //createColorChooser(frame); //new

        frame.pack();
        frame.setVisible(true);
    }


/************************************ SECTION BARRE DE MENU ************************************/


    public static void createColorChooser(Editor frame)
    {
        ColorChooser cc = new ColorChooser();
        Dialog color= new Dialog(frame, "Color Panel");
        color.setPreferredSize(new Dimension(600, 600));
        color.setLocationRelativeTo(frame);
        color.pack();
        color.add(cc);
        color.setVisible(true);
        color.pack();
    }

    public void createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);

        JMenu menu = new JMenu("File");
        JMenu circle = new JMenu("Circle");
        JMenu rectangle = new JMenu("Rectangle");
        JMenu text = new JMenu("Text");

        menuBar.add(menu);
        menuBar.add(circle);
        menuBar.add(rectangle);
        menuBar.add(text);




        rectangle.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(MenuEvent e) {
                createDefaultRectangle();                               
            }
            @Override
            public void menuDeselected(MenuEvent e) {
            }
            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });

        circle.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(MenuEvent e) {
                createDefaultCircle();                  
            }
            @Override
            public void menuDeselected(MenuEvent e) {
            }
            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });

        text.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(MenuEvent e) {
                createDefaultText();    
            }
            @Override
            public void menuDeselected(MenuEvent e) {
            }
            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });



    }

    public void createDefaultCircle() {
        Color Filled = JColorChooser.showDialog(this.cc,"Choose Circle Fill Color",cc.banner.getBackground());
        Color Stroked = JColorChooser.showDialog(this.cc,"Choose Circle Strock Color",cc.banner.getBackground());
        Rectangle windowBounds= this.getBounds();
        SCircle c = new SCircle(new Point(windowBounds.width/2,windowBounds.height/2), Math.min(windowBounds.width, windowBounds.height)/5);
        c.addAttributes(new ColorAttributes(true,true,Filled,Stroked));
        c.addAttributes(new SelectionAttributes());
        this.model.add(c);
        this.sview.invalidate();

    }   

    public void createDefaultRectangle() {
        Color Filled = JColorChooser.showDialog(this.cc,"Choose Rectangle Fill Color",cc.banner.getBackground());
        Color Stroked = JColorChooser.showDialog(this.cc,"Choose Rectangle Stock Color",cc.banner.getBackground());
        Rectangle windowBounds= this.getBounds();
        SRectangle r = new SRectangle(new Point(windowBounds.width/2,windowBounds.height/2),Math.min(windowBounds.width, windowBounds.height)/5,Math.min(windowBounds.width, windowBounds.height)/5);
        r.addAttributes(new ColorAttributes(true,true,Filled,Stroked));
        r.addAttributes(new SelectionAttributes());
        this.model.add(r);
        this.sview.invalidate();

    }

    public void createDefaultText(){
        Color Filled = JColorChooser.showDialog(this.cc,"Choose Back Color",cc.banner.getBackground());
        Color Stroked = JColorChooser.showDialog(this.cc,"Choose Text Color",cc.banner.getBackground());
        String response = JOptionPane.showInputDialog("What's your text ?");
        Rectangle windowBounds=this.getBounds();
        SText t= new SText(new Point(windowBounds.width/2,windowBounds.height/2),response);
        t.addAttributes(new ColorAttributes(true,true,Filled,Stroked));
        t.addAttributes(new SelectionAttributes());
        t.addAttributes(new FontAttributes());
        this.model.add(t);
        this.sview.invalidate();
    }
    }

4

0 回答 0