我正在尝试编写一段代码来添加一个学生,并显示他们的所有信息。另外,我希望它能显示学生使用whatIsUp()方法所做的10倍内容。我不确定如何将我的数组放到按钮上。
app.java
public class app
{
public static void main(String args[])
{
myJFrame mjf = new myJFrame();
student student1 = new student ("John", "Smith");
}
}myJFrame.java
import java.awt.*;
import javax.swing.*;
public class myJFrame extends JFrame
{
public myJFrame ()
{
super ("My Frame");
//------------------------------------------------------
// Create components: Jpanel
myPanel mjp = new myPanel();
getContentPane().add(mjp,"Center");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize (700, 600);
setVisible(true);
}
}myPanel.java
import java.awt.*;
import javax.swing.*;
import static javax.swing.UIManager.get;
public class myPanel extends JPanel {
public myPanel ()
{
setBackground(new Color(200, 190, 255));
JButton b1 = new JButton(get.whatsUp1());
b1.setBackground(Color.green);
add(b1);
JButton b2 = new JButton(get.whatsUp1());
b2.setBackground(Color.yellow);
add(b2);
JButton b3 = new JButton(get.whatsUp1());
b3.setBackground(Color.magenta);
add(b3);
JButton b4 = new JButton(get.whatsUp1());
b4.setBackground(Color.magenta);
add(b4);
JButton b5 = new JButton(get.whatsUp1());
b5.setBackground(Color.magenta);
add(b5);
JButton b6 = new JButton(get.whatsUp1());
b6.setBackground(Color.magenta);
add(b6);
JButton b7 = new JButton(get.whatsUp1());
b7.setBackground(Color.magenta);
add(b7);
JButton b8 = new JButton(get.whatsUp1());
b8.setBackground(Color.magenta);
add(b8);
JButton b9 = new JButton(get.whatsUp1());
b9.setBackground(Color.magenta);
add(b9);
JButton b10 = new JButton(get.whatsUp1());
b10.setBackground(Color.magenta);
add(b10);
}
}还有我的student.java
public class student {
String firstName;
String lastName;
String[] whatsUp = {"walking","sleeping","in class","studying"};
student(String myFirstName, String myLastName)
{
firstName = myFirstName;
lastName= myLastName;
}
String getName()
{
return firstName + " " +lastName;
}
//--------METHODS---------
String getInfo()
{
return "Name = " + firstName + " " + lastName;
}
String whatsUp1()
{
double rn = Math.random();
double rn_max5 = rn * 4;
int ri_max5 = (int) rn_max5;
return whatsUp[ri_max5];
}
}发布于 2015-09-21 04:18:48
您必须向main()中创建的学生传递一个引用。此学生将被传递到JFrame,然后传递到JPanel。所以我改变了构造器,将一个学生作为参数。另一个建议-请保持命名约定,这样类将以大写字母开头(我没有更改以保留您的代码):
public class app
{
public static void main(String args[])
{
student student1 = new student ("John", "Smith");
myJFrame mjf = new myJFrame(student1);
}
}-
public class myJFrame extends JFrame {
public myJFrame(student s) {
super("My Frame");
// ------------------------------------------------------
// Create components: Jpanel
myPanel mjp = new myPanel(s);
getContentPane().add(mjp, "Center");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(700, 600);
setVisible(true);
}
}-
public class myPanel extends JPanel {
public myPanel(student s) {
setBackground(new Color(200, 190, 255));
JButton b1 = new JButton(s.whatsUp1());
b1.setBackground(Color.green);
add(b1);
JButton b2 = new JButton(s.whatsUp1());
b2.setBackground(Color.yellow);
add(b2);
JButton b3 = new JButton(s.whatsUp1());
b3.setBackground(Color.magenta);
add(b3);
JButton b4 = new JButton(s.whatsUp1());
b4.setBackground(Color.magenta);
add(b4);
JButton b5 = new JButton(s.whatsUp1());
b5.setBackground(Color.magenta);
add(b5);
JButton b6 = new JButton(s.whatsUp1());
b6.setBackground(Color.magenta);
add(b6);
JButton b7 = new JButton(s.whatsUp1());
b7.setBackground(Color.magenta);
add(b7);
JButton b8 = new JButton(s.whatsUp1());
b8.setBackground(Color.magenta);
add(b8);
JButton b9 = new JButton(s.whatsUp1());
b9.setBackground(Color.magenta);
add(b9);
JButton b10 = new JButton(s.whatsUp1());
b10.setBackground(Color.magenta);
add(b10);
}
}https://stackoverflow.com/questions/32683772
复制相似问题