我正在尝试添加一些显示来自另一个类中的方法的信息的按钮。我已经创建了一个Jpanel和两个子面板,并且我正在尝试从类student访问该方法。现在,我正在尝试在主面板中创建学生,并在子面板中添加按钮。现在我知道它涉及到点操作,但由于某些原因,我找不到正确的语法。
代码如果有帮助的话:
Master Panel
import java.awt.*;
import javax.swing.*;
public class myJPanel extends JPanel
{
public myJPanel ()
{
super ();
GridLayout grid = new GridLayout(1,1);
setLayout(grid);
setBackground(Color.green);
student st1 = new student("Dan", "Smith", 27);
PanelLeft top = new PanelLeft();
PanelRight bottom = new PanelRight();
add(top);
add(bottom);
}
}子面板1
import java.awt.*;
import javax.swing.*;
public class PanelLeft extends JPanel
{
public PanelLeft ()
{
super ();
GridLayout grid = new GridLayout(1,1);
setLayout(grid);
setBackground(Color.pink);
JButton jb1 = new JButton(st1.getInfo());
add(jb1, "Center");
}
} 子面板2
import java.awt.*;
import javax.swing.*;
public class PanelRight extends JPanel
{
public PanelRight ()
{
super ();
GridLayout grid = new GridLayout(10,1);
setLayout(grid);
setBackground(Color.red);
JButton jb1 = new JButton(st1.whatsUp());
JButton jb2 = new JButton(st1.whatsUp());
JButton jb3 = new JButton(st1.whatsUp());
JButton jb4 = new JButton(st1.whatsUp());
JButton jb5 = new JButton(st1.whatsUp());
JButton jb6 = new JButton(st1.whatsUp());
JButton jb7 = new JButton(st1.whatsUp());
JButton jb8 = new JButton(st1.whatsUp());
JButton jb9 = new JButton(st1.whatsUp());
JButton jb10 = new JButton(st1.whatsUp());
add(jb1);
add(jb2);
add(jb3);
add(jb4);
add(jb5);
add(jb6);
add(jb7);
add(jb8);
add(jb9);
add(jb10);
}
}学生班
public class student
{
String firstName;
String lastName;
int Age;
double r;
int myNumber;
student(String a, String b, int x)
{
firstName = a;
lastName = b;
Age = x;
}
String getInfo()
{
return "NAME = "+firstName+ " "+lastName+" "+"Age = "+ Age;
}
String whatsUp()
{
double r;
int myNumber;
String[] acts = new String[5];
acts[0] = " is fishing";
acts[1] = " is studying";
acts[2] = " is running";
acts[3] = "is interacting";
acts[4] = "is talking";
r = Math.random();
myNumber = (int) (r * 5.0);
return (acts[myNumber]);
}
}发布于 2014-06-23 08:34:29
主要的问题是PanelRight和PanelLeft中没有定义st1。您需要传递要使用的student的引用,例如...
student st1 = new student("Dan", "Smith", 27);
PanelLeft top = new PanelLeft(st1);
PanelRight bottom = new PanelRight(st1);PanelLeft...
public class PanelLeft extends JPanel
{
public PanelLeft (student st1)
{
//...
JButton jb1 = new JButton(st1.getInfo());PanelRight...
public class PanelRight extends JPanel
{
public PanelRight (student st1)
{
//...
JButton jb1 = new JButton(st1.whatsUp());
JButton jb2 = new JButton(st1.whatsUp());
JButton jb3 = new JButton(st1.whatsUp());
JButton jb4 = new JButton(st1.whatsUp());
JButton jb5 = new JButton(st1.whatsUp());
JButton jb6 = new JButton(st1.whatsUp());
JButton jb7 = new JButton(st1.whatsUp());
JButton jb8 = new JButton(st1.whatsUp());
JButton jb9 = new JButton(st1.whatsUp());
JButton jb10 = new JButton(st1.whatsUp());您可能还希望通过Code Conventions for the Java TM Programming Language进行阅读,这将使人们更容易阅读您的代码……
https://stackoverflow.com/questions/24356570
复制相似问题