如何在jframe

时间:2015-07-09 15:56:53

标签: java swing jpanel gridbaglayout

我有一个jframe,以及带有gridbaglayout的4个jpanel。我想通过点击按钮从jpanel切换到另一个。但是,不是获得第一个面板,而是获得最后一个面板。 这就是我的所作所为:

public static void main(String args[]){

        JFrame myframe = new JFrame();

        try {
            contentPane = new BackgroundPanel();
            contentPane.setLayout(new GridBagLayout());
            contentPane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            principalpane = new BackgroundPanel();
            principalpane.setLayout(new GridBagLayout());
            principalpane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            optionspane = new BackgroundPanel();
            optionspane.setLayout(new GridBagLayout());
            optionspane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            datapane = new BackgroundPanel();
            datapane.setLayout(new GridBagLayout());
            datapane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            genpane = new BackgroundPanel();
            genpane.setLayout(new GridBagLayout());
            genpane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /***************************************************************************/
        /*****************************First*****************************************/
        GridBagConstraints gbc=new GridBagConstraints();
        GridBagConstraints gbcl=new GridBagConstraints();
        JLabel welcome=new JLabel("Optimizing & Visualizing Process Progression");
        welcome.setFont(new Font("Tahoma", Font.BOLD, 14));
        gbcl.gridx=0;
        gbcl.gridy=0;
        gbcl.gridwidth = 2;
        gbcl.gridheight=1;
        gbcl.insets=new Insets(10,200,5,100);
        gbcl.fill=GridBagConstraints.BOTH;
        principalpane.add(welcome, gbcl);


        gbc.gridx=0;
        gbc.gridy=1;
        gbc.weightx = 1;
        gbc.weighty = 1;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        gbc.fill=GridBagConstraints.BOTH;
        gbc.insets=new Insets(10,10,10,10);
        try {
            principalpane.add(Mypanel.secondpane(), gbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        gbc.gridx=1;
        gbc.gridy=1;

    //  gbc.anchor=GridBagConstraints.NORTHEAST;
        gbc.fill=GridBagConstraints.BOTH;
        gbc.insets=new Insets(10,10,10,10);
        try {
            principalpane.add(Mypanel.firstpanel(), gbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        gbc.gridx=0;
        gbc.gridy=2;
        gbc.gridwidth = 2;
        gbc.gridheight=2;
    //  gbc.anchor=GridBagConstraints.SOUTH;
        gbc.fill=GridBagConstraints.BOTH;
        gbc.insets=new Insets(0,10,5,10);
        try {
            principalpane.add(Mypanel.thirdpanel(), gbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /******************Second ***************************/
        GridBagConstraints ogbc=new GridBagConstraints();
        GridBagConstraints ogbcl=new GridBagConstraints();

        JLabel optwelcome=new JLabel("Abnormal Process Progression");
        optwelcome.setFont(new Font("Ubuntu", Font.BOLD, 16));
        ogbcl.gridx=0;
        ogbcl.gridy=0;
        ogbcl.gridwidth = 2;
        ogbcl.gridheight=1;
        ogbcl.insets=new Insets(10,280,5,10);
        ogbcl.fill=GridBagConstraints.BOTH;
        optionspane.add(optwelcome, ogbcl);

        gbc.gridx=0;
        ogbc.gridy=1;
        ogbc.weightx = 1;
        ogbc.weighty = 1;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        ogbc.fill=GridBagConstraints.BOTH;
        ogbc.insets=new Insets(10,10,10,10);
        try {
            optionspane.add(Mypanel.forthpanel(), ogbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ogbc.gridx=1;
        ogbc.gridy=1;
        ogbc.weightx = 1;
        ogbc.weighty = 1;
        ogbcl.gridwidth = 1;
        ogbc.gridheight=2;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        ogbc.fill=GridBagConstraints.BOTH;
        ogbc.insets=new Insets(10,10,10,10);
        try {
            optionspane.add(Mypanel.sixthpanel(), ogbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ogbc.gridx=0;
        ogbc.gridy=2;
        ogbc.weightx = 1;
        ogbc.weighty = 1;
        ogbc.gridheight=1;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        ogbc.fill=GridBagConstraints.BOTH;
        ogbc.insets=new Insets(10,10,10,10);
        try {
            optionspane.add(Mypanel.fifthpanel(), ogbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ogbc.gridx=0;
        ogbc.gridy=3;
        ogbc.weightx = 1;
        ogbc.weighty = 1;
        ogbc.gridwidth=2;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        ogbc.fill=GridBagConstraints.BOTH;
        ogbc.insets=new Insets(10,10,10,10);
        try {
            optionspane.add(Mypanel.seventhpanel(), ogbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /*************************Third*************************************/
        GridBagConstraints dgbc=new GridBagConstraints();
        GridBagConstraints dgbcl=new GridBagConstraints();

        JLabel datawelcome=new JLabel("Please Enter the QoS' values");
        datawelcome.setFont(new Font("Ubuntu", Font.BOLD, 16));
        dgbcl.gridx=0;
        dgbcl.gridy=0;
        dgbcl.gridwidth = 1;
        dgbcl.gridheight=1;
        dgbcl.anchor=GridBagConstraints.NORTH;
        dgbcl.insets=new Insets(0,250,35,10);
        dgbcl.fill=GridBagConstraints.BOTH;
        datapane.add(datawelcome, dgbcl);


        dgbc.gridx=0;
        dgbc.gridy=0;
        dgbc.weightx = 1;
        dgbc.weighty = 1;
        dgbcl.anchor=GridBagConstraints.CENTER;
        dgbc.fill=GridBagConstraints.BOTH;
        dgbc.insets=new Insets(5,10,10,5);
        try {
            datapane.add(Mypanel.eighthpanel(), dgbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        dgbc.gridx=0;
        dgbc.gridy=1;
        gbc.anchor=GridBagConstraints.SOUTH;
        dgbc.fill=GridBagConstraints.BOTH;
        dgbc.insets=new Insets(5,0,5,5);
        try {
            datapane.add(Mypanel.ninethpanel(), dgbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    /****************Fourth*************************************/
        GridBagConstraints ggbc=new GridBagConstraints();

        ggbc.gridx=0;
        ggbc.gridy=0;
        ggbc.weightx = 1;
        ggbc.weighty = 1;
        ggbc.anchor=GridBagConstraints.CENTER;
        ggbc.fill=GridBagConstraints.BOTH;
        ggbc.insets=new Insets(5,10,10,5);
        try {
            genpane.add(Mypanel.tenthpanel(), ggbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ggbc.gridx=0;
        ggbc.gridy=1;
        ggbc.anchor=GridBagConstraints.SOUTH;
        ggbc.fill=GridBagConstraints.BOTH;
        ggbc.insets=new Insets(5,0,5,5);
        try {
            genpane.add(Mypanel.eleventhpanel(), ggbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        GridBagConstraints cgbc=new GridBagConstraints();

        cgbc.gridx=0;
        cgbc.gridy=0;
        cgbc.weightx = 1;
        cgbc.weighty = 1;
        cgbc.anchor=GridBagConstraints.CENTER;
        cgbc.fill=GridBagConstraints.BOTH;
        principalpane.setVisible(true);
        contentPane.add(principalpane, cgbc);

        cgbc.gridx=0;
        cgbc.gridy=0;
        cgbc.weightx = 1;
        cgbc.weighty = 1;
        cgbc.anchor=GridBagConstraints.CENTER;
        cgbc.fill=GridBagConstraints.BOTH;
        optionspane.setVisible(false);
        contentPane.add(optionspane, cgbc);

        cgbc.gridx=0;
        cgbc.gridy=0;
        cgbc.weightx = 1;
        cgbc.weighty = 1;
        cgbc.anchor=GridBagConstraints.CENTER;
        cgbc.fill=GridBagConstraints.BOTH;
        datapane.setVisible(false);
        contentPane.add(datapane, cgbc);

        cgbc.gridx=0;
        cgbc.gridy=0;
        cgbc.weightx = 1;
        cgbc.weighty = 1;
        cgbc.anchor=GridBagConstraints.CENTER;
        cgbc.fill=GridBagConstraints.BOTH;
        genpane.setVisible(false);
        contentPane.add(genpane, cgbc);

        contentPane.setVisible(true);
        myframe.getContentPane().add(contentPane);

        myframe.pack();
        myframe.setLocationRelativeTo(null);
        myframe.setSize(new Dimension(800, 600));

        myframe.setVisible(true);
        myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

1 个答案:

答案 0 :(得分:1)

太多代码无法理解你在做什么,但GridBagLayout用于静态布局,其中组件不会动态更改。

  

我想通过点击按钮从jpanel切换到另一个。

听起来像你应该使用Card Layout,它一次只能显示一个面板,并允许你交换面板。

教程示例演示如何在生成ActionEvent时交换面板。