简体   繁体   English

无法访问其类之外的变量

[英]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.当我尝试在 changeSize 类中调用 butSmall 变量时,什么也没有发生,而且它似乎无法识别。 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.您还需要根据您是否希望访问类或包之外的按钮,添加诸如protectedpublic类的访问修饰符。 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.基本上,仅仅因为您在FilledFrame类的构造函数中声明变量(例如butSmall ),它们就成为构造函数作用域的局部变量。
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");
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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