JButton 绝对位于我的 Paint() 板下方

发布于 2024-11-28 11:50:32 字数 1623 浏览 0 评论 0原文

我有一个扑克游戏,我设计了一个漂亮的 GUI 来显示纸牌和玩家。我用大量的 g2d.drawImage 和 g2d.drawString() 扩展了 Paint() 内的 JPanel,并具有明确的 x 和 y 位置。

我现在的问题是我需要在它下面有几个交互式按钮..但是每当我尝试添加 JButton 时,它就会出现在顶部和中间。我已经使用了 setLocation(x, y) 和 setLayout(null) 以及我在其他回复中看到的所有内容,但它们似乎都不符合我的需要(或者至少我不太了解在哪里放它)

这就是我的代码的设置方式: pokerserver.java

public class pokerserver extends JFrame {

    public pokerserver() {
        add(new drawing());    
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(720, 640);
        setLocationRelativeTo(null);
        setTitle("Poker HANGOUTS");
        setResizable(false);
        setVisible(true);            
    }

    public static void main(String args[]) {
        new pokerserver();    
    }

然后在drawing.class中

public drawing() {      
    setFocusable(true);
    setBackground(new Color(39,91,46));
    setDoubleBuffered(true);        
    gameCards = new cards();
    gameCards.shuffle();

    for (int i = 0; i < 10; i++)
        seats[i] = -1;

    HQ = new HeadQuarters(this);
    HQ.start();

}

public void paint(Graphics g) {
    super.paint(g);       

    Graphics2D g2d = (Graphics2D)g;

    //All my UI code
    }

添加

   JButton button = new JButton("TEST");
    add(button);
   button.setLocation(10, 500);

我最后一次尝试是尝试在公共drawing()的末尾 。我不断看到布局管理方面的内容,但这对我没有帮助 - 主要是因为我不确定如何实现它

这是一个屏幕截图,可以帮助可视化我正在谈论的内容 - >

https://i.sstatic.net/O11C0.png

尝试获取下面的按钮。除非有办法将 ActionListener 添加到 drawImage() 中?

I have a poker game where I designed a nice pretty GUI that displays the cards and players. I did it all extending JPanel inside paint() with a lot of g2d.drawImage's and g2d.drawString()'s, with definite x and y locations.

My problem now is that I need to have a couple interactive buttons underneath it.. but whenever I try to add a JButton, it appears top and center. I've used setLocation(x, y) and setLayout(null) and everything I've seen in other replies, but none of them seem to match my need (Or at least I don't have a very well understanding of where to put it)

This is how my code is set up:
pokerserver.java

public class pokerserver extends JFrame {

    public pokerserver() {
        add(new drawing());    
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(720, 640);
        setLocationRelativeTo(null);
        setTitle("Poker HANGOUTS");
        setResizable(false);
        setVisible(true);            
    }

    public static void main(String args[]) {
        new pokerserver();    
    }

And then in drawing.class

public drawing() {      
    setFocusable(true);
    setBackground(new Color(39,91,46));
    setDoubleBuffered(true);        
    gameCards = new cards();
    gameCards.shuffle();

    for (int i = 0; i < 10; i++)
        seats[i] = -1;

    HQ = new HeadQuarters(this);
    HQ.start();

}

public void paint(Graphics g) {
    super.paint(g);       

    Graphics2D g2d = (Graphics2D)g;

    //All my UI code
    }

My last attempt was trying to add

   JButton button = new JButton("TEST");
    add(button);
   button.setLocation(10, 500);

at the end of public drawing(). I Keep seeing things on layout management, but it's not helping me -- mainly because I'm not sure how to implement it

Here's a screenshot to help visualize what I'm talking about->

https://i.sstatic.net/O11C0.png

Trying to get the button underneath. Unless there's a way to add an ActionListener to a drawImage()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2024-12-05 11:50:33

对于您的主面板,请使用 BorderLayout。

然后,您可以在“中心”添加带有所有自定义绘画的游戏面板。

然后创建一个面板并向其中添加按钮。现在您可以将此面板添加到主面板的北部。

换句话说,您不限于使用单个面板。

另外,自定义绘画应该在面板的paintComponent() 方法中完成,而不是在paint() 方法中完成。

For your main panel use a BorderLayout.

Then to the "CENTER" you can add your game panel with all your custom painting.

Then create a panel and add the buttons to it. Now you can add this panel to the NORTH of the main panel.

In other words you are not restricted to using a single panel.

Also, custom painting should be done in the paintComponent() method of your panel, NOT the paint() method.

苍白女子 2024-12-05 11:50:33

我不太确定你在追求什么,但这里有两种解释。

自定义绘画上的按钮 自定义绘画下方的按钮

我怀疑您想要第一个“自定义绘画上方的按钮”,但作为用户,我更喜欢第二个“自定义绘画下方的按钮”。

import java.awt.image.*;
import java.awt.*;
import javax.swing.*;

class PaintPanel extends JPanel {

    BufferedImage bg;

    PaintPanel(LayoutManager2 layout) {
        super(layout);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (bg==null) {
            bg = new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bg.createGraphics();
            GradientPaint gp = new GradientPaint(
                0,0,Color.RED,500,500,Color.BLUE);
            g2.setPaint(gp);
            g2.fillRect(0,0,500,500);
            g2.dispose();
        }

        g.drawImage(bg,0,0,getWidth(),getHeight(),this);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel buttons = new JPanel(
                    new FlowLayout(FlowLayout.CENTER));
                buttons.setOpaque(false);
                buttons.add(new JButton("Start"));
                buttons.add(new JButton("Stop"));

                PaintPanel pp = new PaintPanel(new BorderLayout());
                pp.setPreferredSize(new Dimension(200,100));

                pp.add(buttons, BorderLayout.SOUTH);

                JOptionPane.showMessageDialog(null,pp);

                JPanel gui = new JPanel(new BorderLayout());
                gui.setBackground(Color.ORANGE);
                gui.add(pp, BorderLayout.CENTER);
                gui.add(buttons, BorderLayout.SOUTH);

                JOptionPane.showMessageDialog(null,gui);
            }
        });
    }
}

I'm not really sure what you are after, but here are two interpretations.

Buttons over custom painting Buttons below custom painting

I suspect you want the 1st one 'Buttons over custom painting', but as a user I'd prefer the 2nd, with 'Buttons below custom painting'.

import java.awt.image.*;
import java.awt.*;
import javax.swing.*;

class PaintPanel extends JPanel {

    BufferedImage bg;

    PaintPanel(LayoutManager2 layout) {
        super(layout);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (bg==null) {
            bg = new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bg.createGraphics();
            GradientPaint gp = new GradientPaint(
                0,0,Color.RED,500,500,Color.BLUE);
            g2.setPaint(gp);
            g2.fillRect(0,0,500,500);
            g2.dispose();
        }

        g.drawImage(bg,0,0,getWidth(),getHeight(),this);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel buttons = new JPanel(
                    new FlowLayout(FlowLayout.CENTER));
                buttons.setOpaque(false);
                buttons.add(new JButton("Start"));
                buttons.add(new JButton("Stop"));

                PaintPanel pp = new PaintPanel(new BorderLayout());
                pp.setPreferredSize(new Dimension(200,100));

                pp.add(buttons, BorderLayout.SOUTH);

                JOptionPane.showMessageDialog(null,pp);

                JPanel gui = new JPanel(new BorderLayout());
                gui.setBackground(Color.ORANGE);
                gui.add(pp, BorderLayout.CENTER);
                gui.add(buttons, BorderLayout.SOUTH);

                JOptionPane.showMessageDialog(null,gui);
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文