2012-07-11 55 views
1

朋友,我尝试使用seticon方法将图像添加到我的Jbutton,但它隐藏按钮上的文本标签。代码如下:将图像添加到Jbutton与前景标签

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

我在Eclipse中使用swing没有init()/ paint

+0

1)永远不要在没有报告异常的情况下捕获异常:无论是日志,控制台,还是其他报告意味着您想要的。 2)发布[SSCCE](http://sscce.org) – 2012-07-11 07:55:49

+0

好吧我会记住的,谢谢 – 2012-07-11 09:12:30

回答

7

只需使用

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

这会简单地将图片下面的文字。和输出将是这样的:

这里是有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(); 
      } 
     }); 
    } 
} 

最新编辑:有关添加背景图片THROUGH的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(); 
      } 
     }); 
    } 
} 

这里是相同的输出:

JLabelAsBackground

+0

你能告诉我如何更改JPanel的背景图片而不使用子类吗? – 2012-07-11 09:11:43

+0

@MachMitch使用JLabel作为 – 2012-07-11 09:15:23

+0

正如@GuillaumePolet所建议的那样,您可以使用'JLabel'来设置它,并将其设置为'Layout'并添加组件。给我一些时间的例子。 – 2012-07-11 09:50:29