简体   繁体   English

如何定位按钮-Java

[英]How to position buttons - java

I am trying to code a card game in java in which i need to position some buttons below the cards. 我正在尝试用Java编写纸牌游戏,我需要在纸牌下放置一些按钮。 i have looked into layout managers but i don't know exactly how to use them in the context i'm trying too. 我已经研究过布局管理器,但是我也不知道该如何在我正在尝试的上下文中使用它们。 My code is as follows(i also have a card class but that is working fine): 我的代码如下(我也有一个卡片类,但工作正常):

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 not finished coding my button but i'd like to be able to position it first. 即时消息还没有完成我按钮的编码,但我希望能够先将其定位。

You need to separate the logic of your custom painting from the logic of...everything else. 您需要将自定义绘画的逻辑与其他所有逻辑分开。

  • Don't override paint of top level containers like JFrame . 不要覆盖JFrame类的顶级容器的paint Apart from the fact that they are not double buffered, you will end up painting under the frame decorations and over the top of the child components. 除了它们不是双重缓冲的事实之外,您最终还会在框架装饰下和子组件的顶部上画画。 When a opaque child component is updated, the parent container will not be notified, this means any painting your relying on your frame to perform will not be updated. 更新不透明的子组件时,不会通知父容器,这意味着依赖框架执行的任何绘画都不会更新。 Instead, create a custom component, extending from something like JPanel and override it's paintComponent method and put your custom painting here. 相反,创建一个自定义组件,从诸如JPanel扩展并覆盖它的paintComponent方法,然后将自定义绘画放在此处。
  • Separate the game logic from the view. 将游戏逻辑与视图分开。 Use some kind of model, with which you can interact with and which can provide notifications to the various views of the game. 使用某种模型,您可以与之交互并向游戏的各种视图提供通知。 This improves the communication channels and decouples your code. 这改善了沟通渠道并解耦了您的代码。

Create an instance of a JFrame and add your an instance of your custom panel to it. 创建JFrame的实例,然后向其中添加自定义面板的实例。 Create an instance of something like JPanel , set it's layout to something like FlowLayout and add your JButton s to it. 创建类似JPanel的实例,将其布局设置为类似FlowLayout的实例,然后将JButton添加到其中。 Place this (button) panel to the SOUTH position of the frame, for example... 将此(按钮)面板放置到框架的SOUTH位置,例如...

frame.add(panel, BorderLayout.SOUTH);

JFrame uses a BorderLayout by default. JFrame默认使用BorderLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM