How do I add an image to a JButton in Java Swing?
-
This is the line of code that I'm not sure is valid or not: JButton button1 = new JButton(new ImageIcon("Pictures/smiley.png")); I am using GridBagLayout so I'm not sure whether it has anything to do with that. What happens is it just causes the button to become thinner in height, and the image isn't displayed, some suggestion to what I'm doing wrong? import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class window{ public static void main(String[] args){ JFrame frame = new JFrame("Cartoon Maker"); frame.setVisible(true); frame.setSize(800,500); JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL); JButton exit = new JButton("Exit"); toolbar.add(exit); JButton load = new JButton("Load"); toolbar.add(load); JButton save = new JButton("Save"); toolbar.add(save); JButton about = new JButton("About"); toolbar.add(about); frame.getContentPane().add(toolbar, BorderLayout.SOUTH); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.insets = new Insets(100,100,100,100); frame.add(panel); JButton button1 = new JButton(new ImageIcon("Pictures/smiley.png")); c.gridx = 0; c.gridy = 0; panel.add(button1, c); JButton button2 = new JButton("2"); c.gridx = 2; c.gridy = 0; panel.add(button2, c); JButton button3 = new JButton("3"); c.gridx = 4; c.gridy = 0; panel.add(button3, c); JButton button4 = new JButton("4"); c.gridx = 6; c.gridy = 0; panel.add(button4, c); JButton button5 = new JButton("5"); c.gridx = 0; c.gridy = 5; panel.add(button5, c); JButton button6 = new JButton("6"); c.gridx = 2; c.gridy = 5; panel.add(button6, c); JButton button7 = new JButton("7"); c.gridx = 4; c.gridy = 5; panel.add(button7, c); JButton button8 = new JButton("8"); c.gridx = 6; c.gridy = 5; panel.add(button8, c); class exitaction implements ActionListener{ public void actionPerformed(ActionEvent e){ System.exit(0); } } class aboutaction implements ActionListener{ public void actionPerformed(ActionEvent e){ JFrame frame2 = new JFrame("About Cartoon Maker"); frame2.setVisible(true); frame2.setSize(800,500); JTextArea jt = new JTextArea ("About"); frame2.add(jt); } } exit.addActionListener(new exitaction()); about.addActionListener(new aboutaction()); } } I've just started programming the programme so it isn't much at all yet.
-
Answer:
If you have compiled a .class, then that will be where the JVM working directory will be pointed to. Suppose your project folder is called /game /game window.java window.class /Pictures smilley.gif when you invoke window.class, the ImageIcon wants ("Picture/smilley.gif") there is a folder called Pictures The reason your Button is squished, is perhaps, the Image is null and gridLayout does not resize the button to the smilley.gif dimensions. Suppose the Pictures folder was on desktop, well then, you would need to build the full path to that file and insert that into the code.
Aled at Yahoo! Answers Visit the source
Related Q & A:
- How do I add a background image to my JFrame?Best solution by Stack Overflow
- How do I burn an image onto a DVD?Best solution by Ask Ubuntu
- How do I add Citation Templates for a Wiki?Best solution by Yahoo! Answers
- How do I add a custom background image to my Tumblr?Best solution by Yahoo! Answers
- How can I add an attachment to a message?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.