简体   繁体   中英

Access variables from another class in java

I am trying to write what should be a very simple project in java. I am trying to create 2 classes, where the main one executes a method in class 2 to create a new JFrame object. Then, the main class exicutes the method in class 2 to set 2 variable's values. Then, a string should be printed on the JFrame panel at the set x and y values. However, it's as if xPos and yPos were not changed, and the string is printed at 0,0.

This is the code:

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

As a side note, I don't think that my title is accurate, so it would be great if someone could help me by editing the title. I'm sorry if the title doesn't match the question, but I am new to java. Also, this program is modeling a more complex program, so if this method seems inefficient of indirect, it is because the more complex program will use a structure like this. Thank you in advance.

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

You weren't updating the object that was added to the JFrame. As an aside, I would create the JFrame in a static method or a different class and have Class2 as an argument. Something like:

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

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