简体   繁体   English

我如何将JButton放在图像上?

[英]how can i put a JButton on an image?

I am trying to fix a JFrame where there will be a background image and on the image JButtons which will do some commands. 我正在尝试修复一个JFrame,其中将有一个背景图像和图像JButtons将执行一些命令。 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. 我尝试在没有布局的情况下这样做,因为我想在JFrame上的某些特定位置放置小按钮,但每次我这样做时,背景图像都会出现在前面,或者JFrame的大小等于JFrame大小。 With the following code, the JButton has the same size to JFrame. 使用以下代码,JButton与JFrame具有相同的大小。 I have tried to change the size and location of the JButton but nothing. 我试图改变JButton的大小和位置,但没有。 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; }

You need to change the content pane to get a background for your Frame . 您需要更改content pane以获取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: 输出:

截图

Have you tried using a JLabel with HTML in the label? 您是否尝试在标签中使用带有HTML的JLabel? 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? 然后在你的标签上你可以添加你的按钮?

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. 此外,请注意内容窗格的默认布局是BorderLayout。 Thus you'd set the layout of your content pane to null explicitly. 因此,您可以将内容窗格的布局显式设置为null。

/*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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM