저는 선생님이 제공 한 JFrame과 카드 이미지를 사용하여 기본적인 BlackJack Window 프로그램을 작성했습니다. 나는 그것이 내가 컴파일 할 때 전혀 나타나지 않을 것이라고 생각하는 것에서 모든것을 썼다. 내가 뭔가를 놓친 거니?BlackJack 윈도우 프로그램이 나타나지 않습니다
그러나 프로그램을 컴파일하면 라이브러리 BlackJack.java에서 파일을 실행 한 후에 아무 것도 실행되지 않고 프로그램이 표시되지 않습니다. 명령 프롬프트에서 실행되었음을 나타내지 만 화면에 BlackJack/JFrame이 나타나지 않습니다.
JFrame이 즉시 실행되도록 BlackJack 클래스 및 MyJFrame 클래스를 조정하려고했지만 작동하지 않는 것 같습니다. 이전 과제에서는이 BlackJack 클래스 용 JFrame을 작성해야 했으므로 광산은 정상적으로 작동했습니다. 따라서 BlackJack 도구가 문제를 일으키는 것처럼 느껴집니다.
참고 : 코드의 시작 지점,
수동으로 코드를 실행을 시작할 수 있습니다 : 우리는
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;
public class BlackJack extends JFrame implements ActionListener
{
private JLabel[] lblPlayer = new JLabel[8];
private JLabel[] lblDealer = new JLabel[8];
private JPanel pnlPlayDeal = new JPanel(new GridLayout(2,8));
private DeckOfCards deck = new DeckOfCards();
private Vector <String> playerCardNames = new Vector<String>();
private Vector <String> dealerCardNames = new Vector<String>();
private ImageIcon[] icoPlayerHand = new ImageIcon[8];
private ImageIcon[] icoDealerHand = new ImageIcon[8];
private JButton btnDeal = new JButton("Deal");
private JButton btnPlayer = new JButton("Player");
private JButton btnDealer = new JButton("Dealer");
private JButton btnNew = new JButton("New");
private JButton btnAuthor = new JButton("Author");
private JPanel pnlButtons = new JPanel(new FlowLayout());
private int count = 1;
private int playerval = 0;
private int dealerval = 0;
public void MyJFrame()
{
for(int i=0;i<8;i++)
{
lblPlayer[i] = new JLabel("Player");
lblDealer[i] = new JLabel("Dealer");
}
this.setLocationRelativeTo(null);
this.setSize(800, 350);
this.setVisible(true);
add(pnlPlayDeal,BorderLayout.CENTER);
add(pnlButtons,BorderLayout.SOUTH);
this.addLabels();
this.addButtons();
registerListeners();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addLabels()
{
for(int i= 0;i<8;i++)
{
pnlPlayDeal.add(lblPlayer[i]);
}
for(int i= 0;i<8;i++)
pnlPlayDeal.add(lblDealer[i]);
}
public void addButtons()
{
pnlButtons.add(btnDeal);
pnlButtons.add(btnPlayer);
btnPlayer.setEnabled(false);
pnlButtons.add(btnDealer);
btnDealer.setEnabled(false);
pnlButtons.add(btnNew);
btnNew.setEnabled(false);
pnlButtons.add(btnAuthor);
}
public void registerListeners()
{
btnDeal.addActionListener(this);
btnPlayer.addActionListener(this);
btnDealer.addActionListener(this);
btnNew.addActionListener(this);
btnAuthor.addActionListener(this);
}
public int findRank(String x)
{
int result=0;
if(x.startsWith("Two"))
result=2;
else if(x.startsWith("Three"))
result=3;
else if(x.startsWith("Four"))
result=4;
else if(x.startsWith("Five"))
result=5;
else if(x.startsWith("Six"))
result=6;
else if(x.startsWith("Seven"))
result=7;
else if(x.startsWith("Eight"))
result=8;
else if(x.startsWith("Nine"))
result=9;
else if(x.startsWith("Ten"))
result=10;
else if(x.startsWith("Jack"))
result=10;
else if(x.startsWith("Queen"))
result=10;
else if(x.startsWith("King"))
result=10;
else if(x.startsWith("Ace"))
result=11;
return result;
}
public int getVal (Vector<String> v)
{
int total=0;
Iterator <String> iter = v.iterator();
while(iter.hasNext())
{
total+= findRank(iter.next());
}
return total;
}
public void deal()
{
deck.shuffle();
playerCardNames.add(deck.deal());
playerCardNames.add(deck.deal());
dealerCardNames.add(deck.deal());
dealerCardNames.add(deck.deal());
icoPlayerHand[0] = new ImageIcon("cardImages/"
+playerCardNames.get(0)+".gif");
icoPlayerHand[1] = new ImageIcon("cardImages/"
+playerCardNames.get(1)+".gif");
lblPlayer[0].setIcon(icoPlayerHand[0]);
lblPlayer[1].setIcon(icoPlayerHand[1]);
icoDealerHand[0] = new ImageIcon("cardImages/"+
dealerCardNames.get(0)+".gif");
icoDealerHand[1] = new ImageIcon("cardImages/card.gif");
lblDealer[0].setIcon(icoDealerHand[0]);
lblDealer[1].setIcon(new ImageIcon("cardImages/card.gif"));
btnDeal.setEnabled(false);
btnPlayer.setEnabled(true);
count ++;
}
public void player()
{
while(getVal(playerCardNames)<22)
{
String choice = JOptionPane.showInputDialog(null, "you have "
+getVal(playerCardNames)+" h/s",null);
if(choice.equalsIgnoreCase("h"))
{
playerCardNames.add(deck.deal());
icoPlayerHand[count] = new ImageIcon("cardImages/"
+playerCardNames.lastElement()+".gif");
lblPlayer[count].setIcon(icoPlayerHand[count]);
count++;
}
else
break;
}
if(getVal(playerCardNames)>21)
{
JOptionPane.showMessageDialog(this, "You Busted");
btnPlayer.setEnabled(false);
btnNew.setEnabled(true);
}
else
{
btnPlayer.setEnabled(false);
btnDealer.setEnabled(true);
}
}
public void dealer()
{
btnDealer.setEnabled(false);
count=1;
icoDealerHand[count] = new ImageIcon("cardImages/"
+dealerCardNames.lastElement()+".gif");
lblDealer[1].setIcon(icoDealerHand[count]);
count ++;
while(getVal(dealerCardNames)<17)
{
dealerCardNames.add(deck.deal());
icoDealerHand[count] = new ImageIcon("cardImages/"
+dealerCardNames.lastElement()+".gif");
lblDealer[count].setIcon(icoDealerHand[count]);
count ++;
}
if(getVal(dealerCardNames)>21)
JOptionPane.showMessageDialog(this, "Dealer bust you won");
else
whoWon();
btnNew.setEnabled(true);
}
public void whoWon()
{
if(getVal(playerCardNames)>getVal(dealerCardNames))
JOptionPane.showMessageDialog(this,"You won");
else
JOptionPane.showMessageDialog(this,"You lost");
}
public void newGame()
{
this.setVisible(false);
BlackJack game = new BlackJack();
}
public static void main(String[]args)
{
MyJFrame table= new MyJFrame();
BlackJack first = new BlackJack();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnAuthor)
((JButton)e.getSource()).setText("Conner M");
if(e.getSource()==btnDeal)
deal();
if(e.getSource()==btnPlayer)
player();
if(e.getSource()==btnDealer)
dealer();
if(e.getSource()==btnNew)
{
newGame();
}
}
}
안녕하세요. Conner, StackOverflow에 오신 것을 환영합니다. 귀하의 질문에 귀하를 도울 올바른 정보가 없습니다. 예 : 우리는 "컴파일 할 때 전혀 나타나지 않는다"는 것을 의미하지 않으며, 지금까지 문제를 해결하기 위해 수행 한 작업을 말하지 않았습니다. http : // stackoverflow를 읽으십시오.com/help/how-to-ask를 입력하고 질문을 다음과 같이 업데이트하십시오. ** 달성하고자하는 것, 해결하기 위해 시도한 것, 작동하지 않는 방법 ** – Tim
Simplify JFrame을 보여 주려는 위치에 대한 예제. 일단 당신이 그 일을하면 더 많은 기능 레이어를 다시 추가 할 수 있습니다. 다음과 같이 웹에서 JFrames를 보여주는 간단한 예제를 볼 수 있습니다. http://alvinalexander.com/java/jframe-example setVisible (true) 그리고 예제가 설정 한시기와 장소. –
여기에 도움이되는 몇 가지 [디버깅 팁] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 살펴보십시오. 확실하지 않은 특정 문제가 발생하면 자유롭게 그 문제에 대해 물어보십시오. –