【问题标题】:Access variables from another class in java从java中的另一个类访问变量
【发布时间】:2016-10-15 00:49:50
【问题描述】:

我正在尝试用 java 编写一个非常简单的项目。我正在尝试创建 2 个类,其中主要的类执行 2 类中的一个方法来创建一个新的 JFrame 对象。然后,主类执行类 2 中的方法来设置 2 个变量的值。然后,应在 JFrame 面板上以设置的 x 和 y 值打印一个字符串。但是,就好像 xPos 和 yPos 没有改变,字符串打印在 0,0 处。

这是代码:

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

public class Test {
        public static void main(String[] args){
                Class2 obj = new Class2();
                obj.createJFrame();
                obj.setVal(100, 200);
        }
}

class Class2 extends JPanel{
        private int xPos, yPos;
        public void createJFrame(){
                JFrame window = new JFrame();
                Class2 obj2 = new Class2();
                window.setVisible(true);
                window.setSize(300, 300);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Container c = window.getContentPane();
                c.add(obj2);
        }
        public void setVal(int x, int y){
                xPos = x;
                yPos = y;
                repaint();
        }
        public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawString("This string should be at 100, 200", xPos, yPos);
        }
}

作为旁注,我认为我的标题不准确,所以如果有人可以通过编辑标题来帮助我,那就太好了。如果标题与问题不匹配,我很抱歉,但我是 java 新手。另外,这个程序是在为一个更复杂的程序建模,所以如果这种方法看起来间接效率低下,那是因为更复杂的程序会使用这样的结构。提前谢谢你。

【问题讨论】:

    标签: java swing class object jframe


    【解决方案1】:
    class Class2 extends JPanel{
            private int xPos, yPos;
            public void createJFrame(){
                    JFrame window = new JFrame();
                    // Class2 obj2 = new Class2();
                    window.setVisible(true);
                    window.setSize(300, 300);
                    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    Container c = window.getContentPane();
                    c.add(this); // now the setValue will update the object
            }
    ...
    

    您没有更新添加到 JFrame 的对象。顺便说一句,我会在静态方法或不同的类中创建 JFrame,并将 Class2 作为参数。比如:

    class Class2 extends JPanel{
        private int xPos, yPos;
        public static void createJFrame(Class2 obj){
                JFrame window = new JFrame();
                window.setVisible(true);
                window.setSize(300, 300);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Container c = window.getContentPane();
                c.add(obj);
        }
        public void setVal(int x, int y){
                xPos = x;
                yPos = y;
                repaint();
        }
        public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawString("This string should be at 100, 200", xPos, yPos);
        }
     }
    
    public class Test {
       public static void main(String[] args){
             Class2 obj = new Class2();
             obj.setVal(100, 200);
             Class2.createJFrame(obj);
       }
    }
    

    【讨论】:

    • 谢谢,这行得通。我还没有学会使用对象作为参数,但第一个仍然可以正常工作。我敢肯定,一旦我了解了第二种方法,它就会变得有意义,并且会更好。不过就目前而言,第一种方法效果很好。
    • 另见Initial Threads
    猜你喜欢
    • 1970-01-01
    • 2012-05-04
    • 2022-01-24
    • 2016-10-18
    • 1970-01-01
    • 2023-04-07
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多