我有很多Java课程。我希望如果我点击窗口上的Radiobutton(我的主GUI),布尔值将被传递给其他类。
以下是它们的构造方式" 这是我的班级窗口:
class Window{
Oscillo parent;
Graph graph;
boolean b1=true;
boolean b2 = false;
Window(Oscillo parent){
this.parent = parent;
}
}//end class
这是我的类Graph:
class Graph extends JPanel(){
Window parent;
// tried to get my boolean values from Window but nothing worked
private class SG{
SG(Graphics g, int id){
// tried to get my boolean values from Window but nothing worked
...
}//end private class
}//end class
这是我的班级数据:
class Data{
private Oscillo parent;
// tried to get my boolean values from Window but nothing worked
Data (Oscillo parent){
// tried to get my boolean values from Window but nothing worked
}
...
}//end class
这是我的班级Oscillo
public class Oscillo implements MessageListener{
Data data;
Window window;
// tried to get my boolean values from Window but nothing worked
...
}//end class
我尝试了几件事,super.b1,parent.b1 ......等等,但是我无法将我的布尔值从一个类传递给其他类。
如果你能帮我解决问题,我将不胜感激! 祝你有愉快的一天!
汤姆
答案 0 :(得分:1)
您应该使用get方法将它们暴露给其他类,如下所示:
class Window{
Oscillo parent;
Graph graph;
boolean b1=true;
boolean b2 = false;
Window(Oscillo parent){
this.parent = parent;
}
// I would urge you to choose better names that are more clear
// for both variables and methods
boolean getB1() { return b1; }
boolean getB2() { return b2; }
}//end class
然后只需使用它们来读取值:
class Graph extends JPanel(){
Window parent;
private class SG{
SG(Graphics g, int id){
parent.getB1(); //getting b1's value
}//end private class
}//end class
等等......
如果您只有Oscillo
个对象,并且希望通过其“{1}}或b1
访问b2
对象,为Window添加另一个get方法,然后将这些调用链接在一起:
Window
希望这有帮助。
答案 1 :(得分:0)
对我来说,您似乎正在尝试使用来自 W indow的静态值(因为您在问题的标题中说布尔值不是从<传递< EM>类 强>)。我猜这个。如果b1,b2应该在Window类的实例之间共享,那么它们(b1,b2)是静态的。 Deeclare静态字段如下
class Window
{
public static boolean b1 = true;
public static boolean b2 = false;
}
并以
的形式访问字段boolean somebool = Window.b1;