truble
Goto Top

JButton icon wird nicht angezeigt

Hallo,
ich wollte einen JButton erstellen vor dem ein Icon / Bild angezeigt wird.
Allerdings sehe ich nur den Text und kein Icon / Bild.
Hier mal mein Code:
Importiert habe ich:

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

	private JButton getJButton1() {
		if(jButton1 == null) {
			ImageIcon icon = new ImageIcon("cup.gif");  
			jButton1 = new JButton("Huhu",icon);  
		}
		return jButton1;
	}

Content-Key: 119387

Url: https://administrator.de/contentid/119387

Printed on: April 19, 2024 at 20:04 o'clock

Member: TsukiSan
TsukiSan Jun 30, 2009 at 09:08:32 (UTC)
Goto Top
Hallo Truble

du müßtest (so meine ich) Icon und ImageIcon miteinander tauschen:

Icon ImageIcon = new ImageIcon("cup.gif");  

probier mal!

Gruß

Tsuki
Member: Truble
Truble Jun 30, 2009 at 09:14:40 (UTC)
Goto Top
Funktioniert leider nicht.
Dann habe ich einen falschen Typ.
Member: TsukiSan
TsukiSan Jun 30, 2009 at 09:37:18 (UTC)
Goto Top
Dein ".gif"-File befindet sich im gleichen Ordner und alle Module sind importiert?

import java.awt.BorderLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

Ich bin leider kein Experte in Java, Delphi oder so, aber ich meine es fehlen dann noch Module bei dir. Aber mit Icon und IconImage sollte diese Reihenfolge so richtig sein, wie ich dies beschrieben hatte.

Mh

Gruß
Tsuki
Member: BCCray
BCCray Jun 30, 2009 at 10:28:10 (UTC)
Goto Top
Kleines Beispiel:
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonSample {

  public static void main(String args) {
    String title = "JButton Sample";  
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(2, 2));

    JButton button1 = new JButton("Text Button");  
    button1.setMnemonic(KeyEvent.VK_B);
    content.add(button1);

    Icon warnIcon = new ImageIcon("Warn.gif");  
    JButton button2 = new JButton(warnIcon);
    content.add(button2);

    JButton button3 = new JButton("Warning", warnIcon);  
    content.add(button3);

    String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>" +  
      "<font color=\"#FF0080\"><u>Multi-line</u></font>";  
    JButton button4 = new JButton(htmlButton);
    content.add(button4);

    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

Die jeweiligen Image-Dateien müssen sich natürlich (wie im obigen Beispiel) im selben Package bzw. Projekt befinden.

Greetz
Member: Truble
Truble Jun 30, 2009 at 11:28:58 (UTC)
Goto Top
Hallo,
also ich das jetzt mal angepasst und es sieht so aus:

package xxx;
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.event.KeyEvent; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 

public class test{ 
 
	
	public test(){
    String title = "JButton Sample";   
    JFrame frame = new JFrame(title); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Container content = frame.getContentPane(); 
    content.setLayout(new GridLayout(2, 2)); 
    JButton button1 = new JButton("Text Button");   
    button1.setMnemonic(KeyEvent.VK_B); 
    content.add(button1); 
    Icon warnIcon = new ImageIcon("images/cup.gif");   
    JButton button2 = new JButton(warnIcon); 
    content.add(button2); 
    JButton button3 = new JButton("Warning", warnIcon);   
    content.add(button3); 
    String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>" +   
    "<font color=\"#FF0080\"><u>Multi-line</u></font>";   
    JButton button4 = new JButton(htmlButton); 
    content.add(button4); 
    frame.setSize(300, 200); 
    frame.setVisible(true); 
	}


}

Hier mal Bilder
Hier wie das Java app aussieht
Hier

Hier mal meine Verzeichnisse
Hier

Und hier das Bild was ich eig da vor haben möchte
Hier
Member: BCCray
BCCray Jun 30, 2009 at 11:37:42 (UTC)
Goto Top
JButton-Beispielcodes
schau hier mal face-smile

P.S.: Java-Klassen beginnen mit einem Großbuchstaben face-smile
Member: Truble
Truble Jun 30, 2009 at 11:44:04 (UTC)
Goto Top
ja aber er kann das Bild nicht finden oder wie sehe ich das?
Von der Syntax müsste doch alles richtig sein?