所以我正在构建一个棋盘游戏,每当我按下Roll Dice按钮时,我都找不到用pawn图像移动标签的方法。
似乎我没有访问构建窗口/框架的类,但是唯一能够完成一半工作的是使用新Constraints一次又一次地创建窗口。
此外,动作听众也在不同的班级,我希望将他们留在那里。此外,我知道我的代码是一团糟,我会在找到有效的东西并实际移动当兵后立即清理它。这些是图形。
public class Dashboard extends JPanel{
JButton buttonroll1=new JButton("Roll Dice");
JButton buttonroll2=new JButton("Roll Dice");
JButton mydeal1=new JButton("My Deal Cards");
JButton mydeal2=new JButton("My Deal Cards");
JButton getloan1=new JButton("Get Loan");
JButton getloan2=new JButton("Get Loan");
JButton endturn1=new JButton("End Turn");
JButton endturn2=new JButton("End Turn");
static public GridBagConstraints c = new GridBagConstraints();
public JFrame frame = new JFrame("PayDay");
public GridBagLayout layout = new GridBagLayout();
public JLabel label11=new JLabel();
Icon mailIcon = new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/mailCard.png").getImage().getScaledInstance(150,80, Image.SCALE_DEFAULT));;
JButton buttonmail = new JButton(mailIcon);
Icon dealIcon = new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/dealCard.png").getImage().getScaledInstance(150,80, Image.SCALE_DEFAULT));;
JButton buttondeal = new JButton(dealIcon);
public Dashboard(Player player1,Player player2,Positions pos,int turn){
Game listener=new Game(buttonroll1,buttonroll2,mydeal1,mydeal2,getloan1,getloan2,endturn1,endturn2, buttondeal, buttonmail);
String path = "C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/";
frame.setLayout(layout);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] days={"Start","Monday","Tuesday","Wednseday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday","Wednseday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday","Wednseday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday","Wednseday","Thursday","Friday","Saturday","Sunday","Monay","Tuesday","Wednesday"};
int m=0;
//Labels for the Days+Icons
for(int y=1;y<8;y=y+2){
for(int x=0;x<7;x++){
JLabel label0=new JLabel();
JLabel label1=new JLabel(days[m]+" "+m);
JPanel panel=new JPanel();
Color color = new Color(255,255,0);
panel.setBackground(color);
c.gridx=x;
c.gridy=y;
panel.setBackground(color);
panel.add(label1);
c.fill = GridBagConstraints.HORIZONTAL;
frame.getContentPane().add(panel,c);
label0.setIcon(new ImageIcon(new ImageIcon(path+pos.position_message[m]).getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT)));
m++;
c.gridx=x;
c.gridy=y+1;
frame.getContentPane().add(label0,c);
}
}
//Last Labels for Days+Icons
for(int y=9;y<10;y++){
for(int x=0;x<4;x++){
JLabel label1=new JLabel(days[m]+" "+m);
JPanel panel=new JPanel();
Color color = new Color(255,255,0);
c.gridx=x;
c.gridy=y;
panel.setBackground(color);
panel.add(label1);
c.fill = GridBagConstraints.HORIZONTAL;
frame.getContentPane().add(panel,c);
JLabel label0=new JLabel();
label0.setIcon(new ImageIcon(new ImageIcon(path+pos.position_message[m]).getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT)));
m++;
c.gridx=x;
c.gridy=y+1;
frame.getContentPane().add(label0,c);
}
}
//Jackpot icon label
JLabel label0=new JLabel();
label0.setIcon(new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/jackpot.png").getImage().getScaledInstance(200, 100, Image.SCALE_DEFAULT)));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridx=5;
c.gridwidth=2;
c.gridy=10;
frame.getContentPane().add(label0,c);
//Payday header logo label
JLabel labelheader=new JLabel();
labelheader.setIcon(new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/logo.png").getImage().getScaledInstance(700, 100, Image.SCALE_DEFAULT)));
c.gridwidth=8;
c.gridx=0;
c.gridy=0;
frame.getContentPane().add(labelheader, c);
//Player 1 box layout with player1 info and buttons
JLabel jplayer=new JLabel(" Player"+1);
JLabel jspace1=new JLabel(" ");
JLabel jspace2=new JLabel(" ");
JLabel jspace3=new JLabel(" ");
JLabel jspace4=new JLabel(" ");
JLabel jplayermoney=new JLabel(" Money: "+player1.totalmoney);
JLabel jplayerloan=new JLabel(" Loan: "+player1.timesloan);
JLabel jbills=new JLabel(" Bills: "+player1.bills);
Box box1 = Box.createVerticalBox();
jplayer.setAlignmentX(LEFT_ALIGNMENT);
box1.add(jplayer);
jspace1.setAlignmentX(LEFT_ALIGNMENT);
box1.add(jspace1);
jplayermoney.setAlignmentX(LEFT_ALIGNMENT);
box1.add(jplayermoney);
jspace2.setAlignmentX(LEFT_ALIGNMENT);
box1.add(jspace2);
jplayerloan.setAlignmentX(LEFT_ALIGNMENT);
box1.add(jplayerloan);
jspace3.setAlignmentX(LEFT_ALIGNMENT);
box1.add(jspace3);
jbills.setAlignmentX(LEFT_ALIGNMENT);
box1.add(jbills);
jspace4.setAlignmentX(LEFT_ALIGNMENT);
box1.add(jspace4);
box1.setOpaque(true);
Color color= new Color(240,240,240);
box1.setBackground(color);
buttonroll1.addActionListener(listener);
buttonroll1.setAlignmentX(LEFT_ALIGNMENT);
box1.add(buttonroll1);
box1.add(Box.createRigidArea(new Dimension(5,5)));
mydeal1.addActionListener(listener);
mydeal1.setAlignmentX(LEFT_ALIGNMENT);
box1.add(mydeal1);
box1.add(Box.createRigidArea(new Dimension(5,5)));
getloan1.addActionListener(listener);
getloan1.setAlignmentX(LEFT_ALIGNMENT);
box1.add(getloan1);
box1.add(Box.createRigidArea(new Dimension(5,5)));
c.insets=new Insets(0,10,0,0);
c.gridwidth=1;
c.gridheight=4;
c.gridx=8;
c.gridy=0;
frame.getContentPane().add(box1,c);
//Player2 boxlayout info+buttons
JLabel jplayern=new JLabel(" Player"+2);
JLabel jspace1n=new JLabel(" ");
JLabel jspace2n=new JLabel(" ");
JLabel jspace3n=new JLabel(" ");
JLabel jspace4n=new JLabel(" ");
JLabel jplayermoneyn=new JLabel(" Money: "+player2.totalmoney);
JLabel jplayerloann=new JLabel(" Loan: "+player2.timesloan);
JLabel jbillsn=new JLabel(" Bills: "+player2.bills);
Box box1n = Box.createVerticalBox();
jplayern.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(jplayern);
jspace1n.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(jspace1n);
jplayermoney.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(jplayermoneyn);
jspace2n.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(jspace2n);
jplayerloann.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(jplayerloann);
jspace3n.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(jspace3n);
jbills.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(jbillsn);
jspace4n.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(jspace4n);
box1n.setOpaque(true);
Color colorn= new Color(240,240,240);
box1n.setBackground(colorn);
buttonroll2.addActionListener(listener);
buttonroll2.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(buttonroll2);
box1n.add(Box.createRigidArea(new Dimension(5,5)));
mydeal2.addActionListener(listener);
mydeal2.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(mydeal2);
box1n.add(Box.createRigidArea(new Dimension(5,5)));
getloan2.addActionListener(listener);
getloan2.setAlignmentX(LEFT_ALIGNMENT);
box1n.add(getloan2);
box1n.add(Box.createRigidArea(new Dimension(5,5)));
c.insets=new Insets(0,10,0,0);
c.gridwidth=1;
c.gridheight=4;
c.gridx=8;
c.gridy=7;
frame.getContentPane().add(box1n,c);
//Second boxlayout inside gribgalayout cell for player1 Dice icon/end turn button
Box box2 = Box.createVerticalBox();
JLabel labeldice=new JLabel();
labeldice.setIcon(new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/dice-1.jpg").getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT)));
box2.add(Box.createRigidArea(new Dimension(0,90)));
box2.add(labeldice);
endturn1.addActionListener(listener);
box2.add(Box.createRigidArea(new Dimension(5,5)));
endturn1.setAlignmentX(LEFT_ALIGNMENT);
box2.add(endturn1);
box2.setOpaque(true);
Color colork= new Color(240,240,240);
box2.setBackground(colork);
c.insets=new Insets(0,0,0,0);
c.gridwidth=1;
c.gridheight=4;
c.gridx=9;
c.gridy=0;
frame.getContentPane().add(box2,c);
//Second boxlayout inside gribgalayout cell for player2 Dice icon/end turn button
Box box2v = Box.createVerticalBox();
JLabel labeldicev=new JLabel();
labeldicev.setIcon(new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/dice-1.jpg").getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT)));
box2v.add(Box.createRigidArea(new Dimension(0,90)));
box2v.add(labeldicev);
endturn2.addActionListener(listener);
box2v.add(Box.createRigidArea(new Dimension(5,5)));
endturn2.setAlignmentX(LEFT_ALIGNMENT);
box2v.add(endturn2);
box2v.setOpaque(true);
Color colorkv= new Color(240,240,240);
box2v.setBackground(colorkv);
c.insets=new Insets(0,0,0,0);
c.gridwidth=1;
c.gridheight=4;
c.gridx=9;
c.gridy=7;
frame.getContentPane().add(box2v,c);
//dealbutton
buttondeal.setPreferredSize(new Dimension(150, 80));
buttondeal.addActionListener(listener);
c.insets=new Insets(0,10,0,0);
c.gridwidth=1;
c.gridx=8;
c.gridy=4;
frame.getContentPane().add(buttondeal, c);
//Mail button
buttonmail.setPreferredSize(new Dimension(150, 80));
buttonmail.addActionListener(listener);
c.insets=new Insets(0,10,0,0);
c.gridx=9;
c.gridy=4;
frame.getContentPane().add(buttonmail, c);
//Infobox box layout for players info during their respective turns
Box box3 = Box.createVerticalBox();
JLabel label2 = new JLabel("Info Box");
box3.add(label2);
if(turn==1){
JLabel label3 = new JLabel(player1.months+" Months left");
box3.add(label3);}
else if(turn==2){
JLabel label3 = new JLabel(player2.months+" Months left");
box3.add(label3);}
JLabel label4 = new JLabel("Turn: Player"+turn);
box3.add(label4);
JLabel label5 = new JLabel("Info Box");
box3.add(label5);
box3.setOpaque(true);
Color colorl= new Color(240,240,240);
box3.setBackground(colorl);
c.insets=new Insets(70,10,0,0);
c.gridwidth=2;
c.gridx=8;
c.gridy=2;
frame.getContentPane().add(box3, c);
//Blue pawn label and positioning based on player1 position
label11.setIcon(new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/pawn_blue.png").getImage().getScaledInstance(50,50, Image.SCALE_DEFAULT)));
c.insets=new Insets(0,0,100,0);
int o=player1.position;
int r=1;
if(player1.position<=6){
c.gridx=player1.position;
c.gridy=1;
frame.getContentPane().add(label11, c,0);
}
else {
while(o>6){
o=o-7;
r=r+3;
}
c.gridx=o;
c.gridy=r;
frame.getContentPane().add(label11, c,0);
}
//Yellow pawn Label and positioning based on the player 2 position
JLabel label12=new JLabel();
label12.setIcon(new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/pawn_yellow.png").getImage().getScaledInstance(50,50, Image.SCALE_DEFAULT)));
c.insets=new Insets(0,50,100,0);
int b=player2.position;
int n=1;
if(player2.position<=6){
c.gridx=player2.position;
c.gridy=1;
frame.getContentPane().add(label12, c,0);
}
else {
while(b>6){
b=b-7;
n=n+3;
}
c.gridx=b;
c.gridy=n;
frame.getContentPane().add(label12, c,0);
}
//Green background label
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon(new ImageIcon("C:/Users/Stratos/Desktop/252/Project/src/Input/Icons/bg_green.png").getImage().getScaledInstance(1020, 730, Image.SCALE_DEFAULT)));
c.gridwidth=11;
c.gridheight=12;
c.gridx=0;
c.gridy=0;
c.insets=new Insets(0,0,0,0);
frame.getContentPane().add(background,c);
System.out.println("Mphka");
}
这些是典当的动作侦听器。 buttonroll1
是蓝色棋子,buttonroll2
是黄色棋子。
public void play_game(){
db = new Dashboard(player1, player2, pos, a);
db.frame.pack();
db.frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object src=e.getSource();
if (src==buttonroll1 && turn==1) {
bpos.setPosition(player1, dice, pos);
db.frame.getContentPane().add(db.label11, db.c);
db.validate();
db.repaint();
}
else if(src==buttonroll2 && turn==2){
bpos.setPosition(player2, dice, pos);
}
}
此外,以下是试图解决我的问题的实验代码;可以更改/删除。
db.frame.getContentPane().add(db.label11, db.c);
db.validate();
db.repaint();