windowListenning,keyListenning问题:Java

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

这是我的窗口: Preview of our app 使用一些键,我们可以对形状进行一些更改:第一层,第二层,删除,... JmenuBar“矩形”,“圆形”用于创建新形状。当您单击它时,它将打开一个jcolorchooser对话框。

What appear when i click on rectangle

问题: -当我们按一个键,然后按矩形创建一个矩形时,该矩形不会出现。 -当我们按下矩形时,它会在我们的窗户上创建一个形状,但是我们不能再使用这些键了。

我已经尝试过将某些解决方案发布在堆栈上,但是我什么都没找到。

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,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,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,Color.MAGENTA,Color.BLUE));
        r.addAttributes(new SelectionAttributes());
        sc.add(r);
        c = new SCircle(new Point(150,40);
        c.addAttributes(new ColorAttributes(false,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,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","Choose Rectangle Stock Color",cc.banner.getBackground());
        Rectangle windowBounds= this.getBounds();
        SRectangle r = new SRectangle(new Point(windowBounds.width/2,windowBounds.height)/5,windowBounds.height)/5);
        r.addAttributes(new ColorAttributes(true,Stroked));
        r.addAttributes(new SelectionAttributes());
        this.model.add(r);
        this.sview.invalidate();

    }

    public void createDefaultText(){
        Color Filled = jcolorchooser.showDialog(this.cc,"Choose Back Color","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,response);
        t.addAttributes(new ColorAttributes(true,Stroked));
        t.addAttributes(new SelectionAttributes());
        t.addAttributes(new FontAttributes());
        this.model.add(t);
        this.sview.invalidate();
    }
    }

iCMS 回答:windowListenning,keyListenning问题:Java

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2260312.html

大家都在问