如何在图像上放置 JButton?

发布于 2024-11-05 05:27:03 字数 1197 浏览 9 评论 0原文

我正在尝试修复一个 JFrame,其中将有一个背景图像,并且图像上的 JButtons 将执行一些命令。我尝试在没有布局的情况下执行此操作,因为我想将小按钮放在 JFrame 上的某些特定位置,但每次执行此操作时,背景图像都会出现在前面,或者 JFrame 的大小等于 JFrame 的大小。通过以下代码,JButton 具有与 JFrame 相同的大小。我尝试更改 JButton 的大小和位置,但没有任何效果。你能帮我吗?

这是代码


public final class Test extends JComponent
{
 private Image background;
 private JFrame frame;
 private Dimension dimension;

public Test()
{  
    dimension = new Dimension(15, 15);
    frame = new JFrame("Iphone");
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(this);
    frame.setBounds(641, 0, 344, 655);
    frame.setVisible(true);

    test = displayButton("tigka");
    frame.getContentPane().add(test);
}

public void update(Graphics g)
{
    paint(g);
}


public void paintComponent(Graphics g)
{
    super.paintComponents(g);
    g.drawImage(background, 0, 25, null); // draw background

// 标签();

test = displayButton("test"); } public JButton displayButton(String name) { JButton button = new JButton(name); button.setSize(100, 100); button.setPreferredSize(dimension); return button; }

I am trying to fix a JFrame where there will be a background image and on the image JButtons which will do some commands. I try to do it without layout because i want to put small buttons in some specific locations on the JFrame but every time i do it, the background image comes to the front or the JFrame has size equal to the JFrame size. With the following code, the JButton has the same size to JFrame. I have tried to change the size and location of the JButton but nothing. Can you help me please?

here is the code


public final class Test extends JComponent
{
 private Image background;
 private JFrame frame;
 private Dimension dimension;

public Test()
{  
    dimension = new Dimension(15, 15);
    frame = new JFrame("Iphone");
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(this);
    frame.setBounds(641, 0, 344, 655);
    frame.setVisible(true);

    test = displayButton("tigka");
    frame.getContentPane().add(test);
}

public void update(Graphics g)
{
    paint(g);
}


public void paintComponent(Graphics g)
{
    super.paintComponents(g);
    g.drawImage(background, 0, 25, null); // draw background

// label();

test = displayButton("test");
}

public JButton displayButton(String name)
{
JButton button = new JButton(name);

button.setSize(100, 100);
button.setPreferredSize(dimension);
return button;
}

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

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

发布评论

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

评论(4

蓝戈者 2024-11-12 05:27:03

您需要更改内容窗格才能获取框架的背景。

public static void main(String[] args) throws IOException {

    JFrame frame = new JFrame("Test");

    frame.setContentPane(new JPanel() {
        BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, 300, 300, this);
        }
    });

    frame.add(new JButton("Test Button"));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

输出:

screenshot

You need to change the content pane to get a background for your Frame.

public static void main(String[] args) throws IOException {

    JFrame frame = new JFrame("Test");

    frame.setContentPane(new JPanel() {
        BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, 300, 300, this);
        }
    });

    frame.add(new JButton("Test Button"));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

Output:

screenshot

魂ガ小子 2024-11-12 05:27:03

您是否尝试过在标签中使用带有 HTML 的 JLabel?像这样:

import javax.swing.*;

public class SwingImage1
{
  public static void main( String args[] )
  {
    JFrame  frm = new JFrame( "Swing Image 1" );
    JLabel  lbl = new JLabel( "<html><body><img src=\"http://liv.liviutudor.com/images/liv.gif\"></body></html>" );
    frm.getContentPane().add( lbl );
    frm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frm.pack();
    frm.setVisible( true );
  }
}

然后在标签顶部添加按钮?

Have you tried using a JLabel with HTML in the label? Something like this:

import javax.swing.*;

public class SwingImage1
{
  public static void main( String args[] )
  {
    JFrame  frm = new JFrame( "Swing Image 1" );
    JLabel  lbl = new JLabel( "<html><body><img src=\"http://liv.liviutudor.com/images/liv.gif\"></body></html>" );
    frm.getContentPane().add( lbl );
    frm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frm.pack();
    frm.setVisible( true );
  }
}

then on top of your label you can add your button?

风流物 2024-11-12 05:27:03

您应该交换这两行:

super.paintComponents(g);  //paints the children, like the button
g.drawImage(background, 0, 25, null); // draw background later possibly overwriting the button

因此应该是这样的顺序:

g.drawImage(background, 0, 25, null);
super.paintComponents(g); 

另外,请注意内容窗格的默认布局是 BorderLayout。因此,您可以将内容窗格的布局显式设置为 null。

You should swap those two lines:

super.paintComponents(g);  //paints the children, like the button
g.drawImage(background, 0, 25, null); // draw background later possibly overwriting the button

Thus it should be this order:

g.drawImage(background, 0, 25, null);
super.paintComponents(g); 

Additionally, note that the content pane's default layout is BorderLayout. Thus you'd set the layout of your content pane to null explicitly.

凯凯我们等你回来 2024-11-12 05:27:03
/*it is simple to put button on image first set image by making object then make button object & add the button object direct to image object rather then add to frame.*/

package frame;



import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class frame 
{

    public frame() 
    {
        JFrame obj = new JFrame("Banking Software");
        JButton b1 = new JButton("Opening Account");
        JLabel image = new JLabel(new ImageIcon("money.jpg"));
        image.setBounds(0,0, 1600, 1400);
        obj.setExtendedState(JFrame.MAXIMIZED_BOTH);
        obj.add(image);
        b1.setBounds(500,400, 100, 40);
        image.add(b1);
        obj.setVisible(true);
    }

    public static void main(String args[]) 
    {
        new frame();
    }
}
/*it is simple to put button on image first set image by making object then make button object & add the button object direct to image object rather then add to frame.*/

package frame;



import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class frame 
{

    public frame() 
    {
        JFrame obj = new JFrame("Banking Software");
        JButton b1 = new JButton("Opening Account");
        JLabel image = new JLabel(new ImageIcon("money.jpg"));
        image.setBounds(0,0, 1600, 1400);
        obj.setExtendedState(JFrame.MAXIMIZED_BOTH);
        obj.add(image);
        b1.setBounds(500,400, 100, 40);
        image.add(b1);
        obj.setVisible(true);
    }

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