简体   繁体   English

将变量从一个类传递到paintComponent类

[英]Passing a variable from one class to paintComponent class

my first time here and total newbie. 我第一次来这里,是新手。 I have two classes in my program, the first class SwingPaintDemo2 and the second class MyPanel. 我的程序中有两个类,第一类为SwingPaintDemo2,第二类为MyPanel。 MyPanel contains my paintComponent(Graphics g) method. MyPanel包含我的paintComponent(Graphics g)方法。 I have a boolean variable in my first class called isTrue. 我的第一个类中有一个布尔变量isTrue。 I want to make it so that if isTrue = true; 我想这样做,如果isTrue = true; then paintComponent executes g.fillRect(l, w, 50, 50). 然后paintComponent执行g.fillRect(l,w,50,50)。 Believe me, I have googled and googled and googled...... 相信我,我已经用谷歌和谷歌搜索过……

import java.awt.*;
import javax.swing.*;

public class SwingPaintDemo2 extends JComponent {

public static boolean isTrue = true;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(); 
        }
    });
}

private static void createAndShowGUI() {

    JFrame f = new JFrame("Swing Paint Demo");
    JPanel MyPanel = new JPanel();
     MyPanel.setBorder(BorderFactory.createEmptyBorder(1000, 1000, 1000, 1000));
     MyPanel.setPreferredSize(new Dimension(250, 200));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(new MyPanel());
    f.pack();
    f.setVisible(true);

}

}

class MyPanel extends JComponent {

public MyPanel() {
    setBorder(BorderFactory.createLineBorder(Color.black));
}

public Dimension getPreferredSize() {
    return new Dimension(250,200);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g); 
    int l = 30;
    int w = 30;

    if (SwingPaintDemo2.isTrue){g.setColor(Color.black); 
  g.fillRect(l, w, 50, 50);}


}  
}

how do I get my isTrue variable to my paintComponent class (getting variable not found error in paintComponent class)? 我如何将我的isTrue变量获取到我的paintComponent类(在paintComponent类中获取未找到变量错误)? Thanks in advance for any help. 在此先感谢您的帮助。

Update: I have just posted my most recent code above after making the changes suggested earlier. 更新:进行了建议的更改后,我刚刚在上面发布了我的最新代码。 Now I am getting "cannot find symbol - variable isTrue", any help would be appreciated, thanks 现在,我收到“找不到符号-变量isTrue”,将不胜感激,谢谢

If you want to access a public static variable, always refer to it using the name of the enclosing class (and not by recreating a new instance of your SwingPaintDemo2 class): 如果要访问公共静态变量,请始终使用封闭类的名称来引用它(而不是通过重新创建SwingPaintDemo2类的新实例):

SwingPaintDemo2.isTrue

You should try to avoid static variables. 您应尽量避免使用静态变量。

Now, maybe you meant to declare a constant, then you need to declare it final 现在,也许您打算声明一个常量,那么您需要将其声明为final

public static final boolean isTrue = true;

Finally, I see also a suspicious line: 最后,我还看到一条可疑的行:

if (isTrue=true)

which should rather be 应该是

if (isTrue)

NB: variables should start with a lower-case letter. 注意:变量应以小写字母开头。

Since isTrue is a static member of SwingPaintDemo2 class, you can access this without instantiating a new object ie SwingPaintDemo2.isTrue 由于isTrueSwingPaintDemo2类的静态成员, SwingPaintDemo2您可以在不实例化新对象SwingPaintDemo2.isTrue情况下访问它。

So your code will look like: 因此您的代码将如下所示:

public void paintComponent(Graphics g) {
    super.paintComponent(g); 
    int l = 30;
    int w = 30;
   SwingPaintDemo2 PaintDemo = new SwingPaintDemo2();


    if (SwingPaintDemo2.isTrue == true){
        g.setColor(Color.black); 
        g.fillRect(l, w, 50, 50);
    }


} 

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

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