hallöchen,
ich hab bei diesem code soweit alles in ordnung nur möchte ich bei dem button kein neues fenster aufmachen sonder es soll da drinnen bleiben.
nähmen wir an ich drücke erstellen.
dann müsste das als swing dann dastehen im selben fenster:
mein Gui
danke fürs lesen und für eure hilfe^^
ich hab bei diesem code soweit alles in ordnung nur möchte ich bei dem button kein neues fenster aufmachen sonder es soll da drinnen bleiben.
nähmen wir an ich drücke erstellen.
dann müsste das als swing dann dastehen im selben fenster:
Java:
private static Animal createAnimal() {
boolean bStatus;
try {
Animal oSelected = null;
boolean bExit2 = false;
do {
// Ausgabe
System.out.println("\nBitte suchen Sie sich ein Tier aus :");
System.out.println("Katze \t<k>");
System.out.println("Hund \t<h>");
System.out.println("Maus \t<m>");
System.out.println("Neues Tier \t<n>");
System.out.println("Zurück \t<z>");
// Eingabe
Scanner oScanner = new Scanner(System.in);
char chInput = oScanner.next().charAt(0); // gibt char zurück
switch (chInput) {
case 'n':
System.out.println("Geben Sie den Tiernamen ein: ");
String Name = oScanner.next();
oSelected = new AnyAnimal(Name);
moZoo.add(oSelected);
break;
case 'k':
oSelected = new Cat();
moZoo.add(oSelected);
break;
case 'h':
oSelected = new Dog();
moZoo.add(oSelected);
break;
case 'm':
oSelected = new Mouse();
moZoo.add(oSelected);
break;
case 'z':
bExit2 = true;
break;
default:
System.err.println("Sie haben einen falschen Buchstaben eingegeben.");
}
} while (oSelected == null && !bExit2);
//Ausgabe Nickname
System.out.println("\nBitte geben Sie den Spitznamen des Tieres ein.");
Scanner oScanner = new Scanner(System.in);
oSelected.setNickname(oScanner.next());
System.out.println("\nBitte geben Sie das Datum in tt.mm.jjjj");
bStatus = true;
while (bStatus) {
//Ausgabe Datum
Birthday oDate = new Birthday();
if (!oDate.checkSyntax(oScanner.next())) {
System.err.println("Bitte geben Sie erneut das Datum ein!");
continue;
}
oSelected.setBd(oDate);
bStatus = false;
}
return oSelected;
} catch (Exception oExc) // erstelle Exception Objekt
{
System.err.println("Falsches Format!"); // gebe Ausnahme aus (Objekt)
return null;
}
}
mein Gui
Java:
package animalgui;
import java.awt.*;
import static java.awt.GridBagConstraints.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GridBagLayoutcheck extends JPanel {
private int gridx, gridy, gridwidth, gridheight, fill, anchor, ipadx, ipady;
private double weightx, weighty;
private Insets insets;
private Component component1;
private JButton component2, component3, component4, component5, component6;
public GridBagLayoutcheck() {
setLayout(new GridBagLayout());
example();
}
private void example() {
component1 = new JLabel("Willkommen ");
component1.setFont( new Font( "Comic Sans MS", Font.HANGING_BASELINE, 25 ));
component2 = new JButton(" Erstellen ");
component2.setFont( new Font( "Comic Sans MS", Font.HANGING_BASELINE, 15 ));
component2.addActionListener(new buttonAction());
component3 = new JButton(" Ansehen ");
component3.setFont( new Font( "Comic Sans MS", Font.HANGING_BASELINE, 15 ));
component3.addActionListener(new buttonAction());
component4 = new JButton(" Laden ");
component4.setFont( new Font( "Comic Sans MS", Font.HANGING_BASELINE, 15 ));
component4.addActionListener(new buttonAction());
component5 = new JButton(" Speichern ");
component5.setFont( new Font( "Comic Sans MS", Font.HANGING_BASELINE, 15 ));
component5.addActionListener(new buttonAction());
component6 = new JButton(" Öffnen ");
component6.setFont( new Font( "Comic Sans MS", Font.HANGING_BASELINE, 15 ));
component6.addActionListener(new buttonAction());
addGB(component1,
gridx = 1, gridy = 2,weightx = 1.0, weighty = 0.0,
anchor = PAGE_END, insets = new Insets(0, 150, 450, 150));
addGB(component2,
gridx = 1, gridy = 2,weightx = 1.0, weighty = 0.0,
anchor = PAGE_END, insets = new Insets(0, 150, 350, 150));
addGB(component3,
gridx = 1, gridy = 2,weightx = 1.0, weighty = 0.0,
anchor = PAGE_END, insets = new Insets(0, 150, 290, 150));
addGB(component4,
gridx = 1, gridy = 2,weightx = 1.0, weighty = 0.0,
anchor = PAGE_END, insets = new Insets(0, 150, 230, 150));
addGB(component5,
gridx = 1, gridy = 2,weightx = 1.0, weighty = 0.0,
anchor = PAGE_END, insets = new Insets(0, 150, 170, 150));
addGB(component6,
gridx = 1, gridy = 2,weightx = 1.0, weighty = 0.0,
anchor = PAGE_END, insets = new Insets(0, 150, 110, 150));
}
private void addGB(Component component1, int gridx, int gridy, double weightx, double weighty, int anchor, Insets insets) {
addGB(component1, gridx, gridy, 1, 1, NONE, weightx, weighty, anchor, insets, 0, 0);
}
private void addGB(Component component, int gridx, int gridy, int gridwidth, int gridheight,
int fill, double weightx, double weighty, int anchor, Insets insets,
int ipadx, int ipady) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = gridx;
constraints.gridy = gridy;
constraints.gridwidth = gridwidth;
constraints.gridheight = gridheight;
constraints.fill = fill;
constraints.weightx = weightx;
constraints.weighty = weighty;
constraints.anchor = anchor;
constraints.insets = insets;
constraints.ipadx = ipadx;
constraints.ipady = ipady;
add(component, constraints);
}
private class buttonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == component2){
JOptionPane.showMessageDialog(null, "Das ist der Bereich zum Erstellen","Erstellen", 1);
}
if (e.getSource() == component3){
JOptionPane.showMessageDialog(null, "Ansehen") ;
}
if (e.getSource() == component4){
JOptionPane.showMessageDialog(null, "Laden");
}
if (e.getSource() == component5){
JOptionPane.showMessageDialog(null, "Speichern");
}
if (e.getSource() == component6){
JOptionPane.showMessageDialog(null, "Öffnen");
}
}
}
}
danke fürs lesen und für eure hilfe^^