Place Text and Icon together in Java

Description

The following code shows how to place Text and Icon together.

Example


    /*from ww  w .  j a  va 2s.co  m*/
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon warnIcon = new ImageIcon("a.gif");
    JButton button3 = new JButton("Warning", warnIcon);
    frame.add(button3);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

The code above generates the following result.

Place Text and Icon together in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer