繁体   English   中英

无法访问其类之外的变量

[英]Can't access variables outside of their class

我的按钮变量都没有在他们的班级之外被识别。 当我尝试在 changeSize 类中调用 butSmall 变量时,什么也没有发生,而且它似乎无法识别。 我该如何解决这个问题,以便我可以访问原始类之外的按钮变量?

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){
            }
        });
    }
}

如果你想在整个代码中引用你的按钮而不在方法等中使用参数,那么你应该将声明移到任何方法之外,但仍然在类中,就像安迪说的那样。 您还需要根据您是否希望访问类或包之外的按钮,添加诸如protectedpublic类的访问修饰符。 然后,只需初始化方法内的按钮。

它看起来像这样:

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...
    }
}

正如安迪指出的那样,它应该有效。

基本上,仅仅因为您在FilledFrame类的构造函数中声明变量(例如butSmall ),它们就成为构造函数作用域的局部变量。
如果你想使用其他类中的变量,你应该改变你声明它们的地方。

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