在特定位置将 JButton 置于 JLabel 之上?
Put JButton over JLabel in a specific spot?
我对 JFrame 还很陌生,我正在尝试制作游戏,但卡在游戏菜单上。我有一张背景图片,想在图片的特定位置上方放置一个按钮。我尝试将 jlabel 和 jbutton 放在不同的 jpanels 中,如果有任何事情使它变得更糟,它也无济于事。
这是我当前的代码。
import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.*;
import javax.swing.WindowConstants;
public class JavaApplication1 {
public static void main(String[] args) throws IOException {
JFrame myFrame = new JFrame("Memory Game");
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ImageIcon ii = new
ImageIcon("C:\Users\Nancy\Desktop\Game\start_pg.png");
JLabel img = new JLabel(ii);
JPanel image = new JPanel();
ImageIcon btn = new
ImageIcon("C:\Users\Nancy\Desktop\Game\start_btn.png");
JButton srt_btn = new JButton(btn);
srt_btn.setSize(140, 75);
srt_btn.setOpaque(false);
srt_btn.setContentAreaFilled(false);
srt_btn.setBorderPainted(false);
srt_btn.setBorder(null);
img.add(BorderLayout.SOUTH, srt_btn);
image.add(BorderLayout.NORTH, img);
myFrame.add(image);
myFrame.pack();
myFrame.setVisible(true);
}
}
我找到了解决方案,它正在使用:button.setLocation(x,y)
谢谢 :)
我对 JFrame 还很陌生,我正在尝试制作游戏,但卡在游戏菜单上。我有一张背景图片,想在图片的特定位置上方放置一个按钮。我尝试将 jlabel 和 jbutton 放在不同的 jpanels 中,如果有任何事情使它变得更糟,它也无济于事。
这是我当前的代码。
import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.*;
import javax.swing.WindowConstants;
public class JavaApplication1 {
public static void main(String[] args) throws IOException {
JFrame myFrame = new JFrame("Memory Game");
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ImageIcon ii = new
ImageIcon("C:\Users\Nancy\Desktop\Game\start_pg.png");
JLabel img = new JLabel(ii);
JPanel image = new JPanel();
ImageIcon btn = new
ImageIcon("C:\Users\Nancy\Desktop\Game\start_btn.png");
JButton srt_btn = new JButton(btn);
srt_btn.setSize(140, 75);
srt_btn.setOpaque(false);
srt_btn.setContentAreaFilled(false);
srt_btn.setBorderPainted(false);
srt_btn.setBorder(null);
img.add(BorderLayout.SOUTH, srt_btn);
image.add(BorderLayout.NORTH, img);
myFrame.add(image);
myFrame.pack();
myFrame.setVisible(true);
}
}
我找到了解决方案,它正在使用:button.setLocation(x,y)
谢谢 :)