我目前正在学习java,并选择了既有趣又有半挑战性的项目。我的项目是基于一个70年代至80年代的老式游戏,我小时曾经玩过,叫做DopeWars。
这是我构建这个游戏的第二次尝试,第一次尝试是通过将我的所有代码放在一个.java文件中完成的,这个文件成功到了必须停止的结果,因为它不断地在代码中迷失。
我的第二次尝试,我试图将我的代码分解为逻辑部分,在许多但是小型.java文件之间共享。我认为这种方法很容易处理,但发现它创造了比其他任何问题更多的问题。
应该怎么做: 在我正在运行的应用程序中导航时,系统会提示用户选择他/她的播放器。然后,从默认的起始国家,用户可以购买/出售一些优质产品。然后用户可以按下“旅行”按钮(xx),按下允许说英格兰按钮(tEng)来选择目的地,然后运送到所选目的地以获得他投资的回报。希望用户很幸运能够在他的新目的地找到一个很好的产品。
我的问题: 我试过什么我不能去一个新的目的地。当我点击我要去的国家时,同一个国家我会再次弹出。目前,它设置为在按下按钮时创建新的“国家/地区”对象。 此外,我希望它能做到以下几点:当用户离开英格兰,前往德国,然后返回英国。 Englands随机生成的价格从英格兰首次刷新。或者与德国相反。
我所能想到的只是问题在于英格兰物体一旦被打开,就需要关闭。当用户下次导航回英格兰时,允许创建新的英格兰对象,从而为用户生成新的随机价格,以便买入或卖出。
根据我的理论,我的尝试在jbutton xx(旅行按钮)的按下上以某种方式破坏,处置或移除了英格兰物体。
理想情况下,我希望保留大部分代码,因为任何根本性的改变都可能会让我感到困惑。我还删除了所有失败的代码。任何帮助将不胜感激。提前谢谢。
PlayerSelect.java:
package ggg;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ggg.GUI;
public class PlayerSelect implements ActionListener{
JFrame fr;
JPanel playerSelect;
JButton playerSelect1, playerSelect2, playerSelect3, playerSelect4, playerSelect5, playerSelect6;
JLabel q, n1, n2, n3, n4, n5, n6;
public PlayerSelect(){
fr = new JFrame("DopeWars 2014");
fr.setSize(580, 445);
fr.setLocation(10, 10);
fr.setLayout(null);
fr.setBackground(Color.black);
fr.setLocationRelativeTo(null);
fr.setVisible(true);
playerSelect = new JPanel();
playerSelect.setSize(650, 500);
playerSelect.setLocation(0, 0);
playerSelect.setLayout(null);
playerSelect.setBackground(Color.black);
playerSelect.setVisible(true);
playerSelect1 = new JButton();
playerSelect1.setSize(177, 135);
playerSelect1.setLocation(10, 115);
playerSelect1.setLayout(null);
playerSelect1.setBackground(Color.pink);
playerSelect1.addActionListener(this);
playerSelect2 = new JButton();
playerSelect2.setSize(177, 135);
playerSelect2.setLocation(197, 115);
playerSelect2.setLayout(null);
playerSelect2.setBackground(Color.green);
playerSelect2.addActionListener(this);
playerSelect3 = new JButton();
playerSelect3.setSize(177, 135);
playerSelect3.setLocation(384, 115);
playerSelect3.setLayout(null);
playerSelect3.setBackground(Color.yellow);
playerSelect3.addActionListener(this);
playerSelect4 = new JButton();
playerSelect4.setSize(177, 135);
playerSelect4.setLocation(10, 265);
playerSelect4.setLayout(null);
playerSelect4.setBackground(Color.magenta);
playerSelect4.addActionListener(this);
playerSelect5 = new JButton();
playerSelect5.setSize(177, 135);
playerSelect5.setLocation(197, 265);
playerSelect5.setLayout(null);
playerSelect5.setBackground(Color.red);
playerSelect5.addActionListener(this);
playerSelect6 = new JButton();
playerSelect6.setSize(177, 135);
playerSelect6.setLocation(384, 265);
playerSelect6.setLayout(null);
playerSelect6.setBackground(Color.blue);
playerSelect6.addActionListener(this);
q = new JLabel("Select your player to begin.");
q.setSize(600, 50);
q.setLocation(10, 50);
q.setForeground(Color.white);
q.setFont(q.getFont().deriveFont(18.0f));
n1 = new JLabel("Paul");
n1.setSize(157, 25);
n1.setLocation(10, 10);
n1.setBackground(Color.white);
n2 = new JLabel("Richard");
n2.setSize(157, 25);
n2.setLocation(10, 10);
n2.setBackground(Color.white);
n3 = new JLabel("Lisa");
n3.setSize(157, 25);
n3.setLocation(10, 10);
n3.setBackground(Color.white);
n4 = new JLabel("Roger");
n4.setSize(157, 25);
n4.setLocation(10, 10);
n4.setBackground(Color.white);
n5 = new JLabel("Katherine");
n5.setSize(157, 25);
n5.setLocation(10, 10);
n5.setBackground(Color.white);
n6 = new JLabel("Harold");
n6.setSize(157, 25);
n6.setLocation(10, 10);
n6.setBackground(Color.white);
playerSelect1.add(n1);
playerSelect2.add(n2);
playerSelect3.add(n3);
playerSelect4.add(n4);
playerSelect5.add(n5);
playerSelect6.add(n6);
playerSelect.add(q);
playerSelect.add(playerSelect1);
playerSelect.add(playerSelect2);
playerSelect.add(playerSelect3);
playerSelect.add(playerSelect4);
playerSelect.add(playerSelect5);
playerSelect.add(playerSelect6);
fr.add(playerSelect);
}
@Override
public void actionPerformed(ActionEvent a) {
GUI b = new GUI();
England e = new England();
if (a.getSource() == playerSelect1){
b.img.setBackground(Color.pink);
b.img.add(n1);
b.bg.add(e.england);
fr.dispose();
}
if (a.getSource() == playerSelect2){
b.img.setBackground(Color.green);
b.img.add(n2);
b.bg.add(e.england);
fr.dispose();
}
if (a.getSource() == playerSelect3){
b.img.setBackground(Color.yellow);
b.img.add(n3);
b.bg.add(e.england);
fr.dispose();
}
if (a.getSource() == playerSelect4){
b.img.setBackground(Color.magenta);
b.img.add(n4);
b.bg.add(e.england);
fr.dispose();
}
if (a.getSource() == playerSelect5){
b.img.setBackground(Color.red);
b.img.add(n5);
b.bg.add(e.england);
fr.dispose();
}
if (a.getSource() == playerSelect6){
b.img.setBackground(Color.blue);
b.img.add(n6);
b.bg.add(e.england);
fr.dispose();
}
}
}
GUI.java:
package ggg;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ggg.England;
import ggg.Germany;
import ggg.Engine;
public class GUI extends Engine implements ActionListener {
JFrame f;
JPanel bg, s, img, t;
JLabel £, ca, co;
static JLabel cav, cov;
JButton x, xx, y, z, btEng, btGerm;
public GUI(){
f = new JFrame("FFF");
bg = new JPanel();
bg.setSize(650, 500);
bg.setBackground(Color.darkGray);
bg.setLayout(null);
bg.setLocation(0, 0);
s = new JPanel();
s.setSize(180, 445);
s.setBackground(Color.white);
s.setLayout(null);
s.setLocation(10, 10);
img = new JPanel();
img.setSize(160, 120);
img.setBackground(Color.blue);
img.setLayout(null);
img.setLocation(10, 10);
x = new JButton("Travel");
x.setSize(136, 30);
x.setLocation(202, 10);
x.addActionListener(this);
xx = new JButton("Travels");
xx.setSize(136, 30);
xx.setLocation(202, 10);
xx.addActionListener(this);
xx.setVisible(false);
y = new JButton("Finance");
y.setSize(136, 30);
y.setLocation(349, 10);
y.addActionListener(this);
z = new JButton("Investment");
z.setSize(136, 30);
z.setLocation(496, 10);
z.addActionListener(this);
// STATS WINDOW
ca = new JLabel("CASH:");
ca.setSize(70,25);
ca.setLocation(10, 145);
£ = new JLabel("£");
£.setSize(10, 25);
£.setLocation(80, 145);
cav = new JLabel();
cav.setSize(80, 25);
cav.setLocation(90, 145);
int aaa = Engine.playerCash;
String bbb = String.valueOf(aaa);
cav.setText(bbb);
co = new JLabel("COCAINE:");
co.setSize(70, 25);
co.setLocation(10, 190);
cov = new JLabel();
cov.setSize(70, 25);
cov.setLocation(80, 190);
int ccc = Engine.playerCoca;
String ddd = String.valueOf(ccc);
cov.setText(ddd);
//Travel Window
t = new JPanel();
t.setSize(430, 403);
t.setBackground(Color.white);
t.setLayout(null);
t.setLocation(202, 52);
t.setVisible(false);
btEng = new JButton("ENGLAND");
btEng.setSize(120, 25);
btEng.setLocation(10, 100);
btEng.addActionListener(this);
btGerm = new JButton("GERMANY");
btGerm.setSize(120, 25);
btGerm.setLocation(140, 100);
btGerm.addActionListener(this);
f.setSize(650, 500);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
// ADD COMPONENT
s.add(img);
s.add(ca);
s.add(£);
s.add(cav);
s.add(co);
s.add(cov);
t.add(btEng);
t.add(btGerm);
bg.add(s);
bg.add(x);
bg.add(xx);
bg.add(y);
bg.add(z);
bg.add(t);
f.add(bg);
}
@Override
public void actionPerformed(ActionEvent a) {
England e = new England();
Germany g = new Germany();
if (a.getSource() == x){
t.setVisible(true);
}
if (a.getSource() == y){
}
if (a.getSource() == z){
}
if (a.getSource() == xx){
t.setVisible(true);
x.setVisible(true);
xx.setVisible(false);
}
if (a.getSource() == btEng){
bg.add(e.england);
bg.remove(g.germany);///////////
xx.setVisible(true);
x.setVisible(false);
t.setVisible(false);
}
if (a.getSource() == btGerm){
bg.add(g.germany);
bg.remove(e.england);
xx.setVisible(true);
x.setVisible(false);
t.setVisible(false);
}
}
}
Engine.java:
package ggg;
import javax.swing.JLabel;
import ggg.England;
import ggg.GUI;
public class Engine {
public static int playerCash = 120;
public static int playerCoca = 0;
public int cocaPrice;
public int cocaQuantity;
JLabel cp, cq;
public int cpp, cqq;
public String cqqq;
public int total;
public int deduction;
public void buyEng(){
England.error.setText(null);
// CocaPrice
cp = new JLabel();
cp.setText(England.pr.getText());
cpp = Integer.parseInt(cp.getText());
cocaPrice = cpp;
// CocaQuantity
cq = new JLabel();
cq.setText(England.tf.getText());
cqqq = cq.getText();
cqq = Integer.parseInt(cqqq);
cocaQuantity = cqq;
// CALCULATIONS
total = cocaPrice * cocaQuantity;
if (playerCash >= total){
playerCoca = playerCoca + cocaQuantity;
String q = String.valueOf(playerCoca);
playerCash = playerCash - total;
String t = String.valueOf(playerCash);
GUI.cav.setText(t);
GUI.cov.setText(q);
England.tf.setText(null);
return;
}
else {
England.tf.setText(null);
England.error.setText("You don't have enough money!");
}
return;
}
public void sellEng(){
if (playerCoca > 0){
England.error.setText(null);
// CocaPrice
cp = new JLabel();
cp.setText(England.pr.getText());
cpp = Integer.parseInt(cp.getText());
cocaPrice = cpp;
// CocaQuantity
cq = new JLabel();
cq.setText(England.tf.getText());
cqqq = cq.getText();
cqq = Integer.parseInt(cqqq);
cocaQuantity = cqq;
// CALCULATIONS
total = cocaPrice * cocaQuantity;
playerCoca = playerCoca - cocaQuantity;
String q = String.valueOf(playerCoca);
playerCash = playerCash + total;
String t = String.valueOf(playerCash);
GUI.cav.setText(t);
GUI.cov.setText(q);
England.tf.setText(null);
return;
}
else if (playerCoca <= 0){
England.tf.setText(null);
England.error.setText("You don't have anything to sell!");
}
return;
}
public void buyGerm(){
Germany.error.setText(null);
// CocaPrice
cp = new JLabel();
cp.setText(Germany.pr.getText());
cpp = Integer.parseInt(cp.getText());
cocaPrice = cpp;
// CocaQuantity
cq = new JLabel();
cq.setText(Germany.tf.getText());
cqqq = cq.getText();
cqq = Integer.parseInt(cqqq);
cocaQuantity = cqq;
// CALCULATIONS
total = cocaPrice * cocaQuantity;
if (playerCash >= total){
playerCoca = playerCoca + cocaQuantity;
String q = String.valueOf(playerCoca);
playerCash = playerCash - total;
String t = String.valueOf(playerCash);
GUI.cav.setText(t);
GUI.cov.setText(q);
Germany.tf.setText(null);
return;
}
else {
Germany.tf.setText(null);
Germany.error.setText("You don't have enough money!");
}
return;
}
public void sellGerm(){
if (playerCoca > 0){
Germany.error.setText(null);
// CocaPrice
cp = new JLabel();
cp.setText(Germany.pr.getText());
cpp = Integer.parseInt(cp.getText());
cocaPrice = cpp;
// CocaQuantity
cq = new JLabel();
cq.setText(Germany.tf.getText());
cqqq = cq.getText();
cqq = Integer.parseInt(cqqq);
cocaQuantity = cqq;
// CALCULATIONS
total = cocaPrice * cocaQuantity;
playerCoca = playerCoca - cocaQuantity;
String q = String.valueOf(playerCoca);
playerCash = playerCash + total;
String t = String.valueOf(playerCash);
GUI.cav.setText(t);
GUI.cov.setText(q);
Germany.tf.setText(null);
return;
}
else if (playerCoca <= 0){
Germany.tf.setText(null);
Germany.error.setText("You don't have anything to sell!");
}
return;
}
public static void main(String[] args) {
new PlayerSelect();
}
}
England.java:
package ggg;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import ggg.Engine;
public class England extends Engine implements ActionListener {
JPanel england, imgEng;
JLabel co2, £co2;
static JLabel pr;
static JLabel error;
static JTextField tf;
JButton b1, b2;
public England(){
england = new JPanel();
england.setSize(430, 403);
england.setBackground(Color.red);
england.setLayout(null);
england.setLocation(202, 52);
england.setVisible(true);
imgEng = new JPanel();
imgEng.setSize(410, 80);
imgEng.setBackground(Color.blue);
imgEng.setLayout(null);
imgEng.setLocation(10, 10);
co2 = new JLabel("COCAINE");
co2.setSize(70, 25);
co2.setLocation(10, 100);
£co2 = new JLabel("£");
£co2.setSize(10, 25);
£co2.setLocation(80, 100);
pr = new JLabel();
pr.setSize(60, 25);
pr.setLocation(90, 100);
Random random1 = new Random();
int x = random1.nextInt(80) + 1;
String c = String.valueOf(x);
pr.setText(c);
tf = new JTextField();
tf.setSize(70, 25);
tf.setLocation(160, 100);
b1 = new JButton("BUY");
b1.setSize(70, 25);
b1.setLocation(270, 100);
b1.addActionListener(this);
b2 = new JButton("SELL");
b2.setSize(70, 25);
b2.setLocation(350, 100);
b2.addActionListener(this);
// ERROR MESSAGE
error = new JLabel();
error.setSize(200, 25);
error.setLocation(10, 40);
// ADD COMPONENTS
england.add(imgEng);
imgEng.add(error);
england.add(co2);
england.add(£co2);
england.add(pr);
england.add(tf);
england.add(b1);
england.add(b2);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1){
Engine a = new Engine();
a.buyEng();
}
if (e.getSource() == b2){
Engine a = new Engine();
a.sellEng();
}
}
}
Germany.java:
package ggg;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import ggg.Engine;
public class Germany extends Engine implements ActionListener {
JPanel germany, imgGerm;
JLabel co2, £co2;
static JLabel pr;
static JLabel error;
static JTextField tf;
JButton b1, b2;
public Germany(){
germany = new JPanel();
germany.setSize(430, 403);
germany.setBackground(Color.blue);
germany.setLayout(null);
germany.setLocation(202, 52);
germany.setVisible(true);
imgGerm = new JPanel();
imgGerm.setSize(410, 80);
imgGerm.setBackground(Color.red);
imgGerm.setLayout(null);
imgGerm.setLocation(10, 10);
co2 = new JLabel("COCAINE");
co2.setSize(70, 25);
co2.setLocation(10, 100);
£co2 = new JLabel("£");
£co2.setSize(10, 25);
£co2.setLocation(80, 100);
pr = new JLabel();
pr.setSize(60, 25);
pr.setLocation(90, 100);
Random random1 = new Random();
int x = random1.nextInt(80) + 1;
String c = String.valueOf(x);
pr.setText(c);
tf = new JTextField();
tf.setSize(70, 25);
tf.setLocation(160, 100);
b1 = new JButton("BUY");
b1.setSize(70, 25);
b1.setLocation(270, 100);
b1.addActionListener(this);
b2 = new JButton("SELL");
b2.setSize(70, 25);
b2.setLocation(350, 100);
b2.addActionListener(this);
// ERROR MESSAGE
error = new JLabel();
error.setSize(200, 25);
error.setLocation(10, 40);
// ADD COMPONENTS
germany.add(imgGerm);
imgGerm.add(error);
germany.add(co2);
germany.add(£co2);
germany.add(pr);
germany.add(tf);
germany.add(b1);
germany.add(b2);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1){
Engine a = new Engine();
a.buyGerm();
}
if (e.getSource() == b2){
Engine a = new Engine();
a.sellGerm();
}
}
}
答案 0 :(得分:2)
“我的问题:我尝试过什么,我无法前往新目的地。当我点击我要去的国家时,同一个国家我会再次弹出。”
我认为发生的事情是你错过了扩展课程的全部想法。它以 no 的方式使它们引用任何相同的继承对象。为了实现这一点,需要传递相同的对象。
我看到你做的是每按一次按钮就创建一个new Engine()
。这就是为什么你要让同一个国家出现的原因。
我建议你使用CardLayout
。由于您的England
和Germany
对象似乎主要由带有组件和逻辑的JPanel
组成,因此您可以创建这些类extends JPanel
。使用CardLayout
将这些面板添加到主面板。如果您想要去不同的国家/地区,只需更改CardLayout
的面板即可。请参阅this example如何使用CardLayout
,您也可以查看How to Use CardLayout教程