JButton 绝对位于我的 Paint() 板下方
我有一个扑克游戏,我设计了一个漂亮的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您的主面板,请使用 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.
我不太确定你在追求什么,但这里有两种解释。
我怀疑您想要第一个“自定义绘画上方的按钮”,但作为用户,我更喜欢第二个“自定义绘画下方的按钮”。
I'm not really sure what you are after, but here are two interpretations.
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'.