使用前景标签将图像添加到 Jbutton

Adding image to Jbutton with foreground label(使用前景标签将图像添加到 Jbutton)
本文介绍了使用前景标签将图像添加到 Jbutton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我正在尝试使用 seticon 方法将图像添加到我的 Jbutton,但它隐藏了按钮上的文本标签.这是代码:

Friends, i m trying add image to my Jbutton using seticon method but it hide the text label on the button. Here is the code :

      try {
        Image img = ImageIO.read(getClass().getResource("image.jpg"));
        studentsButton.setIcon(new ImageIcon(img));         
      } catch (IOException ex) {
      }

我在没有init()/paint()/graphics的eclipse中使用swing,它在main方法中的简单框架.

And i m using swing in eclipse without init()/paint()/graphics, its simple frame in main method.

推荐答案

简单使用

studentsButton.setHorizontalTextPosition(AbstractButton.CENTER);
studentsButton.setVerticalTextPosition(AbstractButton.BOTTOM);

这只会将文本放置在图像下方.输出将是这样的:

This will simply place the Text below the Image. And the output will be like this :

这是一个代码示例,可以帮助您将 作为输出:

Here is one code example for your help having as output:

import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.imageio.ImageIO;

public class ButtonImageExample
{
    private JButton imageButton;
    private ImageIcon image;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Button Image Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        try
        {
            image = new ImageIcon(ImageIO.read(
                    new URL("http://i.imgur.com/6mbHZRU.png")));
        }
        catch(MalformedURLException mue)
        {
            mue.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }       

        imageButton = new JButton("Button Text");
        imageButton.setIcon(image);
        imageButton.setHorizontalTextPosition(AbstractButton.CENTER);
        imageButton.setVerticalTextPosition(AbstractButton.BOTTOM);

        contentPane.add(imageButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonImageExample().displayGUI();
            }
        });
    }
}

最新关于通过 JLABEL 添加背景图像

import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.imageio.ImageIO;

public class ButtonImageExample
{
    private ImageIcon image, imageForLabel;
    private JLabel imageLabel;

    private JTextField userField;
    private JPasswordField passField;
    private JButton loginButton;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Button Image Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));
        try
        {
            image = new ImageIcon(ImageIO.read(
                    new URL("http://i.imgur.com/jwyrvXC.gif")));
            imageForLabel = new ImageIcon(ImageIO.read(
                    new URL("http://i.imgur.com/09zgEvG.jpg")));           
        }
        catch(MalformedURLException mue)
        {
            mue.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }       

        imageLabel = new JLabel(imageForLabel);
        JPanel basePanel = new JPanel();
        // setOpaque(false) is used to make the JPanel translucent/transparent.
        basePanel.setOpaque(false);
        basePanel.setLayout(new BorderLayout(5, 5));
        JPanel topPanel = new JPanel();     
        topPanel.setOpaque(false);
        topPanel.setLayout(new GridLayout(2, 2, 5, 5));
        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        userLabel.setForeground(Color.WHITE);
        userField = new JTextField(10);
        JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
        passLabel.setForeground(Color.WHITE);
        passField = new JPasswordField(10);

        topPanel.add(userLabel);
        topPanel.add(userField);
        topPanel.add(passLabel);
        topPanel.add(passField);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setOpaque(false);
        loginButton = new JButton("Click to LOGIN");
        loginButton.setIcon(image);
        loginButton.setHorizontalTextPosition(AbstractButton.CENTER);
        loginButton.setVerticalTextPosition(AbstractButton.BOTTOM);
        bottomPanel.add(loginButton);

        basePanel.add(topPanel, BorderLayout.CENTER);
        basePanel.add(bottomPanel, BorderLayout.PAGE_END);

        imageLabel.setLayout(new GridBagLayout());
        imageLabel.add(basePanel);

        contentPane.add(imageLabel, BorderLayout.CENTER);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonImageExample().displayGUI();
            }
        });
    }
}

这是相同的输出:

​​

这篇关于使用前景标签将图像添加到 Jbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)