こんにちは、初めてのポスターです。私は、800 x 500 の水槽で泳ぐ魚を生成して表示する水槽 GUI に関する宿題に取り組んでいます。当面の問題は、フレームの下部に情報を表示する何かを追加する必要があるということです。私の問題は、私たちのクラスが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);
}
}
}
ほんのわずかな金額であっても、提供できるヘルプは大歓迎です!