在使用 JPanel、2D Graphics 和 MouseListener 时遇到问题



当我试图为最终项目制作二十一点游戏时,我的代码遇到了问题,我让板子全部用 2D 图形制作,图像可以工作,但是当我尝试添加键盘监听器或鼠标监听器甚至 JButtons 等控件时,它们似乎从未工作过。目前我正在尝试使用 mouseListener,所以我可以单击它说要点击的区域,但它似乎不想工作。我假设它与图形阻止 mouseListener 功能或未正确排序有关。

我尝试使用JPanells和contentPanes,但我永远无法让它们很好地相互配合。

CardValue()只是从数组中随机确定一张牌,而油漆就是很好的油漆。

我还在底部添加了我的 MouseListener 类,这是一个与二十一点不同的类

如果有人能帮助我理解为什么 MouseListener 无法处理图形或解决我的问题,那将是惊人的,因为我对编码很陌生并且不完全理解图形

import java.util.Random;
import java.util.Stack;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.File;
import javax.imageio.ImageIO;
@SuppressWarnings("unused")
public class Blackjack extends Canvas {
//Declaring Variables for images, Strings, and other things
//Image Cards, cardback1, cardback2, dealer, you;
static String blah;
static int dealerstotal = 0;
public static Stack <Integer> cards = new Stack<Integer>();
//An array that prints out a random card and gives me the value of the cards, along with the name
//so i can match it up with an image of the card with a match name
public static void CardValue() {
for (int i = 1; i<53; i++) {
cards.add(i);
}
String suit1 ="", number1="";
Random r = new Random();
int suit = r.nextInt(4);
switch (suit) {
case 0: suit1 = "c"; break;
case 1: suit1 = "d"; break;
case 2: suit1 = "h"; break;
case 3: suit1 = "s"; break;
}
int number = r.nextInt(13)+1;
switch (number) {
case 1: number1 = "1"; break;
case 2: number1 = "2"; break;
case 3: number1 = "3"; break;
case 4: number1 = "4"; break;
case 5: number1 = "5"; break;
case 6: number1 = "6"; break;
case 7: number1 = "7"; break;
case 8: number1 = "8"; break;
case 9: number1 = "9"; break;
case 10: number1 = "10"; break;
case 11: number1 = "11"; break;
case 12: number1 = "12"; break;
case 13: number1 = "13"; break;
}
blah = suit1+number1;
if (number>=10) {
number = 10;
}
if (number == 1) {
}
dealerstotal+=number;
int newcard = (suit*13)+number;
System.out.println(blah);
cards.remove(newcard);
}
public Blackjack() throws Exception
{
//Declaring images, I //'d all these out because I didn't include the downloads for these images
//Cards = ImageIO.read( new File(blah+ ".png") );
//dealer = ImageIO.read( new File("dealer.jpg") );
//you = ImageIO.read( new File("you.jpg") );
//cardback1 = ImageIO.read(new File("redback.png"));
//cardback2 = ImageIO.read(new File("blackback.png"));
}
//Creating the game board
public void paint( Graphics g )
{
int l = 5;
Color table = new Color(74, 179, 0);
Color blues = new Color(13, 38, 163);
String title = "Welcome to Kris' Casino";
String dealers = "Dealers Cards";
String dealerst = "Dealers Total: "+dealerstotal;
String your = "Your Cards";
String yourt = "Your Total: "+l;
String game = "The Game is Blackjack, Goodluck";
String hit = "Press Up to Hit";
String stand = "Press Down to Stand";
g.setColor(table);
g.fillRect(0,0,10000,10000);
//Title/Pictures
g.setColor(Color.black);
g.fillRect(40,40,254,334);
g.fillRect(40,420,254,334);
//g.drawImage(dealer,50,50,this);
//g.drawImage(you,50,430,this);
g.setFont(new Font("Arial", Font.BOLD, 51));
g.setColor(Color.black);
g.drawString(title, 400, 90);
g.setColor(Color.YELLOW);
g.drawString(title, 403, 93);
g.setFont(new Font("Arial", Font.BOLD, 30));
g.setColor(Color.black);
g.drawString(game, 450, 140);
g.setColor(Color.YELLOW);
g.drawString(game, 452, 142);
g.setColor(Color.black);
g.fillRect(400, 105, 595, 5);
//Dealers Graphics
//Dealers Cards
g.drawString(dealers, 430, 240);
g.setColor(Color.red);
g.fillRect(450, 265, 82, 105);
g.fillRect(550, 265, 82, 105);
g.fillRect(650, 265, 82, 105);
g.fillRect(750, 265, 82, 105);
g.fillRect(850, 265, 82, 105);
g.drawString(dealers, 432, 242);
//g.drawImage(Cards,455,270,this);
//g.drawImage(cardback1,555,270,this);
//g.drawImage(cardback1,655,270,this);
//g.drawImage(cardback1,755,270,this);
//g.drawImage(cardback1,855,270,this);
//Dealers Total
g.setColor(Color.black);
g.drawString(dealerst, 730, 240);
g.setColor(Color.red);
g.drawString(dealerst, 732, 242);
//Yours Cards
g.setColor(Color.white);
g.drawString(your, 452, 440);
g.setColor(blues);
g.fillRect(450, 465, 82, 105);
g.fillRect(550, 465, 82, 105);
g.fillRect(650, 465, 82, 105);
g.fillRect(750, 465, 82, 105);
g.fillRect(850, 465, 82, 105);
g.drawString(your, 450, 438);
//g.drawImage(cardback2,455,470,this);
//g.drawImage(cardback2,555,470,this);
//g.drawImage(cardback2,655,470,this);
//g.drawImage(cardback2,755,470,this);
//g.drawImage(cardback2,855,470,this);
g.setColor(Color.white);
g.drawString(yourt, 750, 440);
g.setColor(blues);
g.drawString(yourt, 748, 438);
g.setColor(Color.black);
g.fillRect(1116, 200, 1000, 70);
g.setColor(Color.black);
g.fillRect(1032, 300, 1000, 70);
g.setColor(Color.white);
g.drawString(hit, 1134, 245);
g.drawString(stand, 1050, 345);
}
//Here is where I'm having my issues, Whenever I add "frame.add(new Blackjack());" mouseListener doesnt work
//mouseListener only works when I don't have "frame.add(new Blackjack());"
//Im guessing this is some kind of issue to do with graphics ontop of the mouseListener/JFrame 
//disabling it from working
public static void Frame() throws Exception {
JFrame frame = new JFrame("Kris' Casino");
frame.setSize(500,500);
frame.getContentPane().addMouseListener(new ClickListener());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.add(new Blackjack());
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
CardValue();
Frame();
}
}

这是鼠标侦听器的第二个类

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ClickListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
System.out.println(e.getX() + "," + e.getY());
}
}
public static void Frame() throws Exception {
JFrame frame = new JFrame("Kris' Casino");
frame.setSize(500,500);
frame.getContentPane().addMouseListener(new ClickListener());  // *** 1 ***
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.add(new Blackjack());                                    // *** 2 ***
frame.setVisible(true);
}

请注意,您将 MouseListener 添加到 JFrame 的内容窗格中*** 1 ***,然后您在*** 2 ***处用Blackjack 组件覆盖 contentPane,因此 MouseListener 无法工作,因为它正在侦听的组件已被完全覆盖,具有重量级组件(Canvas 对象(。

解决方案:将鼠标侦听器添加到显示的组件中,即二十一点组件

也:

  • 不要扩展 Canvas,而是扩展 JPanel
  • 重写 JPanel 的 paintComponent 方法
  • 始终在重写中调用 super's draw 方法,因此,如果重写 paintComponent,则调用super.paintComponent(g);,如果重写 paint 方法,则调用super.paint(g);

相关内容

  • 没有找到相关文章

最新更新