gpt4 book ai didi

java - 向子面板添加按钮

转载 作者:行者123 更新时间:2023-12-01 12:49:24 24 4
gpt4 key购买 nike

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

代码(如果有帮助):

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

学生类(class)

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

最佳答案

主要问题是 st1PanelRightPanelLeft 中未定义。您需要传递您想要使用的 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 ,这将使人们更容易阅读您的代码...

关于java - 向子面板添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24356570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com