代码拉取完成,页面将自动刷新
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameMode implements ActionListener,MouseListener {
Test mainMenu;
String name="";
JFrame f=new JFrame("24点游戏-游戏模式");
JLayeredPane l=f.getLayeredPane();
JButton b1=new JButton(new ImageIcon("image/Pass.png"));
JButton b2=new JButton(new ImageIcon("image/Return.png"));
JButton bO1=new JButton(new ImageIcon("image/+.png"));
JButton bO2=new JButton(new ImageIcon("image/-.png"));
JButton bO3=new JButton(new ImageIcon("image/×.png"));
JButton bO4=new JButton(new ImageIcon("image/÷.png"));
JButton bO5=new JButton(new ImageIcon("image/(.png"));
JButton bO6=new JButton(new ImageIcon("image/).png"));
JButton bO7=new JButton(new ImageIcon("image/=.png"));
JButton bO8=new JButton("Clear");
Card[] card=new Card[] {new Card(), new Card(), new Card(), new Card()};
FindExpression fe=new FindExpression();
CalculateExpression ce=new CalculateExpression();
JLabel JLbExpression=new JLabel("");
String expression;
JLabel JLbTime=new JLabel("Time : ");
static Timer timer;
int time;
boolean canEnterNumber;
public GameMode(Test mainMenu) {
this.mainMenu=mainMenu;
JLabel bg=new JLabel(new ImageIcon("image/game3.jpg"));
bg.setBounds(0,0,900,600);
b1.setBorderPainted(false);
b1.setContentAreaFilled(false);
b1.setRolloverIcon(new ImageIcon("image/PassS.png"));
b1.setBounds(50,415,160,40);
b1.addMouseListener(this);
b2.setBorderPainted(false);
b2.setContentAreaFilled(false);
b2.setRolloverIcon(new ImageIcon("image/ReturnS.png"));
b2.setBounds(200,415,160,40);
b2.addMouseListener(this);
bO1.setBorderPainted(false);
bO1.setContentAreaFilled(false);
bO1.setRolloverIcon(new ImageIcon("image/+S.png"));
bO1.setBounds(20,350,40,40);
bO1.addMouseListener(this);
bO2.setBorderPainted(false);
bO2.setContentAreaFilled(false);
bO2.setRolloverIcon(new ImageIcon("image/-S.png"));
bO2.setBounds(60,350,40,40);
bO2.addMouseListener(this);
bO3.setBorderPainted(false);
bO3.setContentAreaFilled(false);
bO3.setRolloverIcon(new ImageIcon("image/×S.png"));
bO3.setBounds(100,350,40,40);
bO3.addMouseListener(this);
bO4.setBorderPainted(false);
bO4.setContentAreaFilled(false);
bO4.setRolloverIcon(new ImageIcon("image/÷S.png"));
bO4.setBounds(140,350,40,40);
bO4.addMouseListener(this);
bO5.setBorderPainted(false);
bO5.setContentAreaFilled(false);
bO5.setRolloverIcon(new ImageIcon("image/(S.png"));
bO5.setBounds(180,350,40,40);
bO5.addMouseListener(this);
bO6.setBorderPainted(false);
bO6.setContentAreaFilled(false);
bO6.setRolloverIcon(new ImageIcon("image/)S.png"));
bO6.setBounds(220,350,40,40);
bO6.addMouseListener(this);
bO7.setBorderPainted(false);
bO7.setContentAreaFilled(false);
bO7.setRolloverIcon(new ImageIcon("image/=S.png"));
bO7.setBounds(260,350,40,40);
bO7.addMouseListener(this);
bO8.setBorderPainted(false);
bO8.setContentAreaFilled(false);
bO8.setBounds(300,350,80,40);
bO8.addMouseListener(this);
JLbExpression.setForeground(Color.BLACK);
JLbExpression.setBounds(40,250,400,50);
JLbExpression.setFont(new Font("Arial", Font.BOLD, 30));
JLbTime.setForeground(Color.BLACK);
JLbTime.setBounds(20,20,400,50);
JLbTime.setFont(new Font("Arial", Font.BOLD, 30));
l.add(bg, new Integer(0));
l.add(b1, new Integer(1));
l.add(b2, new Integer(1));
l.add(bO1, new Integer(1));
l.add(bO2, new Integer(1));
l.add(bO3, new Integer(1));
l.add(bO4, new Integer(1));
l.add(bO5, new Integer(1));
l.add(bO6, new Integer(1));
l.add(bO7, new Integer(1));
l.add(bO8, new Integer(1));
l.add(JLbExpression, new Integer(1));
l.add(JLbTime, new Integer(1));
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800,600);
f.setLocationRelativeTo(null);
while (name.equals("")) {
name=JOptionPane.showInputDialog(null, "请输入您的昵称", "24点游戏-游戏模式", JOptionPane.QUESTION_MESSAGE);
if (name==null) {
mainMenu.setVisible(true);
return;
}
}
f.setVisible(true);
start();
}
public void start() {
fe.setNumbers(new int[] {0,0,0,0});
canEnterNumber=true;
l.remove(card[0]);
l.remove(card[1]);
l.remove(card[2]);
l.remove(card[3]);
while (fe.getExpression()[0].equals("无解\n 第1页 / 共1页")) {
JLbExpression.setText("");
expression="";
for (int i=0; i<4; i++) {
card[i]=new Card((int)(Math.random()*13+1), (int)(Math.random()*4));
for (int j=0; j<i; j++) {
if (card[i].getNumber()==card[j].getNumber() && card[i].getVariety().equals(card[j].getVariety())) {
continue;
}
}
}
fe.setNumbers(new int[] {card[0].getNumber(), card[1].getNumber(), card[2].getNumber(), card[3].getNumber()});
}
card[0].setBounds(new Rectangle(150, 60, 100, 200));
card[0].setVisible(true);
card[1].setBounds(new Rectangle(250, 60, 100, 200));
card[1].setVisible(true);
card[2].setBounds(new Rectangle(350, 60, 100, 200));
card[2].setVisible(true);
card[3].setBounds(new Rectangle(450, 60, 100, 200));
card[3].setVisible(true);
card[0].addMouseListener(this);
card[1].addMouseListener(this);
card[2].addMouseListener(this);
card[3].addMouseListener(this);
card[0].setEnabled(true);
card[1].setEnabled(true);
card[2].setEnabled(true);
card[3].setEnabled(true);
l.add(card[0], new Integer(1));
l.add(card[1], new Integer(1));
l.add(card[2], new Integer(1));
l.add(card[3], new Integer(1));
timer=new Timer(1000, this);
time=card[0].getTime()+card[1].getTime()+card[2].getTime()+card[3].getTime();
JLbTime.setForeground(Color.WHITE);
JLbTime.setText("Time : "+time);
timer.start();
}
public void actionPerformed(ActionEvent e) {
time-=1;
JLbTime.setText("Time : "+time);
if (time==15) {
JLbTime.setForeground(Color.RED);
}
if (time<=0) {
timeIsUp();
}
}
public void mouseClicked(MouseEvent e) {
if (e.getSource()==b1) {
new RankList().addPlayer(name, -1);
timer.stop();
start();
}
if (e.getSource()==b2) {
mainMenu.setVisible(true);
timer.stop();
f.dispose();
}
if (canEnterNumber) {
for (int i=0; i<4; i++) {
if (e.getSource()==card[i]) {
JLbExpression.setText(JLbExpression.getText()+card[i].getNumber());
expression+=card[i].getNumber();
card[i].setEnabled(false);
canEnterNumber=false;
}
}
}
if (e.getSource()==bO1) {
JLbExpression.setText(JLbExpression.getText()+"+");
expression+="+";
canEnterNumber=true;
}
if (e.getSource()==bO2) {
JLbExpression.setText(JLbExpression.getText()+"-");
expression+="-";
canEnterNumber=true;
}
if (e.getSource()==bO3) {
JLbExpression.setText(JLbExpression.getText()+"×");
expression+="*";
canEnterNumber=true;
}
if (e.getSource()==bO4) {
JLbExpression.setText(JLbExpression.getText()+"÷");
expression+="/";
canEnterNumber=true;
}
if (e.getSource()==bO5) {
JLbExpression.setText(JLbExpression.getText()+"(");
expression+="(";
canEnterNumber=true;
}
if (e.getSource()==bO6) {
JLbExpression.setText(JLbExpression.getText()+")");
expression+=")";
canEnterNumber=true;
}
if (e.getSource()==bO7 && !card[0].isEnabled() && !card[1].isEnabled() && !card[2].isEnabled() && !card[3].isEnabled()) {
ce.setExpression(expression);
canEnterNumber=true;
if (!ce.isCorrect()) {
JOptionPane.showMessageDialog(null,"表达式输入有误,请重新输入",
"24点游戏-游戏模式",JOptionPane.INFORMATION_MESSAGE);
JLbExpression.setText("");
expression="";
card[0].setEnabled(true);
card[1].setEnabled(true);
card[2].setEnabled(true);
card[3].setEnabled(true);
return;
}
timer.stop();
if (Math.pow(ce.getDoubleResult()-24,2)<0.000001){
right();
} else {
fault();
}
}
if (e.getSource()==bO8) {
canEnterNumber=true;
JLbExpression.setText("");
expression="";
card[0].setEnabled(true);
card[1].setEnabled(true);
card[2].setEnabled(true);
card[3].setEnabled(true);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
void right() {
int score=card[0].getScore()+card[1].getScore()+card[2].getScore()+card[3].getScore();
JOptionPane.showMessageDialog(null,"回答正确,加上"+score+"分",
"24点游戏-游戏模式",JOptionPane.PLAIN_MESSAGE, new ImageIcon("image\\right.gif"));
new RankList().addPlayer(name, score);
start();
}
void fault() {
JOptionPane.showMessageDialog(null,"回答错误,扣除1分",
"24点游戏-游戏模式",JOptionPane.PLAIN_MESSAGE, new ImageIcon("image\\fault.gif"));
new RankList().addPlayer(name, -1);
start();
}
void timeIsUp() {
timer.stop();
JOptionPane.showMessageDialog(null,"时间到,扣除1分",
"24点游戏-游戏模式",JOptionPane.PLAIN_MESSAGE, new ImageIcon("image\\TimeIsUp.gif"));
new RankList().addPlayer(name, -1);
start();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。