0

ボタンが押されたら、タブ付きパネルのタブに表示されるjpanelを変更できるようにしたいと考えています。問題は、actionlisteners 内で JPanel にアクセスしてそれらを無効にし、新しい Jpanel を有効にすることができないことです。変更を作成するために呼び出すメソッドを作成すると、コンストラクターの外側のメソッドは自分の Jpanel にアクセスできません。何か案は?

編集:宣言をコンストラクターの上に移動しましたが、それでもトークンは検出されません:

編集 2: コンストラクターの外側で宣言すると、リスナー内でそれらを使用できるようになりました。コンストラクタの外では使用できませんが、リスナで十分です。ありがとうございました

public class MainFrame extends 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