简体   繁体   中英

Can't access variables outside of their class

None of my button variables are recognised outside their class. When I try to call the butSmall var in the changeSize class nothing happens and it appears unrecognized. How can I fix this so I can access the button variables outside of the original class?

import javax.swing.*;
import java.awt.*
import java.awt.event.*;
public class SquareSimp
{
    public static void main( String[] args )
    {
        FilledFrame frame = new FilledFrame();
        frame.setVisible( true );
    }
}
class FilledFrame extends JFrame
{
    int size = 400;
    public FilledFrame()
    {
        JButton butSmall = new JButton("Small");
        JButton butMedium = new JButton("Medium");
        JButton butLarge = new JButton("Large");
        JButton butMessage = new JButton("Say Hi!");
        JButton butQuit = new JButton("QUIT");
        SquarePanel panel = new SquarePanel(this);
        JPanel butPanel = new JPanel();
        butPanel.add(butSmall);
        butPanel.add(butMedium);
        butPanel.add(butLarge);
        butPanel.add(butMessage);
        butPanel.add(butQuit);
        add(butPanel, BorderLayout.NORTH);
        add(panel, BorderLayout.CENTER);
        setSize( size+80, size+100 );

        butMessage.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, "Hi");
            }
        });
    }
}

class SquarePanel extends JPanel {
    FilledFrame theApp;
    SquarePanel(FilledFrame app) {
        theApp = app;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.blue);
        g.fillRect(40, 20, theApp.size, theApp.size);
    }

}
class changeSize extends FilledFrame{
    changeSize(){
        butLarge.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
            }
        });
    }
}

If you want to refer to your buttons throughout the code without using parameters in methods and such, then you should move the declarations to be outside any methods, but still inside the class, like Andy said. You also need to add a access modifier like protected or public depending on whether or not you wish to access the buttons outside of the classes or packages. Then, just initialize the buttons inside the method.

It would look something like this:

class FilledFrame extends JFrame {
    protected JButton butSmall;
    //Do this for all the buttons
    int size = 400;
    public FilledFrame() {
        butSmall = new JButton("Small");
        // Do this for all the buttons then continue with rest of code...
    }
}

It should work, again as Andy pointed out.

Basically, just because you statement variables (eg butSmall ) inside the construct function of FilledFrame class that they became the local variable of construct function scope.
If you want use the variables in other class, you should change the place you statement them.

class FilledFrame extends JFrame
{
    JButton butSmall;
    JButton butMedium;
    JButton butLarge;
    JButton butMessage;
    JButton butQuit;
    SquarePanel panel;
    JPanel butPanel;
    int size = 400;
    public FilledFrame()
    {
        butSmall = new JButton("Small");
        butMedium = new JButton("Medium");
        butLarge = new JButton("Large");
        butMessage = new JButton("Say Hi!");
        JbutQuit = new JButton("QUIT");
        panel = new SquarePanel(this);
        butPanel = new JPanel();
        butPanel.add(butSmall);
        butPanel.add(butMedium);
        butPanel.add(butLarge);
        butPanel.add(butMessage);
        butPanel.add(butQuit);
        add(butPanel, BorderLayout.NORTH);
        add(panel, BorderLayout.CENTER);
        setSize( size+80, size+100 );

        butMessage.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, "Hi");
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM