[英]Jpanel doesn't draw when another class is loaded
I know exactly what line is causing the problem, but there are no stack trace errors being shown, so I'm left with debugging. 我确切地知道导致问题的是哪一行,但是没有显示堆栈跟踪错误,所以我留下了调试。 The problem that has got me is that I thought the panel didn't have focus.
让我感到困惑的是,我认为小组没有焦点。 When first opened, the panel is blank, and not drawn, but when I minimize and restore, the panel gets painted.
首次打开时,面板是空白的,而不是绘制的,但是当我最小化并恢复时,面板会被绘制。 I tried adding
panel.requestFocus()
and panel.requestFocusInWindow()
to the constructor of the class that is being initialized before the screen is painted, but it doesn't seem to do anything. 我尝试将
panel.requestFocus()
和panel.requestFocusInWindow()
到在绘制屏幕之前初始化的类的构造函数,但它似乎没有做任何事情。 Here is the code: 这是代码:
JPanel class: JPanel类:
package blackjack;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Board extends JPanel {
int dealer = 0;
int player = 0;
int money = 2500;
Deck deck;
Player p = new Player();
public Board() {
deck = new Deck(false, this); //if I comment this out, the panel gets painted straight away
}
public void paint(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillRect(0,0,getWidth(),getHeight());
repaint();
}
}
And the class that is causing the problem: 导致问题的类:
package blackjack;
import java.awt.Image;
import java.util.Random;
import javax.swing.ImageIcon;
public class Deck {
Card[] deck = new Card[52];
int[] used = new int[52];
Random r = new Random();
public Deck(boolean shuffle, Board b) {
int i = 0;
for(int x=0; x<51; x++) {
used[x] = -1;
}
for(Cards card : Cards.values()) {
if(card.getId() != 53) {
Image image = new ImageIcon(this.getClass().getResource(card.getImagePath())).getImage();
deck[i] = new Card(card.getId(),card.getValue(),image);
i = i+1;
}
}
if(shuffle) shuffle();
b.requestFocus();
b.requestFocusInWindow();
}
public void shuffle() {
Card[] shuffled = new Card[52];
for(int x=0; x<51; x++) {
int i = pickNotUsed();
if(i==-1) break;
shuffled[x] = deck[i];
}
deck = shuffled;
}
private int pickNotUsed() {
int notpicked = r.nextInt(52);
for(int x=0; x<51; x++) {
if(used[x] != notpicked) {
return notpicked;
}
}
return -1;
}
public Card[] getCards() {
return deck;
}
public Card drawCard() {
for(int x=0; x<deck.length; x++) {
if(deck[x] != null) {
return deck[x];
}
}
return null;
}
}
original class, extends JFrame package blackjack; 原始类,扩展JFrame包二十一点;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Blackjack extends JFrame {
public static void main(String[] args) {
new Blackjack();
}
public Blackjack() {
setSize(1000,600);
setTitle("Blackjack");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
add(new Board());
}
}
A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent.
一个只想专门化UI(外观)委托的paint方法的子类应该只覆盖paintComponent。
Anyway, even in this state you application shows me the green background frame from the start. 无论如何,即使在这种状态下,你的应用程序也会从一开始就向我展示绿色背景框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.