Hallo, ich habe einige Zeit hier im Forum nach einer lösung gesucht, aber leider nichts gefunden.
Ich habe ein JFrame mit Container. Nun möchte ich, nachdem ich einen Button gedrückt habe, dass ein Bild erscheint. Ich hab jetzt so lange daran rumgedoktert und nichts gefunden, dass ich nun mal die Profis nach Rat fragen muss.
Das mit dem Container c in meiner action.class scheint nicht korrekt zu sein.
Ich bedanke mich schonmal für eure Mühe und würde mich über eine Antwort freuen.
Mfg Velow
Ich habe ein JFrame mit Container. Nun möchte ich, nachdem ich einen Button gedrückt habe, dass ein Bild erscheint. Ich hab jetzt so lange daran rumgedoktert und nichts gefunden, dass ich nun mal die Profis nach Rat fragen muss.
Das mit dem Container c in meiner action.class scheint nicht korrekt zu sein.
Ich bedanke mich schonmal für eure Mühe und würde mich über eine Antwort freuen.
Mfg Velow
Code:
package GUI;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.awt.Toolkit;
import javax.swing.JFrame;
import org.apache.commons.httpclient.HttpException;
public class GUI {
public GUI() {
JFrame f = new JFrame("XX");
Container c = f.getContentPane();
c.setLayout(null);
JButton random = new JButton();
random.setText("Random Funktion");
random.setBounds(250, 180, 140, 25);
c.add(random);
JButton eingabe = new JButton();
eingabe.setText("Eingabe ausführen");
eingabe.setBounds(250, 120, 140, 25);
c.add(eingabe);
JTextField a = new JTextField("xxxxxx");
a.setBounds(60, 80, 240, 20);
c.add(a);
JTextField b = new JTextField("Madonna");
b.setBounds(400, 80, 170, 20);
c.add(b);
JTextField d = new JTextField("Informations-Zeile");
d.setHorizontalAlignment(JLabel.CENTER);
d.setBounds(130, 220, 370, 20);
d.setEditable(false);
c.add(d);
action action = new action(random, eingabe, a, b, d, c);
random.addActionListener(action);
eingabe.addActionListener(action);
f.setSize(660, 530);
f.setResizable(false);
f.setVisible(true);
}
class action implements ActionListener {
JButton random, eingabe;
JTextField a, b, d;
Container c;
action(JButton random, JButton eingabe,
JTextField a, JTextField b, JTextField d
,Container c) {
this.random = random; this.eingabe = eingabe;
this.a = a; this.b = b; this.d = d;
this.c = c;
}
public void actionPerformed(ActionEvent e) {
Object button = e.getSource();
if (button == this.random) {
Icon _icon = new ImageIcon("xx.jpg");
JLabel _label = new JLabel(_icon);
_label.setBounds(20, 330, 130, 130);
this.c.add(_label);
} else if (button == this.eingabe) {
} else {
}
}
}
}
Zuletzt bearbeitet: