Bild-Button

DurzT

Grünschnabel
Hi

Ich versuch gerade ein Bild als anklickbaren Button zu erstellen und dieser soll nur sichtbar werden wenn ich mit der Maus drüber fahre. Bin mir aber nicht sicher ob das zu machen geht?

Wenn da jemand ne Lösung hätte wäre ich da sehr dankbar!!
(Brauch das für ein Projekt in meiner Ausbildung)

mfg
 
Hallo,

schau mal hier:

Java:
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;

public class ScrollExample extends JFrame implements MouseListener {

	private BufferedImage buf = new BufferedImage(32, 32,
			BufferedImage.TYPE_INT_ARGB);

	private ImageIcon img1 = new ImageIcon("b1.jpg");

	private ImageIcon img2 = new ImageIcon(buf);

	private JButton b1 = new JButton(img2);

	private JButton b2 = new JButton(img2);

	private JButton b3 = new JButton(img2);

	private JButton b4 = new JButton(img2);

	private JTable t = new JTable(new QuadratTableModelSimple());

	private JScrollPane sp = new JScrollPane(t);

	public ScrollExample() {
		super("ScrollExample");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setAlwaysOnTop(true);
		this.setLocationByPlatform(true);

		t.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		b1.addMouseListener(this);
		b2.addMouseListener(this);
		b3.addMouseListener(this);
		b4.addMouseListener(this);
		b1.setName("North");
		b2.setName("East");
		b3.setName("South");
		b4.setName("West");
		this.modificateButton(b1);
		this.modificateButton(b2);
		this.modificateButton(b3);
		this.modificateButton(b4);

		this.add(b1, BorderLayout.NORTH);
		this.add(b2, BorderLayout.EAST);
		this.add(b3, BorderLayout.SOUTH);
		this.add(b4, BorderLayout.WEST);
		this.add(sp, BorderLayout.CENTER);

		this.pack();
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new ScrollExample();
	}

	public void showButton(JButton b) {
		b.setIcon(img1);
		b.setBorderPainted(true);
	}

	public void hideButton(JButton b) {
		b.setBorderPainted(false);
		b.setIcon(img2);
	}

	public void modificateButton(JButton b) {
		b.setContentAreaFilled(false);
		b.setFocusPainted(false);
		b.setBorderPainted(false);
		b.setMargin(new Insets(0, 0, 0, 0));
		b.setIcon(img2);
	}

	public void mouseClicked(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {
		Rectangle r = t.getVisibleRect();

		switch (((JButton) e.getComponent()).getName().charAt(0)) {
		case 'N':
			r.translate(0, -5);
			break;
		case 'E':
			r.translate(5, 0);
			break;
		case 'S':
			r.translate(0, 5);
			break;
		case 'W':
			r.translate(-5, 0);
			break;
		}

		t.scrollRectToVisible(r);
	}

	public void mouseReleased(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {
		this.showButton((JButton) e.getComponent());

	}

	public void mouseExited(MouseEvent e) {
		this.hideButton((JButton) e.getComponent());
	}

	class QuadratTableModelSimple extends AbstractTableModel {
		public int getRowCount() {
			return 30;
		}

		public int getColumnCount() {
			return 20;
		}

		public Object getValueAt(int row, int col) {
			if (col == 0)
				return "" + row;
			else if (col == 1)
				return "" + (row * row);
			else
				return "" + (row * row * row);
		}

		public String getColumnName(int column) {
			return "Spalte" + column;
		}
	}
}


Vg Erdal
 
Zurück