美好的一天,第一次在这里发布海报。我一直在做涉及鱼缸 gui 的作业,该 gui 生成并显示鱼在 800 x 500 的鱼缸中游泳。手头的问题是,现在我必须在框架底部添加一些显示信息的内容。我的问题是我们的班级几乎没有接受过使用 gui 或指令的培训,我所有的尝试都给我留下了一堆乱七八糟的代码,而且没有可见的 jpanel 或容器。充其量我只会得到一个面板可以去的区域,但没有任何显示和蓝色背景。我至少要在水箱底部添加一个可以添加组件的可见 jpanel 需要什么?
这是我为 gui 提供的代码:
package fishtank.view;
import fishtank.model.Fish;
import fishtank.model.Tank;
/**
* GUI frame for the fish tank that displays the frame and places the fish
* within it.
*
*/
public class FishTankGUI extends JFrame {
private Tank tank; //Class that generates the fish onto the frame.
// Required as JFrame implements Serializable interface
private static final long serialVersionUID = 1L;
private static final int REFRESH_TIME_MS = 500;
/**
* Default constructor that sets up the GUI frame.
* @param width
* The width of the frame
* @param height
* The height of the frame
* @param titleBarText
* The text to display on the title bar
*/
public FishTankGUI(int width, int height, String titleBarText) {
super(titleBarText);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height); //another class defines this as 800 by 500.
setResizable(false);
setVisible(true);
}
/**
* Fills the tank and puts it in motion.
* <p>
* Precondition: none
*/
public void start() {
this.setupTank();
this.animateTank();
}
/**
* Fills the tank and puts it in motion.
* @param totalFish
* The number of fish to place in the tank.
*/
public void start(int totalFish) {
this.setupTank(totalFish);
this.animateTank();
}
private void setupTank() {
TankPane tankPane = new TankPane();
getContentPane().add(tankPane);
getContentPane().validate();
this.tank = new Tank(tankPane);
}
private void setupTank(int totalFish) {
TankPane tankPane = new TankPane();
getContentPane().add(tankPane);
getContentPane().validate();
this.tank = new Tank(tankPane, totalFish);
}
private void animateTank() {
if (this.tank == null) {
this.setupTank();
}
while (true) {
this.pause(REFRESH_TIME_MS);
this.tank.moveFish();
}
}
private void pause(long milliseconds) {
if (milliseconds <= 0) {
throw new IllegalArgumentException("milliseconds is out of range.");
}
try {
Thread.sleep(milliseconds);
} catch (InterruptedException ie) {
System.err.println(ie);
}
}
}
任何可以提供的帮助,即使是最少量的帮助,将不胜感激!