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
  • 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...

  • 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...

  • Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import...

    Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class translatorApp extends JFrame implements ActionListener {    public static final int width = 500;    public static final int height = 300;    public static final int no_of_lines = 10;    public static final int chars_per_line = 20;    private JTextArea lan1;    private JTextArea lan2;    public static void main(String[] args){        translatorApp gui = new translatorApp();...

  • 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....

  • Debug this java and post the corrected code. /* Creates a simple JPanel with a single...

    Debug this java and post the corrected code. /* Creates a simple JPanel with a single "quit" JButton * that ends the program when clicked. * There are 3 errors, one won't prevent compile, you have to read comments. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QuitIt extends JFrame {     public QuitIt() {         startIt(); //Create everything and start the listener     }     public final void startIt() {        //Create the JPanel        JPanel myPanel =...

  • I want to extend this GUI application in java language. Thanks for the help in advance. import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.Action...

    I want to extend this GUI application in java language. Thanks for the help in advance. import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class FormWindow {     private JFrame frame;     /**      * Launch the application.      */     public static void main(String[] args) {         EventQueue.invokeLater(new Runnable() {             public void run() {                 try {                     FormWindow window = new FormWindow();                     window.frame.setVisible(true);                 } catch (Exception e) {                     e.printStackTrace();                 }             }         });     }     /**      * Create the application.      */     public FormWindow() {         initialize();    ...

  • How can i make the java class seperate into an MVC pattern? public class ChatView implements Acti...

    How can i make the java class seperate into an MVC pattern? public class ChatView implements ActionListener {    public static void main(String[] args)    {        JFrame chatFrame = new JFrame();        JFrame chatFrame1 = new JFrame();        JFrame chatFrame2 = new JFrame();               JPanel chatPanel = new JPanel();        JPanel chatPanel1 = new JPanel();        JPanel chatPanel2 = new JPanel();        chatFrame.setContentPane(chatPanel);        chatFrame1.setContentPane(chatPanel1);        chatFrame2.setContentPane(chatPanel2);        chatFrame.setLayout(new FlowLayout());        chatFrame1.setLayout(new FlowLayout());        chatFrame2.setLayout(new FlowLayout());        JTextField chatWrite = new JTextField();        JTextField chatWrite1 = new JTextField();        JTextField chatWrite2 = new JTextField();        JTextArea chatDisplay...

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
ADVERTISEMENT