Button klick

Knödel

Grünschnabel
Hallo,

ich habe verschiedene Oberflächen mit Button erzeugt.
ich möchte Sie nun so verknüpfen, daß wenn ich auf einen Button klicke, die neue Oberfläche erscheint.
Die erste Oberfläche sollte dann aber verschwinden.

Ich häng mal den code für 2 Oberflächen hin: (bei klick auf den Button Student, sollte die 1.Berfläche verschwinden und die Student-Oberfläche erscheinen)
1. Oberfläche
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Seite1 extends JFrame {
//Main UI panel
JPanel contentPane;

//Card layout panels and layouts
JPanel jPanel2 = new JPanel();
GridBagLayout gridBagLayout1_panel3 = new GridBagLayout();
FlowLayout flowLayout1_panel4 = new FlowLayout();

//jPanel2 (brauch ich das noch?)
JButton ButtonFHAng = new JButton("Button_FHAng");
JButton ButtonStudent = new JButton("Button_Student");


JTextPane Textfeld = new JTextPane();



//Construct the frame
public Seite1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

//Component initialization
private void jbInit() throws Exception {

//Create main UI panel
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(400, 200));
this.setTitle("Seite 1");


//Add components

jPanel2.setBounds(new Rectangle(29, 56, 331, 98));
ButtonFHAng.setMinimumSize(new Dimension(120, 25));
ButtonFHAng.setText("FH-Angestellter");
ButtonFHAng.addActionListener(new Seite1_ButtonFHAng_actionAdapter(this));
ButtonStudent.setMaximumSize(new Dimension(120, 25));
ButtonStudent.setMinimumSize(new Dimension(120, 25));
ButtonStudent.setText("Student");
ButtonStudent.addActionListener(new Seite1_ButtonStudent_actionAdapter(this));
Textfeld.setBackground(SystemColor.control);
Textfeld.setFont(new java.awt.Font("Dialog", 0, 20));
Textfeld.setText("Bitte wähen Sie aus:");
Textfeld.setBounds(new Rectangle(98, 30, 214, 30));
contentPane.setBorder(BorderFactory.createLineBorder(Color.black));
contentPane.setMaximumSize(new Dimension(3276, 3276));
contentPane.setMinimumSize(new Dimension(10, 1));
contentPane.setOpaque(true);
contentPane.add(jPanel2, null);

//Create card layout container and panels
jPanel2.setBackground(SystemColor.control);
jPanel2.setLayout(null);

//Panel 1 in card layout (jPanel2)
jPanel2.add(ButtonStudent, null);
jPanel2.add(ButtonFHAng, null);
contentPane.add(Textfeld, null);
ButtonFHAng.setBounds(new Rectangle(192, 26, 126, 47));
ButtonStudent.setBounds(new Rectangle(32, 26, 119, 46));

}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void ButtonFHAng_actionPerformed(ActionEvent e) {

}

void ButtonStudent_actionPerformed(ActionEvent e) {

}
}

class Seite1_ButtonFHAng_actionAdapter implements java.awt.event.ActionListener {
Seite1 adaptee;

Seite1_ButtonFHAng_actionAdapter(Seite1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.ButtonFHAng_actionPerformed(e);
}
}

class Seite1_ButtonStudent_actionAdapter implements java.awt.event.ActionListener {
Seite1 adaptee;

Seite1_ButtonStudent_actionAdapter(Seite1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.ButtonStudent_actionPerformed(e);
}}

2. Obefläche
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Student1 extends JFrame {

//Main UI panel
JPanel contentPane;

//Radio buttons that control the current panel
ButtonGroup buttonGroup1 = new ButtonGroup();

//Card layout panels and layouts
JPanel jPanel2 = new JPanel();
GridBagLayout gridBagLayout1_panel3 = new GridBagLayout();
FlowLayout flowLayout1_panel4 = new FlowLayout();

//jPanel2 (brauch ich das noch?)
JButton button1_panel2 = new JButton("Button 1");
JButton button2_panel2 = new JButton("Button 2");

JTextPane Textfeld = new JTextPane();
JButton jButton1 = new JButton();

//Construct the frame
public Student1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

//Component initialization
private void jbInit() throws Exception {

//Create main UI panel
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(402, 261));
this.setTitle("Card Layout Sample");


//Add components

jPanel2.setBounds(new Rectangle(24, 56, 336, 171));
button1_panel2.setMinimumSize(new Dimension(120, 25));
button1_panel2.setText("Meine Belegung anschauen");
button2_panel2.setMaximumSize(new Dimension(120, 25));
button2_panel2.setMinimumSize(new Dimension(120, 25));
button2_panel2.setRolloverEnabled(false);
button2_panel2.setText("Seminarbelegung anschauen");
Textfeld.setBackground(SystemColor.control);
Textfeld.setFont(new java.awt.Font("Dialog", 0, 20));
Textfeld.setText("Hallo Student");
Textfeld.setBounds(new Rectangle(127, 19, 142, 30));
contentPane.setBorder(BorderFactory.createLineBorder(Color.black));
contentPane.setMaximumSize(new Dimension(3276, 3276));
contentPane.setMinimumSize(new Dimension(10, 1));
contentPane.setOpaque(true);
contentPane.setPreferredSize(new Dimension(400, 400));
jButton1.setBounds(new Rectangle(50, 122, 250, 43));
jButton1.setText("Seminar einschreiben");
contentPane.add(jPanel2, null);

//Create card layout container and panels
jPanel2.setBackground(SystemColor.control);
jPanel2.setLayout(null);

//Panel 1 in card layout (jPanel2)
jPanel2.add(button2_panel2, null);
jPanel2.add(button1_panel2, null);
jPanel2.add(jButton1, null);
contentPane.add(Textfeld, null);
button1_panel2.setBounds(new Rectangle(52, 61, 248, 44));
button2_panel2.setBounds(new Rectangle(54, 1, 244, 46));

}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}


Kann mir jemand helfen?
Danke
 

Thomas Darimont

Erfahrenes Mitglied
Servus!

Code:
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 * Created on 09.12.2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author Darimont
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SwingExample extends JFrame {

	private Object ref;
	private Object me;

	public SwingExample() {

		this.setTitle("SwingExample");
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				System.exit(0);
			}
		});

		JPanel panel = new JPanel();
		panel.setSize(320, 240);
		me = this;
		panel.setLayout(new FlowLayout());
		panel.add(new JLabel("Text: blablabla"));
		JButton btn = new JButton("push me");
		btn.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent event) {
				if (ref == null) {
					ref = new MyFrame(me);
					setVisible(false);
				} else {
					setVisible(false);
					((JFrame) ref).setVisible(true);
				}
			}
		});

		panel.add(btn);
		this.getContentPane().add(panel);

		this.setSize(320, 240);
		this.setVisible(true);

	}

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

	class MyFrame extends JFrame {

		private Object ref = null;
		private Object me = null;

		public MyFrame(Object refer) {
			this.ref = refer;
			me = this;
			this.setTitle("SwingExample2");
			this.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent event) {
					System.exit(0);
				}
			});

			JPanel panel = new JPanel();
			panel.setSize(320, 240);

			panel.setLayout(new FlowLayout());
			panel.add(new JLabel("Text: blablabla2"));
			JButton btn = new JButton("push me2");
			btn.addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent event) {
					//System.out.println("MousePressed");
					setVisible(false);
					((JFrame) ref).setVisible(true);

				}
			});

			panel.add(btn);
			this.getContentPane().add(panel);

			this.setSize(320, 240);
			this.setVisible(true);

		}

	}
}

Gruß Tom
 

Neue Beiträge