【问题标题】:how can i put a JButton on an image?如何在图像上放置 JButton?
【发布时间】:2011-08-20 06:04:24
【问题描述】:

我正在尝试修复一个 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

// label();

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

【问题讨论】:

  • 去掉 update() 方法。在 Swing 中无需重写此方法。这是 AWT 的旧代码,不应在 Swing 应用程序中使用。

标签: java swing size jbutton paintcomponent


【解决方案1】:
/*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();
    }
}

【讨论】:

    【解决方案2】:

    您需要更改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);
    }
    

    输出:

    【讨论】:

    • 错误:(可能是错字)调用的超级方法是paintComponent而不是paintComponents
    • 现在再次颠倒调用顺序 ;-)
    • @dacwe @kleopatra 如果您将其更改为paintComponent(我认为这是正确的),那么应该首先使用它来执行您想要绘制图像的所有默认绘画。自己测试一下。
    • @Boro,这就是我的意思 - 请参阅我对 Thomas 回答的评论 - 懒得在这里重复 :-)
    • @kleopatra 是的。我见过,但后来。我也加了它。抱歉重复了。 BTW +1 @dacwe 非常好的答案和很好的例子。
    【解决方案3】:

    你应该交换这两行:

    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。

    【讨论】:

    • 这是一个非常有帮助的答案,因为我不知道内容窗格的默认布局。谢谢。关于第一个tip,有什么区别?
    • 我的帖子没有区别,只是你的代码与 cmets 以及交换它们的提示。 :)
    • 从不接触 Swing JComponents 中的 awt 绘画方法 ;-) 所以它是一个 -1,除了它完全让我感到困惑 - 因为调用 super.paintComponent before custom绘制代码要实现的序列(否则,自定义绘制将被不透明组件的背景覆盖)
    • 是的,这将是正确的顺序,但他叫super.paintComponents而不是super.paintComponent。除此之外,您没有手动调用该方法而是调用paintComponent 是正确的(不认为这是一个潜在的错字:))。
    【解决方案4】:

    您是否尝试过在标签中使用带有 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 );
      }
    }
    

    那么您可以在标签顶部添加按钮吗?

    【讨论】:

    • 其实这是解决这个问题的另一种方法。它会帮助我解决我现在遇到的问题,但我认为它也会改变以后的许多想法。不过还是谢谢你的回答
    • -1 用于在不需要的地方使用 html ...改为创建并设置 ImageIcon。再加上使用 JLabel 根本不是一个好主意:要求是在添加的普通组件下有一个 background ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2022-01-17
    • 2016-01-21
    • 2013-02-02
    • 2013-05-03
    相关资源
    最近更新 更多