0
我想在java中編寫一個卡片遊戲,我需要在卡片下面放置一些按鈕。我已經看過佈局經理,但我不知道如何在上下文中使用它們。 我的代碼如下(我也有一個卡類,但其工作正常):如何定位按鈕 - java
public class Elevens extends Frame implements MouseListener, ActionListener
{
private BufferedImage buffer;`
Graphics2D h;
private ArrayList<card> randDeck;
private Random generator;
private card currentStuff[] = new card[9];
private ArrayList<card> deck;
private boolean card1, card2, card3, card4, card5, card6, card7, card8, card9;
private JButton replace;
public Elevens()
{
JFrame frame = new JFrame("Elevens");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
generator = new Random();
deck = new ArrayList<card>();
randDeck = new ArrayList<card>();
addMouseListener(this);
replace = new JButton("Replace");
replace.setActionCommand("replace");
replace.addActionListener(this);
frame.add(replace);
card1 = false;
card2 = false;
card3 = false;
card4 = false;
card5 = false;
card6 = false;
card7 = false;
card8 = false;
card9 = false;
for(int i = 1; i < 53; i++)
{
card c = new card();
if(i < 14)
{
c.setSuit('S');
c.setValue(i);
deck.add(c);
}
else if(i < 27 && i >= 14)
{
c.setSuit('D');
c.setValue(i - 13);
deck.add(c);
}
else if(i < 40 && i >= 27)
{
c.setSuit('C');
c.setValue(i - 26);
deck.add(c);
}
else
{
c.setSuit('H');
c.setValue(i - 39);
deck.add(c);
}
}
while(deck.size() > 0)
{
int index = generator.nextInt(deck.size());
randDeck.add(deck.get(index));
deck.remove(index);
}
for(int i = 0; i < 9; i++)
{
currentStuff[i] = randDeck.get(i);
randDeck.remove(i);
}
}
public static void main(String[]arg)
{
Elevens e = new Elevens();
e.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
e.show();
}
public void paint(Graphics g)
{
h = (Graphics2D)g;
g.setColor(new Color(0, 51, 25));
g.fillRect(100, 300, 500, 500);
for(int i = 0; i < 9; i++)
{
int x = (i * 100) + 150, y = 360;
card c = currentStuff[i];
if(i > 2)
{
x = (i - 3) * 100 + 150;
y = 470;
}
if(i > 5)
{
x = (i - 6) * 100 + 150;
y = 580;
}
c.drawCard(x, y, 80, 100, g, this);
}
g.setColor(Color.RED);
h.setStroke(new BasicStroke(3));
if(card1)
{
g.drawRect(150, 360, 80, 100);
}
if(card2)
{
g.drawRect(250, 360, 80, 100);
}
if(card3)
{
g.drawRect(350, 360, 80, 100);
}
if(card4)
{
g.drawRect(150, 470, 80, 100);
}
if(card5)
{
g.drawRect(250, 470, 80, 100);
}
if(card6)
{
g.drawRect(350, 470, 80, 100);
}
if(card7)
{
g.drawRect(150, 580, 80, 100);
}
if(card8)
{
g.drawRect(250, 580, 80, 100);
}
if(card9)
{
g.drawRect(350, 580, 80, 100);
}
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
int x = e.getX(), y = e.getY();
if(x >= 150 && y >= 360 && x < 230 && y < 460)
{
if(card1)
{
card1 = false;
}
else
{
card1 = true;
}
}
if(x >= 250 && y >= 360 && x < 330 && y < 460)
{
if(card2)
{
card2 = false;
}
else
{
card2 = true;
}
}
if(x >= 350 && y >= 360 && x < 430 && y < 460)
{
if(card3)
{
card3 = false;
}
else
{
card3 = true;
}
}
if(x >= 150 && y >= 470 && x < 230 && y < 570)
{
if(card4)
{
card4 = false;
}
else
{
card4 = true;
}
}
if(x >= 250 && y >= 470 && x < 330 && y < 570)
{
if(card5)
{
card5 = false;
}
else
{
card5 = true;
}
}
if(x >= 350 && y >= 470 && x < 430 && y < 570)
{
if(card6)
{
card6 = false;
}
else
{
card6 = true;
}
}
if(x >= 150 && y >= 580 && x < 230 && y < 680)
{
if(card7)
{
card7 = false;
}
else
{
card7 = true;
}
}
if(x >= 250 && y >= 580 && x < 330 && y < 680)
{
if(card8)
{
card8 = false;
}
else
{
card8 = true;
}
}
if(x >= 350 && y >= 580 && x < 430 && y < 680)
{
if(card9)
{
card9 = false;
}
else
{
card9 = true;
}
}
repaint();
}
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
if(action.equals("replace"))
{
if(card1)
{
}
card1 = false;
card2 = false;
card3 = false;
card4 = false;
card5 = false;
card6 = false;
card7 = false;
card8 = false;
card9 = false;
}
}
}`
IM沒有完成編碼我的按鈕,但我希望能夠將它定位第一。
所以在我主我應該做些更喜歡這個'公共靜態無效的主要(字串[] ARG) \t { \t \t的JFrame幀=新的JFrame( 「7-11」)?; \t \t frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); \t \t frame.setVisible(true); \t \t \t \t \t Elevens e = new Elevens(); \t \t e.setSize(900,900); \t \t e.addWindowListener(新WindowAdapter的() { \t公共無效的windowClosing(WindowEvent E) \t { \t \t System.exit(0); \t} }); \t e.show(); \t frame.add(e); \t}' – koibeast
您可以使用JFrame#setDfaultCloseOperation並傳遞它JFrame.EXIT_ON_CLOSE它將在窗口關閉時關閉JVM,因此您不需要窗口偵聽器,但是,是的,這是主意 – MadProgrammer