我要写一个类似简陋版的这样的游戏(只需要双人对战):http://www.mathsisfun.com/games/connect4.html现在的进度是这样:
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.event.MouseInputAdapter;
import javax.swing.*;public class Pconnect4 {
public static void main(String[] args) {
Panels frame = new Panels();
frame.setTitle("Connect Four");
frame.setSize(755,510);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class Panels extends JFrame {
public Panels() {
// Create button panel
JPanel pButton = new JPanel();
pButton.setLayout(new GridLayout(1,7,0,0));
JButton Jb1 = new JButton("Add");
JButton Jb2 = new JButton("Add");
JButton Jb3 = new JButton("Add");
JButton Jb4 = new JButton("Add");
JButton Jb5 = new JButton("Add");
JButton Jb6 = new JButton("Add");
JButton Jb7 = new JButton("Add");
JButton Jbs[] = {Jb1,Jb2,Jb3,Jb4,Jb5,Jb6,Jb7};
for (int i=0; i<Jbs.length; i++){
pButton.add(Jbs[i]);
} // Create panel p1 to draw the game board
JPanel p1 = new DrawGameBoard(Jbs);
// Creat p2 to hold a text field, buttons, and p1
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout(20,15));
//p2.setBackground(Color.white);
// NEED TO BE MODIFIED HERE ******************************
JLabel turn = new JLabel("User's turn");
p2.add(turn, BorderLayout.NORTH);
p2.add(p1, BorderLayout.CENTER);
p2.add(pButton, BorderLayout.SOUTH);
//System.out.println("added"); // Create p3in to hold 2 TextFileds to show scores
JPanel p3in = new JPanel();
p3in.setLayout(new GridLayout(1,3));
p3in.add(new JTextField(3));
p3in.add(new JLabel(" VS "));
p3in.add(new JTextField(3)); // Create p3 to hold player interface
String[] playerSetting = {"Human"};
String[] difficulty = {"Default"};
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(11,2,5,10)); //p3.setBackground(Color.white);
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
JButton newGame = new JButton("New Game");
p3.add(newGame);
//newGame.addActionListener(new newGameListener());
//****************************
p3.add(new JLabel("Difficulty"));
p3.add(new JComboBox(difficulty));
p3.add(new JLabel("Red Player is a")); // THESE TEXTS NEED TO BE RED
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel("Blue Player is a")); // THESE TEXTS NEED TO BE BLUE
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel(" "));
p3.add(new JLabel("Red Blue")); // DIFFERENT COLOR TEXT
p3.add(new JLabel("Score"));
p3.add(p3in);
p3.add(new JLabel(" "));
p3.add(new JButton("New Match")); //*****************************
p3.add(new JLabel(" "));
p3.add(new JLabel(" ")); // Add contents to the frame
setLayout(new BorderLayout(20,20));
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.WEST);
//p2.setAlignmentX(CENTER_ALIGNMENT);
//p3.setAlignmentX(CENTER_ALIGNMENT);
}
}class DrawGameBoard extends JPanel{ // implements MouseListener {
int height = 420;
int width = 490;
int gap = height/6;
JButton[] Jbs; DrawGameBoard(JButton[] Jbs) {
this.Jbs = Jbs;
}
protected void paintComponent(Graphics g) {
int[] diskInCol = new int[7];
super.paintComponent(g);
super.setSize(width,height);
// Draw Grids
for (int i = 0; i<7;i++ ) {g.drawLine(gap*i, 0, gap*i,height);}
for (int j = 0;j<6; j++){g.drawLine(0, gap*j, width,gap*j);}
g.drawLine(width-1, 0, width-1, height-1);
g.drawLine(0,height-1, width-1, height-1); for (int i=0;i<Jbs.length; i++){
Jbs[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("pressed!");
g.drawOval(0, 0, gap, gap);
}});
}
}
//public void drawDisk(MouseEvent e) {
// int X,Y;
//}
public void mousePressed(MouseEvent e) {//drawDisk(e);
}
public void mouseReleased(MouseEvent e) {}
}
第126行出现CompileError. 每一列底下的按钮被按下后, 要在Graphics g上画个圆(先暂时不考虑画在哪里。)。但是g不是final的不能引用。有什么办法可以解决这个问题? 作业马上要截至了,任何可行的方法都可以。请把想法或代码都告诉我。。如果做过类似的游戏的话有Code最好了多谢。
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.event.MouseInputAdapter;
import javax.swing.*;public class Pconnect4 {
public static void main(String[] args) {
Panels frame = new Panels();
frame.setTitle("Connect Four");
frame.setSize(755,510);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class Panels extends JFrame {
public Panels() {
// Create button panel
JPanel pButton = new JPanel();
pButton.setLayout(new GridLayout(1,7,0,0));
JButton Jb1 = new JButton("Add");
JButton Jb2 = new JButton("Add");
JButton Jb3 = new JButton("Add");
JButton Jb4 = new JButton("Add");
JButton Jb5 = new JButton("Add");
JButton Jb6 = new JButton("Add");
JButton Jb7 = new JButton("Add");
JButton Jbs[] = {Jb1,Jb2,Jb3,Jb4,Jb5,Jb6,Jb7};
for (int i=0; i<Jbs.length; i++){
pButton.add(Jbs[i]);
} // Create panel p1 to draw the game board
JPanel p1 = new DrawGameBoard(Jbs);
// Creat p2 to hold a text field, buttons, and p1
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout(20,15));
//p2.setBackground(Color.white);
// NEED TO BE MODIFIED HERE ******************************
JLabel turn = new JLabel("User's turn");
p2.add(turn, BorderLayout.NORTH);
p2.add(p1, BorderLayout.CENTER);
p2.add(pButton, BorderLayout.SOUTH);
//System.out.println("added"); // Create p3in to hold 2 TextFileds to show scores
JPanel p3in = new JPanel();
p3in.setLayout(new GridLayout(1,3));
p3in.add(new JTextField(3));
p3in.add(new JLabel(" VS "));
p3in.add(new JTextField(3)); // Create p3 to hold player interface
String[] playerSetting = {"Human"};
String[] difficulty = {"Default"};
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(11,2,5,10)); //p3.setBackground(Color.white);
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
JButton newGame = new JButton("New Game");
p3.add(newGame);
//newGame.addActionListener(new newGameListener());
//****************************
p3.add(new JLabel("Difficulty"));
p3.add(new JComboBox(difficulty));
p3.add(new JLabel("Red Player is a")); // THESE TEXTS NEED TO BE RED
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel("Blue Player is a")); // THESE TEXTS NEED TO BE BLUE
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel(" "));
p3.add(new JLabel("Red Blue")); // DIFFERENT COLOR TEXT
p3.add(new JLabel("Score"));
p3.add(p3in);
p3.add(new JLabel(" "));
p3.add(new JButton("New Match")); //*****************************
p3.add(new JLabel(" "));
p3.add(new JLabel(" ")); // Add contents to the frame
setLayout(new BorderLayout(20,20));
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.WEST);
//p2.setAlignmentX(CENTER_ALIGNMENT);
//p3.setAlignmentX(CENTER_ALIGNMENT);
}
}class DrawGameBoard extends JPanel{ // implements MouseListener {
int height = 420;
int width = 490;
int gap = height/6;
JButton[] Jbs; DrawGameBoard(JButton[] Jbs) {
this.Jbs = Jbs;
}
protected void paintComponent(Graphics g) {
int[] diskInCol = new int[7];
super.paintComponent(g);
super.setSize(width,height);
// Draw Grids
for (int i = 0; i<7;i++ ) {g.drawLine(gap*i, 0, gap*i,height);}
for (int j = 0;j<6; j++){g.drawLine(0, gap*j, width,gap*j);}
g.drawLine(width-1, 0, width-1, height-1);
g.drawLine(0,height-1, width-1, height-1); for (int i=0;i<Jbs.length; i++){
Jbs[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("pressed!");
g.drawOval(0, 0, gap, gap);
}});
}
}
//public void drawDisk(MouseEvent e) {
// int X,Y;
//}
public void mousePressed(MouseEvent e) {//drawDisk(e);
}
public void mouseReleased(MouseEvent e) {}
}
第126行出现CompileError. 每一列底下的按钮被按下后, 要在Graphics g上画个圆(先暂时不考虑画在哪里。)。但是g不是final的不能引用。有什么办法可以解决这个问题? 作业马上要截至了,任何可行的方法都可以。请把想法或代码都告诉我。。如果做过类似的游戏的话有Code最好了多谢。
解决方案 »
- JAVA 控制POS机的双屏
- 为什么java对象不能直接被访问?
- 本人刚学SOFTWARE WORKSHOP,上了几个星期GROOVY,现在刚开始接触JAVA,有几个问题,有高人能帮忙看看
- 我也发个面试题:华为食堂面试
- 跪求Oreilly的Java网络编程的中文版电子书,本人不胜感激!
- 请问有人用java做桌面开发吗?这有前途吗?
- 关于汉字的读取显示问题...急!!!
- 为什么不能获取最新的ID??
- 难道中国程序员真的是XXXX?没人能够回答这个问题??第三次提问! 100分送上!
- 本人最近弄了一盘IBM WebSphere,所以想学JAVA,可一点基础都没有,所以请各位大哥传授些学习经验,外加好的学习资料或书。万分感激!!
- 怎么知道自己电脑装了JDK没有,以及版本号
- 请问一个根据坐标画图的问题~感激回答~感激不尽
Graphics g2;
protected void paintComponent(Graphics g) {
g2 = g;
int[] diskInCol = new int[7];
super.paintComponent(g);
super.setSize(width, height);
// Draw Grids
for (int i = 0; i < 7; i++) {
g.drawLine(gap * i, 0, gap * i, height);
}
for (int j = 0; j < 6; j++) {
g.drawLine(0, gap * j, width, gap * j);
}
g.drawLine(width - 1, 0, width - 1, height - 1);
g.drawLine(0, height - 1, width - 1, height - 1); for (int i = 0; i < Jbs.length; i++) {
Jbs[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("pressed!");
g2.drawOval(0, 0, gap, gap);
}
});
}
}
你试试这样行么 我点按钮什么都没画出来
paintComponent方法里面啊
改了下,你看看,
你批量给按钮加事件的方式也有些问题,你很快会发现的,这个我暂时没改import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.event.MouseInputAdapter;
import javax.swing.*;public class Pconnect4 {
public static void main(String[] args) {
Panels frame = new Panels();
frame.setTitle("Connect Four");
frame.setSize(755, 510);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class Panels extends JFrame {
public Panels() {
// Create button panel
JPanel pButton = new JPanel();
pButton.setLayout(new GridLayout(1, 7, 0, 0));
JButton Jb1 = new JButton("Add");
JButton Jb2 = new JButton("Add");
JButton Jb3 = new JButton("Add");
JButton Jb4 = new JButton("Add");
JButton Jb5 = new JButton("Add");
JButton Jb6 = new JButton("Add");
JButton Jb7 = new JButton("Add");
JButton Jbs[] = { Jb1, Jb2, Jb3, Jb4, Jb5, Jb6, Jb7 };
for (int i = 0; i < Jbs.length; i++) {
pButton.add(Jbs[i]);
} // Create panel p1 to draw the game board
JPanel p1 = new DrawGameBoard(Jbs); // Creat p2 to hold a text field, buttons, and p1
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout(20, 15));
// p2.setBackground(Color.white);
// NEED TO BE MODIFIED HERE ******************************
JLabel turn = new JLabel("User's turn");
p2.add(turn, BorderLayout.NORTH);
p2.add(p1, BorderLayout.CENTER);
p2.add(pButton, BorderLayout.SOUTH);
// System.out.println("added"); // Create p3in to hold 2 TextFileds to show scores
JPanel p3in = new JPanel();
p3in.setLayout(new GridLayout(1, 3));
p3in.add(new JTextField(3));
p3in.add(new JLabel(" VS "));
p3in.add(new JTextField(3)); // Create p3 to hold player interface
String[] playerSetting = { "Human" };
String[] difficulty = { "Default" };
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(11, 2, 5, 10)); // p3.setBackground(Color.white);
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
JButton newGame = new JButton("New Game");
p3.add(newGame);
// newGame.addActionListener(new newGameListener());
// ****************************
p3.add(new JLabel("Difficulty"));
p3.add(new JComboBox(difficulty));
p3.add(new JLabel("Red Player is a")); // THESE TEXTS NEED TO BE RED
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel("Blue Player is a")); // THESE TEXTS NEED TO BE BLUE
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel(" "));
p3.add(new JLabel("Red Blue")); // DIFFERENT COLOR TEXT
p3.add(new JLabel("Score"));
p3.add(p3in);
p3.add(new JLabel(" "));
p3.add(new JButton("New Match")); // *****************************
p3.add(new JLabel(" "));
p3.add(new JLabel(" ")); // Add contents to the frame
setLayout(new BorderLayout(20, 20));
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.WEST); // p2.setAlignmentX(CENTER_ALIGNMENT);
// p3.setAlignmentX(CENTER_ALIGNMENT);
}
}class DrawGameBoard extends JPanel { // implements MouseListener {
int[] diskInCol = new int[7];
int height = 420;
int width = 490;
int gap = height / 6;
int round = 0;
JButton[] Jbs; DrawGameBoard(JButton[] Jbs) {
this.Jbs = Jbs;
for (int i = 0; i < Jbs.length; i++) {
Jbs[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("pressed!");
round = 1;
repaint();
}
});
}
} protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw Grids
for (int i = 0; i < 7; i++) {
g.drawLine(gap * i, 0, gap * i, height);
}
for (int j = 0; j < 6; j++) {
g.drawLine(0, gap * j, width, gap * j);
}
g.drawLine(width - 1, 0, width - 1, height - 1);
g.drawLine(0, height - 1, width - 1, height - 1);
if (round == 1) {
g.drawOval(0, 0, gap, gap);
}
} public void mousePressed(MouseEvent e) {// drawDisk(e);
} public void mouseReleased(MouseEvent e) {
}
}
谢谢你的回复! 但是好莫名啊。。我按照你的方法加g2在那里以后,每次跑都出现Exception in thread "main" java.lang.VerifyError: (class: Panels, method: <init> signature: ()V) Constructor must call super() or this()
at Pconnect4.main(Pconnect4.java:9)
。泪奔
就是这个问题,为了区分你的事件,你的按钮的文本名称最好不一样,这样好判断
不多说了,看代码吧,改了下
示例:
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.event.MouseInputAdapter;
import javax.swing.*;public class Pconnect4 {
public static void main(String[] args) {
Panels frame = new Panels();
frame.setTitle("Connect Four");
frame.setSize(755, 510);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class Panels extends JFrame {
public Panels() {
// Create button panel
JPanel pButton = new JPanel();
pButton.setLayout(new GridLayout(1, 7, 0, 0));
JButton Jb1 = new JButton("Add1");
JButton Jb2 = new JButton("Add2");
JButton Jb3 = new JButton("Add3");
JButton Jb4 = new JButton("Add4");
JButton Jb5 = new JButton("Add5");
JButton Jb6 = new JButton("Add6");
JButton Jb7 = new JButton("Add7");
JButton Jbs[] = { Jb1, Jb2, Jb3, Jb4, Jb5, Jb6, Jb7 };
for (int i = 0; i < Jbs.length; i++) {
pButton.add(Jbs[i]);
} // Create panel p1 to draw the game board
JPanel p1 = new DrawGameBoard(Jbs); // Creat p2 to hold a text field, buttons, and p1
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout(20, 15));
// p2.setBackground(Color.white);
// NEED TO BE MODIFIED HERE ******************************
JLabel turn = new JLabel("User's turn");
p2.add(turn, BorderLayout.NORTH);
p2.add(p1, BorderLayout.CENTER);
p2.add(pButton, BorderLayout.SOUTH);
// System.out.println("added"); // Create p3in to hold 2 TextFileds to show scores
JPanel p3in = new JPanel();
p3in.setLayout(new GridLayout(1, 3));
p3in.add(new JTextField(3));
p3in.add(new JLabel(" VS "));
p3in.add(new JTextField(3)); // Create p3 to hold player interface
String[] playerSetting = { "Human" };
String[] difficulty = { "Default" };
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(11, 2, 5, 10)); // p3.setBackground(Color.white);
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
JButton newGame = new JButton("New Game");
p3.add(newGame);
// newGame.addActionListener(new newGameListener());
// ****************************
p3.add(new JLabel("Difficulty"));
p3.add(new JComboBox(difficulty));
p3.add(new JLabel("Red Player is a")); // THESE TEXTS NEED TO BE RED
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel("Blue Player is a")); // THESE TEXTS NEED TO BE BLUE
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel(" "));
p3.add(new JLabel("Red Blue")); // DIFFERENT COLOR TEXT
p3.add(new JLabel("Score"));
p3.add(p3in);
p3.add(new JLabel(" "));
p3.add(new JButton("New Match")); // *****************************
p3.add(new JLabel(" "));
p3.add(new JLabel(" ")); // Add contents to the frame
setLayout(new BorderLayout(20, 20));
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.WEST); // p2.setAlignmentX(CENTER_ALIGNMENT);
// p3.setAlignmentX(CENTER_ALIGNMENT);
}
}class DrawGameBoard extends JPanel implements ActionListener { // implements MouseListener {
int[] diskInCol = new int[7];
int height = 420;
int width = 490;
int gap = height / 6;
int round = 0;
JButton[] Jbs; DrawGameBoard(JButton[] Jbs) {
this.Jbs = Jbs;
for (int i = 0; i < Jbs.length; i++) {
Jbs[i].addActionListener(this);
}
} protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw Grids
for (int i = 0; i < 7; i++) {
g.drawLine(gap * i, 0, gap * i, height);
}
for (int j = 0; j < 6; j++) {
g.drawLine(0, gap * j, width, gap * j);
}
g.drawLine(width - 1, 0, width - 1, height - 1);
g.drawLine(0, height - 1, width - 1, height - 1);
if (round != 0) {
g.drawOval((round-1)*gap, 0, gap, gap);
}
} public void mousePressed(MouseEvent e) {// drawDisk(e);
} public void mouseReleased(MouseEvent e) {
} @Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equals("Add1")) {
round = 1;
} else if (action.equals("Add2")) {
round = 2;
} else if (action.equals("Add3")) {
round = 3;
} else if (action.equals("Add4")) {
round = 4;
} else if (action.equals("Add5")) {
round = 5;
} else if (action.equals("Add6")) {
round = 6;
} else if (action.equals("Add7")) {
round = 7;
} else {
round = 0;
}
repaint();
}
}
多谢!满分送给你。后来我又有了些进展。。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;public class Pconnect4 {
public static void main(String[] args) {
Panels frame = new Panels();
frame.setTitle("Connect Four");
frame.setSize(755,510);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class Panels extends JFrame {
public Panels() {
// Create button panel
JPanel pButton = new JPanel();
final OvalDrawer p1 = new OvalDrawer(420,490);
pButton.setLayout(new GridLayout(1,7,0,0)); JButton jbs[] = new JButton[7]; for(int i = 0; i < jbs.length; i++) {
jbs[i] = new JButton("Add");
pButton.add(jbs[i]);
jbs[i].addActionListener(new ActionListenerWithTag(i, p1));
}
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout(20,15));
JLabel turn = new JLabel("User's turn");
p2.add(turn, BorderLayout.NORTH);
p2.add(p1, BorderLayout.CENTER);
p2.add(pButton, BorderLayout.SOUTH); // Create p3in to hold 2 TextFileds to show scores
JPanel p3in = new JPanel();
p3in.setLayout(new GridLayout(1,3));
p3in.add(new JTextField(3));
p3in.add(new JLabel(" VS "));
p3in.add(new JTextField(3)); // Create p3 to hold player interface
String[] playerSetting = {"Human"};
String[] difficulty = {"Default"};
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(11,2,5,10)); //p3.setBackground(Color.white);
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
p3.add(new JLabel(" "));
JButton newGame = new JButton("New Game");
newGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
p1.cleanUp();
p1.repaint(); }
});
p3.add(newGame);
//****************************
p3.add(new JLabel("Difficulty"));
p3.add(new JComboBox(difficulty));
p3.add(new JLabel("Red Player is a")); // THESE TEXTS NEED TO BE RED
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel("Blue Player is a")); // THESE TEXTS NEED TO BE BLUE
p3.add(new JComboBox(playerSetting));
p3.add(new JLabel("called"));
p3.add(new TextField(10)); // ACTION
p3.add(new JLabel(" "));
p3.add(new JLabel("Red Blue")); // DIFFERENT COLOR TEXT
p3.add(new JLabel("Score"));
p3.add(p3in);
p3.add(new JLabel(" "));
JButton newMatch = new JButton("New Match");
newMatch.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
p1.cleanUp();
p1.repaint();
} });
p3.add(newMatch);
p3.add(new JLabel(" "));
p3.add(new JLabel(" ")); // Add contents to the frame
setLayout(new BorderLayout(20,20));
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.WEST); }
}class OvalDrawer extends JPanel{
private int height, width, gap, diskInCol[] = new int[7];
OvalDrawer(int h, int w) {
height = h;
width = w;
gap = height/6;
// initialize the count of disk in each column
for(int i = 0; i < diskInCol.length; i++)
diskInCol[i] = 0;
}
void addOval(int index){
diskInCol[index] ++;
}
void cleanUp(){
for(int i = 0; i < diskInCol.length; i++)
diskInCol[i] = 0;
}
public void whoWins() {
} protected void paintComponent(Graphics g){
super.paintComponent(g);
super.setSize(width,height); for(int m = 0; m < 7;m++) {
for(int n = 1; n <= diskInCol[m]; n++)
g.drawOval(gap * m, height - gap * n , gap, gap);
//g.setColor(Color.RED);
} for (int i = 0; i<7;i++ ) {g.drawLine(gap*i, 0, gap*i,height);}
for (int j = 0;j<6; j++){g.drawLine(0, gap*j, width,gap*j);}
g.drawLine(width-1, 0, width-1, height-1);
g.drawLine(0,height-1, width-1, height-1);
for (int i = 0; i<7;i++ ) {g.drawLine(gap*i, 0, gap*i,height);}
for (int j = 0;j<6; j++){g.drawLine(0, gap*j, width,gap*j);}
g.drawLine(width-1, 0, width-1, height-1);
g.drawLine(0,height-1, width-1, height-1);
}
}class ActionListenerWithTag implements ActionListener{
int tag;
OvalDrawer oval;
int countGames;
int[] score1, score2; ActionListenerWithTag(int t, OvalDrawer d)
{
tag = t;
oval = d;
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
oval.addOval(tag);
oval.repaint();
oval.whoWins(); }}现在的问题是在哪里放Player的Method分别记分,还有判定输赢的class或者Method。。要是可以大概说一下也行。。>.<另外Oval不能上色(实心)呢!g.setColor会把整个颜色都变成红色