0

一旦按下按钮,我希望能够更改选项卡式面板的选项卡上显示的 jpanel 内容。问题是,在动作侦听器内部,我无法访问 JPanel 以禁用它们并启用新的 Jpanel,如果我创建一个方法来调用以创建更改,则构造函数之外的方法无法访问我的 Jpanel。有任何想法吗?

编辑:我已将 declerations 移到构造函数上方,但即使如此,也没有检测到令牌:

编辑2:好吧,在构造函数之外声明它允许我在听众内部使用它们。虽然我不能在构造函数之外使用它们,但侦听器就足够了。谢谢你

公共类 MainFrame 扩展 JFrame {

private JTabbedPane tabPane;

    // Add Swing components to content pane
    JPanel p1 = new JPanel(new BorderLayout());
    JPanel p2 = new JPanel(new BorderLayout());
    JPanel p3 = new JPanel(new BorderLayout());
    JPanel p4 = new JPanel(new BorderLayout());


    //Add subpanels for buttons
    JPanel sp1 = new JPanel();
    JPanel sp2 = new JPanel();
    JPanel sp3 = new JPanel();
    JPanel sp4 = new JPanel();





    public MainFrame(){

如上所示

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.*;



public class MainFrame extends JFrame {



    private JTabbedPane tabPane;

    public MainFrame(){

        this.setTitle("Final Project");



        // Add Swing components to content pane
        JPanel p1 = new JPanel(new BorderLayout());
        JPanel p2 = new JPanel(new BorderLayout());
        JPanel p3 = new JPanel(new BorderLayout());
        JPanel p4 = new JPanel(new BorderLayout());

        //Add subpanels for buttons
        JPanel sp1 = new JPanel();
        JPanel sp2 = new JPanel();
        JPanel sp3 = new JPanel();
        JPanel sp4 = new JPanel();

        //Creates buttons
          JButton buttonA1 = new JButton("LOAD");
          JButton buttonA2 = new JButton("SAVE");
          JButton buttonB1 = new JButton("ADD");
          JButton buttonB2 = new JButton("DELETE");
          JButton buttonC1 = new JButton("PRINT BY NAME");
          JButton buttonC2 = new JButton("PRINT BY ID");
          JButton buttonC3 = new JButton("PRINT BY DATE");
          JButton buttonC4 = new JButton("PRINT BY SIZE");
          JButton buttonD1 = new JButton("SEARCH BY NAME");
          JButton buttonD2 = new JButton("SEARCH BY ID");




        //adds buttons to subpanels, then subpanels to mainpanels
          sp1.add(buttonA1);
          sp1.add(buttonA2);
          sp2.add(buttonB1);
          sp2.add(buttonB2);
          sp3.add(buttonC1);
          sp3.add(buttonC2);
          sp3.add(buttonC3);
          sp3.add(buttonC4);
          sp4.add(buttonD1);
          sp4.add(buttonD2);


       //Creates and adds text to tabs
        JLabel text1 = new JLabel("                                        " +
                "Would you like to load or save customers?");
        JLabel text2 = new JLabel("                                        " +
                "Would you like to add or delete a customers?");
        JLabel text3 = new JLabel("                                        " +
                "Would you like to print a customer(s)?");
        JLabel text4 = new JLabel("                                        " +
                "Would you like to search a customer?");

        //places the text in the tabs
        p1.add(text1, BorderLayout.CENTER);
        p1.add(sp1, BorderLayout.SOUTH );
        p2.add(text2, BorderLayout.CENTER);
        p2.add(sp2, BorderLayout.SOUTH );
        p3.add(text3, BorderLayout.CENTER);
        p3.add(sp3, BorderLayout.SOUTH);
        p4.add(text4, BorderLayout.CENTER);
        p4.add(sp4, BorderLayout.SOUTH);






        this.tabPane = new JTabbedPane();
        this.tabPane.setSize(750, 50);
        this.tabPane.setLocation(10, 10);
        tabPane.addTab("Load or Save", null, p1, "This is where you can load " +
                " or save a file.");
        tabPane.addTab("Add or delete",null , p2, "This is where you add " +
                "or delete a customer.");
        tabPane.addTab("print",null , p3, "this is where you print" +
                " one or more customers.");
        tabPane.addTab("search",null , p4, "This is where you can " +
                "search customers.");
        this.setVisible(true);


        this.setSize(500, 400);
        this.setLocation(400, 300);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);

        Container c = getContentPane();
        c.add(tabPane);

        ///////////////////////////////////////////////////////////////
        // BUTTON LISTNERS
        ///////////////////////////////////////////////////////////////
     // Add behaviour to button.
        buttonA1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });

        // Add behaviour to button.
        buttonA2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });

        // Add behaviour to button.
        buttonB1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //method that would disable the panel
            disableP1();
            //then would create a new panel to take its place.





        }
        });

        // Add behaviour to button.
        buttonB2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });

        // Add behaviour to button.
        buttonC1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });

        // Add behaviour to button.
        buttonC2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });

        // Add behaviour to button.
        buttonC3.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });

        // Add behaviour to button.
        buttonC4.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });

        // Add behaviour to button.
        buttonD1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });

        // Add behaviour to button.
        buttonD2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        });




    }

    public void close(){
        WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
    }


    public static void disableP1(){



    }


}
4

0 回答 0