Question

This is the java GUI program. I want to add an icon to a button, but...

This is the java GUI program.

I want to add an icon to a button, but it shows nothing.

Can you fix it for me please?

import java.awt.BorderLayout;

import java.awt.Container;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

class Icon extends JFrame{

public static void main(String args[]){

Icon frame = new Icon("title");

frame.setVisible(true);

}

Icon(String title){

setTitle(title);

setBounds(100, 100, 300, 250);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel p = new JPanel();

ImageIcon icon1 = new ImageIcon("https://static.thenounproject.com/png/610387-200.png");

JButton button1 = new JButton(icon1);

p.add(button1);

Container contentPane = getContentPane();

contentPane.add(p, BorderLayout.CENTER);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

setBounds (100, 100, 300, 250) setDefaultcioseoperation (JFrame. EXIT ON CLOSE) JPanel p new JPanel ) ImageIcon iconi new Ima

import java.awt.BorderLayout;

import java.awt.Container;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

class Icon extends JFrame{

public static void main(String args[]) throws MalformedURLException{

Icon frame = new Icon("title");

frame.setVisible(true);

}

Icon(String title) throws MalformedURLException{

setTitle(title);

setBounds(100, 100, 300, 250);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel p = new JPanel();

ImageIcon icon1 = new ImageIcon(new URL("https://static.thenounproject.com/png/610387-200.png"));

JButton button1 = new JButton(icon1);

p.add(button1);

Container contentPane = getContentPane();

contentPane.add(p, BorderLayout.CENTER);

}

}

setBounds (100, 100, 300, 250) setDefaultcioseoperation (JFrame. EXIT ON CLOSE) JPanel p new JPanel ) ImageIcon iconi new ImageIcon (new URL ("https://static.thenounproject.com/png/610387-200.png")) UButton buttoni-new JButton(iconi) p.add (buttoni) Container contentPanegetContentPane ) title

////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

public class Menu_Panel {

public static void main(String[] args)

{

JFrame frame = new ImageViewerFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class ImageViewerFrame extends JFrame

{

public ImageViewerFrame()

{

setTitle("Make a new panel");

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// set up the menu bar

JMenuBar menuBar = new JMenuBar();

setJMenuBar(menuBar);

JMenu menu1 = new JMenu("Panel");

JMenuItem p = new JMenuItem("New Panel");

menu1.add(p);

menuBar.add(menu1);

p.addActionListener(new

ActionListener()

{
@Override

public void actionPerformed(ActionEvent event)

{
//make a new panel

JPanel panel = new JPanel();

JButton button = new JButton("hai");

panel.add(button);

add(panel);

setVisible(true);
}

});

}

private static final int DEFAULT_WIDTH = 1000;

private static final int DEFAULT_HEIGHT = 600;

}

Add a comment
Know the answer?
Add Answer to:
This is the java GUI program. I want to add an icon to a button, but...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you fix my error? I created a program that changes labels every second in Java....

    Can you fix my error? I created a program that changes labels every second in Java. But, it does not change. And, it will starts with second label. This is also an error. import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Map2 extends JFrame{ JPanel panel; JLabel pic; Timer tm; int x = 0; String ly = "<html> " + "<br> <font size='10' color='red'> <b> 111111111 <b/> </font>...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box...

    Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box that displays the temporary password should be read-only. The temporary password consists of the user’s first name, an asterisk (*), and the user’s birth year. If the user enters data in the first three fields, display a temporary password in the appropriate text field and a welcome message in the label below the text fields. If the user does not enter data, clear the...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • package timing; import java.awt.event.*; import javax.swing.*; public class Timing implements ActionListener{     JButton button1;     JLabel...

    package timing; import java.awt.event.*; import javax.swing.*; public class Timing implements ActionListener{     JButton button1;     JLabel label1;     public int minutes=3;//initial time     public int time=minutes*60;//total amount of time in seconds only     public int seconds=time%60;     public String defaulttimeset="0"+Integer.toString(minutes)+":0"+Integer.toString(seconds);     public static void main(String[] args) {         Timing move=new Timing();         move.go();     }     public void go(){         JFrame frame=new JFrame();         frame.setBounds(50,50,400,400);         JPanel panel2=new JPanel();         button1=new JButton("Start Time");         panel2.add(button1);                 button1.setBounds(30,50,150,170);         frame.getContentPane().add(button1);...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • Why when executed my frame does not contain what i have added to it? Here is...

    Why when executed my frame does not contain what i have added to it? Here is my code import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JPanel; @SuppressWarnings("serial") public class AddressBook1 extends JFrame implements ActionListener {    /**    *    */       JLabel name = new JLabel("Name: ");    JLabel address = new JLabel("Address: ");    JLabel phone = new JLabel("Phone: ");    JLabel email = new JLabel("Email: ");...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT