向子面板添加按钮

时间:2014-06-22 23:01:47

标签: java swing jpanel jbutton

我正在尝试添加一些按钮,这些按钮显示我在另一个类中的方法的信息。我已经创建了一个Jpanel和2个子面板,我试图从班级学生那里访问该方法。现在我尝试在主面板中创建学生,并在子面板中添加按钮。现在我知道它涉及点操作,但由于某种原因我找不到正确的语法。

代码如果有帮助:

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

1 个答案:

答案 0 :(得分:1)

主要问题是st1PanelRight中未定义PanelLeft。您需要传递要使用的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,这样可以让人们更轻松地阅读您的代码......