Buttons und Textfelder in eine Grafik einfügen

dibz

Grünschnabel
Hallo zusammen.

Wir sitzen grad in der Schule an nem Projekt mit Java. Wir wollen ein paar Textfelder und einige Buttons in einer Grafik einfügen. Die Textfelder sollen beim drücken der Buttons raufzählen und dann die neuen Werte ausgeben.


Unser Problem ist jetzt jedoch, das wir es nich schaffen, diese auf der Grafik bzw in der Grafik anzuzeigen.
Entweder wird die Grafik von den Buttons und Textfeldern überlappt oder andersrum.

Hab schon einige Docs durch, darunter auch Java-Insel 4 und 5.

Wisst ihr vll noch ne Möglichkeit?


Hier mal unser Code:

Grafik-Ausgabe:
---------------------------------------------
import java.awt.*;
import java.awt.event.*;

/**
*
* Beschreibung.
*
* @version 1.0 vom 18.01.2007
* @dibz
*/

public class woj extends Frame {
// Anfang Variablen

// Ende Variablen

public woj(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); } });
int frameWidth = 800;
int frameHeight = 630;
setSize(frameWidth, frameHeight);


// Anfang Komponenten

// Ende Komponenten

setVisible(true);
}

// Anfang Ereignisprozeduren

public void paint(Graphics g)
{
Image img;
img = getToolkit().getImage("woj.jpg");
g.drawImage(img,0,30,this);
}


// Ende Ereignisprozeduren

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

------------------------------------------------------------

Buttons:
-----------------------------------------
import java.awt.*;
import java.awt.event.*;

/**
*
* Beschreibung.
*
* @version 1.0 vom 18.01.2007
* @dibz
*/

public class wojbutton extends Frame {
// Anfang Variablen
private Button button1 = new Button();
private Button button2 = new Button();
private Button button3 = new Button();
private Button button4 = new Button();
private TextField textField1 = new TextField();
private TextField textField2 = new TextField();
private TextField textField3 = new TextField();
private int a = 0;
private int b = 0;
private int c = 0;
// Ende Variablen

public wojbutton(String title)
{
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); } });
int frameWidth = 800;
int frameHeight = 600;
setSize(frameWidth, frameHeight);
Panel cp = new Panel(null);
add(cp);

// Anfang Komponenten

button1.setBounds(300, 80, 73, 57);
button1.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
button1.setLabel("+");
cp.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt); } });

button2.setBounds(632, 168, 73, 57);
button2.setLabel("+");
cp.add(button2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button2ActionPerformed(evt); } });

/*button3.setBounds(632, 40, 73, 57);
button3.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
button3.setLabel("+");
cp.add(button3);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt); } }); */

/*button4.setBounds(632, 10, 73, 57);
button4.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
button4.setLabel("+");
cp.add(button1);
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt); } }); */

textField1.setBounds(200, 80, 89, 53);
textField1.setEditable(false);
textField1.setBackground(Color.YELLOW);
textField1.setText(" "+ a);
cp.add(textField1);
textField2.setBounds(200, 168, 89, 53);
textField2.setEditable(false);
textField2.setBackground(Color.YELLOW);
textField2.setText(" " + b);
cp.add(textField2);
textField3.setBounds(200, 264, 89, 53);
textField3.setEditable(false);
textField3.setBackground(Color.YELLOW);
textField3.setText(" ");
cp.add(textField3);
// Ende Komponenten

setVisible(true);
}

// Anfang Ereignisprozeduren
public void button1ActionPerformed(ActionEvent evt)
{
if (evt.getSource()== button1)
{
a=a+1;
textField1.setText("" + a);
textField3.setText("" + (a+b));
}

}
public void button2ActionPerformed(ActionEvent evt)
{
if (evt.getSource()== button2)
{
b=b+1;
textField2.setText("" + b);
textField3.setText("" + (a+b));
}
}



// Ende Ereignisprozeduren

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

-------------------
 
Hallo dibz,

zunächst mal 2 formale Dinge.
1. Am besten benutzt du die code oder java Tags, wenn du Quellcode zeigen willst. Das ist etwas übersichtlicher.
2. Glaub ich bist du mit dem Problem im Swing /AWT / SWT Forum besser aufgehoben.

Nichts desto trotz :)
Ohne deinen Quelltext jetzt getestet zu haben, würde ich mir mal die Methode setOpaque anschauen. Wenn du dann folgendest machst, werden die Hintergrundpixel normalerweise nicht mehr gezeichnet.
Java:
textField1.setOpaque(false);
textField2.setOpaque(false);
textField3.setOpaque(false);

MfG
Daniel
 
Von mir auch dreiDinge vorweg:
1. Klassennamen werden immer groß geschrieben und ein wenig sprechendere Namen als "woj" wären schön. Genauso solltet ihr die Buttons nachdem benamen was sie tun.

2.
Java:
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); } });

System.exit sollte nach möglichkeit nicht explizit aufgerufen werden.
Daher bitte:
Java:
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { dispose(); } });

3. Wenn ihr eine Zahl in einen String umwandeln wollt, dies bitte nicht mit ""+zahl machen sondern mit Integer.toString(zahl). Das ist deutlich schneller und genau dafür gedacht.

Ansonsten sieht das schon ganz gut aus.

Zu dem Problem:
Warum sind die Buttons in einem eigenen Frame? Wenn du einfach all die Buttons nimmst und in den Frame kopierst wo das Bild drin geladen wird dann erscheinen die Buttons auch über dem Bild.
 
Danke schonmal für die Antworten, werden das gleich direkt mal ausprobieren.

Sollten noch Fragen oder Probleme auftauchen, wende ich mich nochmal an euch.

Vielen dank ;)
 
Das ist nochmal dein Original-Code:
Java:
import java.awt.*;
import java.awt.event.*;

/**
*
* Beschreibung.
*
* @version 1.0 vom 18.01.2007
* @dibz
*/

public class woj extends Frame {
// Anfang Variablen

// Ende Variablen

public woj(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); } });
int frameWidth = 800;
int frameHeight = 630;
setSize(frameWidth, frameHeight);


// Anfang Komponenten

// Ende Komponenten

setVisible(true);
}

// Anfang Ereignisprozeduren

public void paint(Graphics g)
{
Image img;
img = getToolkit().getImage("woj.jpg");
g.drawImage(img,0,30,this);
}


// Ende Ereignisprozeduren

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

Java:
import java.awt.*;
import java.awt.event.*;

/**
*
* Beschreibung.
*
* @version 1.0 vom 18.01.2007
* @dibz
*/

public class wojbutton extends Frame {
// Anfang Variablen
private Button button1 = new Button();
private Button button2 = new Button();
private Button button3 = new Button();
private Button button4 = new Button();
private TextField textField1 = new TextField();
private TextField textField2 = new TextField();
private TextField textField3 = new TextField();
private int a = 0;
private int b = 0;
private int c = 0;
// Ende Variablen

public wojbutton(String title)
{
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); } });
int frameWidth = 800;
int frameHeight = 600;
setSize(frameWidth, frameHeight);
Panel cp = new Panel(null);
add(cp);

// Anfang Komponenten

button1.setBounds(300, 80, 73, 57);
button1.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
button1.setLabel("+");
cp.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt); } });

button2.setBounds(632, 168, 73, 57);
button2.setLabel("+");
cp.add(button2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button2ActionPerformed(evt); } });

/*button3.setBounds(632, 40, 73, 57);
button3.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
button3.setLabel("+");
cp.add(button3);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt); } }); */

/*button4.setBounds(632, 10, 73, 57);
button4.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
button4.setLabel("+");
cp.add(button1);
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt); } }); */

textField1.setBounds(200, 80, 89, 53);
textField1.setEditable(false);
textField1.setBackground(Color.YELLOW);
textField1.setText(" "+ a);
cp.add(textField1);
textField2.setBounds(200, 168, 89, 53);
textField2.setEditable(false);
textField2.setBackground(Color.YELLOW);
textField2.setText(" " + b);
cp.add(textField2);
textField3.setBounds(200, 264, 89, 53);
textField3.setEditable(false);
textField3.setBackground(Color.YELLOW);
textField3.setText(" ");
cp.add(textField3);
// Ende Komponenten

setVisible(true);
}

// Anfang Ereignisprozeduren
public void button1ActionPerformed(ActionEvent evt)
{
if (evt.getSource()== button1)
{
a=a+1;
textField1.setText("" + a);
textField3.setText("" + (a+b));
}

}
public void button2ActionPerformed(ActionEvent evt)
{
if (evt.getSource()== button2)
{
b=b+1;
textField2.setText("" + b);
textField3.setText("" + (a+b));
}
}



// Ende Ereignisprozeduren

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

Damit die Buttons auf dem Bild angezeigt werden musst du einfach alles von Zeile 40-85 und zugehörige Buttons und ...actionPerformed aus wojbutton nach Zeile 28 in woj kopieren. Das cp.add entsprechend durch ein this.add ersetzen. So hats bei mir funktioniert.
 
Hi zeja. also anscheinend bin ich zu doof o_O

hiermal der code:
Java:
import java.awt.*;
import java.awt.event.*;

/**
  *
  * Beschreibung.
  *
  * @version 1.0 vom 12.01.2007
  * @author
  */

public class woj extends Frame {
  // Anfang Variablen

  // Ende Variablen

  public woj(String title) {
    // Frame-Initialisierung
    super(title);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) { System.exit(0); } });
    int frameWidth = 800;
    int frameHeight = 630;
    setSize(frameWidth, frameHeight);

    // Anfang Komponenten
    //Neuer Teil
    button1.setBounds(515, 198, 25, 20);
    button1.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
    button1.setLabel("+");
    this.add(button1);
    button1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        button1ActionPerformed(evt); } });

      //richtig platziert
        public void button1ActionPerformed(ActionEvent evt)
        {
          if (evt.getSource()== button1)
           {
            a=a+1;
            textField1.setText("" + a);
           }
        }

    button2.setBounds(515, 278, 25, 20);
    button2.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
    button2.setLabel("+");
    this.add(button2);
    button2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        button2ActionPerformed(evt); } });

         public void button2ActionPerformed(ActionEvent evt)
         {
           if (evt.getSource()== button2)
            {
              b=b+1;
              textField2.setText("" + b);
              //textField9.setText("" + ce1.getSelectedItem());
              int block = Integer.parseInt(ce1.getSelectedItem());
              int ergebnis = (b*2)-block;
              textField9.setText("" + ergebnis);
            }
         }

    button3.setBounds(515, 378, 25, 20);
    button3.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
    button3.setLabel("+");
    this.add(button3);
    button3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        button3ActionPerformed(evt); } });

    button4.setBounds(515, 458, 25, 20);
    button4.setFont (new Font("MS Sans Serif", Font.PLAIN, 12));
    button4.setLabel("+");
    this.add(button4);
    button4.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        button4ActionPerformed(evt); } });

    textField1.setBounds(475, 200, 30, 18);
    textField1.setEditable(false);
    textField1.setBackground(Color.DARK_GRAY);
    textField1.setForeground(Color.white);
    textField1.setText(" "+ a);
    this.add(textField1);

    textField2.setBounds(475, 280, 30, 18);
    textField2.setEditable(false);
    textField2.setBackground(Color.DARK_GRAY);
    textField2.setForeground(Color.white);
    textField2.setText(" " + b);
    this.add(textField2);

    textField3.setBounds(475, 380, 30, 18);
    textField3.setEditable(false);
    textField3.setBackground(Color.DARK_GRAY);
    textField3.setForeground(Color.white);
    textField3.setText(" " + c);
    this.add(textField3);

    textField4.setBounds(475, 460, 30, 18);
    textField4.setEditable(false);
    textField4.setBackground(Color.DARK_GRAY);
    textField4.setForeground(Color.white);
    textField4.setText(" " + d);
    this.add(textField4);

    textField5.setBounds(620, 200, 70, 18);
    textField5.setEditable(true);
    textField5.setBackground(Color.DARK_GRAY);
    textField5.setForeground(Color.white);
    textField5.setText(" ");
    this.add(textField5);

    ce1.setBounds(695, 200, 35, 18);
    ce1.add("1");
    ce1.add("2");
    ce1.add("3");
    ce1.add("4");
    ce1.add("5");
    ce1.add("6");
    ce1.add("7");
    ce1.add("8");
    ce1.add("9");
    ce1.add("10");
    ce1.add("11");
    ce1.add("12");
    ce1.add("13");
    ce1.add("14");
    ce1.add("15");
    ce1.add("16");
    ce1.add("17");
    ce1.add("18");
    ce1.add("19");
    ce1.add("20");
    ce1.setBackground(Color.DARK_GRAY);
    ce1.setForeground(Color.WHITE);
    this.add(ce1);

    textField7.setBounds(620, 245, 70, 18);
    textField7.setEditable(false);
    textField7.setBackground(Color.DARK_GRAY);
    textField7.setForeground(Color.white);
    textField7.setText("Krieger");
    this.add(textField7);

    ce2.setBounds(695, 245, 35, 18);
    ce2.add("M");
    ce2.add("W");
    ce2.setBackground(Color.darkGray);
    ce2.setForeground(Color.WHITE);
    this.add(ce2);

    textField9.setBounds(700, 340, 35, 18);
    textField9.setEditable(false);
    textField9.setBackground(Color.DARK_GRAY);
    textField9.setForeground(Color.white);
    textField9.setText("");
    this.add(textField9);

    textField10.setBounds(640, 380, 95, 18);
    textField10.setEditable(false);
    textField10.setBackground(Color.DARK_GRAY);
    textField10.setForeground(Color.white);
    textField10.setText("");
    this.add(textField10);

    textField11.setBounds(640, 425, 95, 18);
    textField11.setEditable(false);
    textField11.setBackground(Color.DARK_GRAY);
    textField11.setForeground(Color.white);
    textField11.setText("");
    this.add(textField11);

    textField12.setBounds(640, 460, 95, 18);
    textField12.setEditable(true);
    textField12.setBackground(Color.DARK_GRAY);
    textField12.setForeground(Color.WHITE);
    textField12.setText("");
    this.add(textField12);
    // Ende neuer Teil
    // Ende Komponenten

    setVisible(true);
  }
  // Anfang Ereignisprozeduren
  
 public void paint(Graphics g)
 {
   Image img;
   img = getToolkit().getImage("woj.jpg");
   g.drawImage(img,0,30,this);
 }
  // Ende Ereignisprozeduren

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

Das ist jetzt nur ein Teil davon, keine lust alles erst wieder einzufügen jetzt auf die schnelle.
Beim compilieren gibt er mir noch Fehler aus (
woj.java:37: illegal start of expression
public void button1ActionPerformed(ActionEvent evt)
^
)

Hast du dein "fertiges" prog vom testen noch? falls ja, kannst mir den code mal zukommen lassen? kannst dich ja mal bitte per PN melden, danke.


Oder hat sonst jemand eine Idee?


€dith: so ähnlich soll die ganze kist dann aussehen.

wojinfobildsf6.th.jpg



alle Felder mit Strich oder geschriebenem Inhalt sollen Textfelder werden. und rechts neben den 4 Attributen (also zwischen den 4 linken Textfeldern und rechts den ganz vielen) sollen die buttons hin.
 
Zuletzt bearbeitet:
Deine actionPerformed sind nun Methoden innerhalb einer Methode. Und das geht nunmal nicht :)

Java:
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ImageFrame extends Frame {

	// Anfang Variablen
	private static final int WIDTH = 800;

	private static final int HEIGHT = 630;

	private Label labelA;

	private Label labelB;

	private Label labelSum;

	private int a = 0;

	private int b = 0;

	private int c = 0;

	// Ende Variablen

	public ImageFrame(String title) {

		// Frame-Initialisierung
		super(title);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				dispose();
			}
		});

		setSize(WIDTH, HEIGHT);
		init();

		setVisible(true);
	}

	private void init() {
		this.setLayout(new GridBagLayout());

		// Anfang Komponenten
		Button button1 = new Button("+");
		button1.setBounds(300, 80, 73, 57);
		button1.setFont(new Font("MS Sans Serif", Font.PLAIN, 12));
		add(button1);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				a++;
				labelA.setText(Integer.toString(a));
				labelSum.setText(Integer.toString(a + b));
			}
		});

		Button button2 = new Button("+");
		button2.setBounds(632, 168, 73, 57);
		add(button2);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				b++;
				labelB.setText(Integer.toString(b));
				labelSum.setText(Integer.toString(a + b));
			}
		});

		labelA = new Label(Integer.toString(a));
		labelA.setBounds(200, 80, 89, 53);
		labelA.setBackground(Color.YELLOW);
		add(labelA);

		labelB = new Label(Integer.toString(b));
		labelB.setBounds(200, 168, 89, 53);
		labelB.setBackground(Color.YELLOW);
		add(labelB);

		labelSum = new Label(" ");
		labelSum.setBounds(200, 264, 89, 53);
		labelSum.setBackground(Color.YELLOW);
		add(labelSum);
		// Ende Komponenten
	}

	// Anfang Ereignisprozeduren
	public void paint(Graphics g) {
		Image img = getToolkit().getImage("diablo.jpg");
		g.drawImage(img, 0, 30, this);
	}

	// Ende Ereignisprozeduren

	public static void main(String[] args) {
		new ImageFrame("diablo");
	}
}
 
Zurück