how can i put a JButton on an image?

后端 未结 4 1640
终归单人心
终归单人心 2020-12-18 07:54

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 pu

相关标签:
4条回答
  • 2020-12-18 08:04

    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.

    0 讨论(0)
  • 2020-12-18 08:14
    /*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();
        }
    }
    
    0 讨论(0)
  • 2020-12-18 08:16

    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?

    0 讨论(0)
  • 2020-12-18 08:21

    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

    0 讨论(0)
提交回复
热议问题