Java 将按钮放置在图像顶部
我又回来了。我想知道如何在GUI中的图像顶部放置一个按钮。这是我目前的代码:Java 将按钮放置在图像顶部,java,image,swing,jbutton,jlabel,Java,Image,Swing,Jbutton,Jlabel,我又回来了。我想知道如何在GUI中的图像顶部放置一个按钮。这是我目前的代码: private static JPanel titlePanel = new JPanel(); private static JLabel titleScreen = new JLabel(); private static JLabel titleScreenBackground = new JLabel(); private static JButton startGameButt
private static JPanel titlePanel = new JPanel();
private static JLabel titleScreen = new JLabel();
private static JLabel titleScreenBackground = new JLabel();
private static JButton startGameButton = new JButton("START GAME");
private static ImageIcon titleScreenPic = new ImageIcon("http://icdn6.digitaltrends.com/image/battleship-650x0.jpg");
private static JFrame frame=new JFrame(); //creates frame
public static void main(String[] args) throws MalformedURLException{
titleScreen();
}
public static void titleScreen() throws IOException{
titleScreen.setLayout(new GridBagLayout());
titlePanel.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.anchor = GridBagConstraints.PAGE_END;
titleScreenBackground.setIcon(titleScreenPic);
titlePanel.add(startGameButton);
titlePanel.setAlignmentY(SwingConstants.BOTTOM);
frame.add(titleScreenBackground);
frame.add(titlePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(630, 300); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
我试图使面板成为gridbaglayout,这样我就可以将组件放在同一个单元格中,但它仍然先放置图像,然后将按钮直接放在它旁边
编辑:我已经重做了代码,使它做一些我想要的。如您所见,我尝试设置按钮位置的行对按钮没有任何作用
我将如何在GUI中的图像顶部放置一个按钮
如果您想在图像顶部放置一个Swing按钮,则需要遵循2个步骤
label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());
我将如何在GUI中的图像顶部放置一个按钮
如果您想在图像顶部放置一个Swing按钮,则需要遵循2个步骤
label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());
我将如何在GUI中的图像顶部放置一个按钮
如果您想在图像顶部放置一个Swing按钮,则需要遵循2个步骤
label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());
我将如何在GUI中的图像顶部放置一个按钮
如果您想在图像顶部放置一个Swing按钮,则需要遵循2个步骤
label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());
如果你想在图像上点击按钮,你可以使用JPanel的画图方法。 示例(使用资源im)
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestFrame extends JFrame {
BackgroundPane bgPane;
private JButton startButton;
public TestFrame() {
super();
initComponents();
}
private void initComponents() {
try {
URL url = getClass().getResource("battleship-650x0.jpg");
BufferedImage image = ImageIO.read(url);
bgPane = new BackgroundPane(image);
bgPane.setLayout(new GridBagLayout());
startButton = new JButton("Start");
bgPane.add(startButton);
setContentPane(bgPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}
class BackgroundPane extends JPanel {
Image image;
public BackgroundPane(Image backGroundImage) {
super();
image = backGroundImage;
setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
}
如果你想在图像上点击按钮,你可以使用JPanel的画图方法。 示例(使用资源im)
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestFrame extends JFrame {
BackgroundPane bgPane;
private JButton startButton;
public TestFrame() {
super();
initComponents();
}
private void initComponents() {
try {
URL url = getClass().getResource("battleship-650x0.jpg");
BufferedImage image = ImageIO.read(url);
bgPane = new BackgroundPane(image);
bgPane.setLayout(new GridBagLayout());
startButton = new JButton("Start");
bgPane.add(startButton);
setContentPane(bgPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}
class BackgroundPane extends JPanel {
Image image;
public BackgroundPane(Image backGroundImage) {
super();
image = backGroundImage;
setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
}
如果你想在图像上点击按钮,你可以使用JPanel的画图方法。 示例(使用资源im)
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestFrame extends JFrame {
BackgroundPane bgPane;
private JButton startButton;
public TestFrame() {
super();
initComponents();
}
private void initComponents() {
try {
URL url = getClass().getResource("battleship-650x0.jpg");
BufferedImage image = ImageIO.read(url);
bgPane = new BackgroundPane(image);
bgPane.setLayout(new GridBagLayout());
startButton = new JButton("Start");
bgPane.add(startButton);
setContentPane(bgPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}
class BackgroundPane extends JPanel {
Image image;
public BackgroundPane(Image backGroundImage) {
super();
image = backGroundImage;
setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
}
如果你想在图像上点击按钮,你可以使用JPanel的画图方法。 示例(使用资源im)
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestFrame extends JFrame {
BackgroundPane bgPane;
private JButton startButton;
public TestFrame() {
super();
initComponents();
}
private void initComponents() {
try {
URL url = getClass().getResource("battleship-650x0.jpg");
BufferedImage image = ImageIO.read(url);
bgPane = new BackgroundPane(image);
bgPane.setLayout(new GridBagLayout());
startButton = new JButton("Start");
bgPane.add(startButton);
setContentPane(bgPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}
class BackgroundPane extends JPanel {
Image image;
public BackgroundPane(Image backGroundImage) {
super();
image = backGroundImage;
setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
}
可能。好的,我已经把按钮放在图像上了,但是现在我不能对齐它。我在按钮上使用.SealOntMeTy,但是它仍然停留在中间的顶部。@ USE371402,使用合适的布局管理器,BorderLayout可能是最简单的,并更新您的代码,这样我们就可以看到变化。另一种选择是使用鼠标。听众。不,你不应该在JButton上使用Mouististor。你可以在按钮上添加ActualListor。可能。好,我已经把按钮放在了图像上,但是现在我不能对齐它。我在按钮上使用.SealOntMeTy,但是它仍然停留在中间的顶部。@ USE371402,使用适当的布局管理器,BordDay布局是Pro。请尽可能简单地更新您的代码,以便我们可以看到更改。另一个选项是使用鼠标侦听器。不,您不应该在JButton上使用鼠标侦听器。您可以向按钮添加ActionListener。可能。好的,我已设法将按钮放置在图像上,但现在无法对齐它。我正在按钮上使用.setAlignmentY,但它无法对齐使用适当的布局管理器,BorderLayout可能是最简单的,并更新你的代码,这样我们就可以看到变化。另一种选择是使用一个鼠标侦听器。不,你不应该在JButton上使用一个Mouististor。你把一个ActualListor添加到按钮。可能。好的,我已经成功地放置了。E按钮在图像上,但现在我不能对齐它。我在按钮上使用.SealOntMeTy,但是它仍然停留在中间的顶部。@ USE371402,使用适当的布局管理器,BorderLayout可能是最简单的,并更新您的代码,这样我们就可以看到变化。另一种选择是使用鼠标侦听器。不,您不应该使用鼠标。JButton上的侦听器。您向按钮添加了ActionListener。您的原始代码向JLabel添加了一个图标。为什么要更改该代码?为什么要在面板上进行自定义绘制?这是我的评论吗?@user3741402,是的,因为您似乎忽略了我对您发布的原始代码的原始建议。您这样做是为了complicated。您只需将按钮添加到标签(其中包含一个图标)然后将标签添加到框架中。关键是标签需要布局管理器才能显示组件,就像JPanel需要布局管理器才能显示组件一样。因此,您建议在标签上添加图像,然后将按钮和图像标签放在面板中?是的,这是我的两个步骤建议的。我在e正在使用此建议再次更新我以前的评论。您的原始代码在JLabel中添加了一个图标。您为什么要更改此代码?为什么要在面板上进行自定义绘制?此评论是为我写的吗?@user3741402,是的,因为您似乎忽略了我对您发布的原始代码的原始建议。您正在以这种方式进行comp复杂。您只需将按钮添加到标签(其中包含一个图标)然后将标签添加到框架中。关键是标签需要布局管理器才能显示组件,就像JPanel需要布局管理器才能显示组件一样。因此,您建议在标签上添加图像,然后将按钮和图像标签放在面板中?是的,这是我的两个步骤建议的。我在e正在更新我以前的公司