Hallo, wie kann man den Konstruktor kürzen? Es muss doch möglich sein gleiche Alaufe über eine funktion auszulagern und aus dem Konstruktor aufzurufen.
Hier mal mein Code wäre über verbesserungsvorschläge dankbar.
Hier mal mein Code wäre über verbesserungsvorschläge dankbar.
Java:
Cronik2 (String titel) {
super(titel);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
// Die Ereignisbehandlung für die Schaltflächen
class CMeinActionLauscher implements ActionListener {
public void actionPerformed(ActionEvent e) {
String label;
label = e.getActionCommand();
if (label.equals("Cronik laden")) {dateiladen();}
if (label.equals("Cronik speichern")) {}
if (label.equals("Programm beenden")) {System.exit(0);}
if (label.equals("Neue Person")) {}
if (label.equals("Stammbaum anzeigen")) {}
if (label.equals("Aktuelle Person bearbeiten")) {
person_jobbearbeitenfenster NeueCronik_person =new person_jobbearbeitenfenster ("Person bearbeiten");
NeueCronik_person.pack();
NeueCronik_person.setSize(200,200);
NeueCronik_person.setResizable(false);
NeueCronik_person.setVisible(true);
}
if (label.equals("Entwickler")) {
entwicklerfenster NeueCronik_entwickler =new entwicklerfenster ("Entwickler");
NeueCronik_entwickler.pack();
NeueCronik_entwickler.setSize(400,80);
NeueCronik_entwickler.setResizable(false);
NeueCronik_entwickler.setVisible(true);
}
if (label.equals("Person suchen")) {suchewahl();}
if (label.equals("V")) {suche(true,false,"");}
if (label.equals("M")) {suche(false,true,"");}
if (label.equals("Neue Cronik")) {
neuecronikerstellenfenster NeueCronik =new neuecronikerstellenfenster ("FamilienCronik2");
NeueCronik.pack();
NeueCronik.setSize(200,200);
NeueCronik.setResizable(false);
NeueCronik.setVisible(true);
}
}
}
//Für ehe combo
class CMeinActionLauschercombo implements ItemListener {
public void itemStateChanged(ItemEvent e) {
status_auswahl_ehepartner=0;
}
}
class Actioncombo extends MouseAdapter{
JComboBox combo;
public Actioncombo(JComboBox l){combo = l;}
public void mouseClicked(MouseEvent e){
status_auswahl_ehepartner++;
System.out.println(status_auswahl_ehepartner);
if (status_auswahl_ehepartner>=2) {
status_auswahl_ehepartner=0;
int index = combo.getSelectedIndex();
if (!(combo.getSelectedItem()).equals("Neu")){
akt_person=Integer.parseInt (Ehepartner[akt_person][index][0])-1;
eintragen();
}
}
}
}
//Listen
class ActionJList extends MouseAdapter{
JList listen;
public ActionJList(JList l){listen = l;}
public void mouseClicked(MouseEvent e){
int index = listen.locationToIndex(e.getPoint());
ListModel dlm = listen.getModel();
Object item = dlm.getElementAt(index);
String name=item + "";
if (!(name.equals("null"))) suche(false,false,name);
System.out.println(":" +name+":");
}
}
//einfügend er Objekte ohne Schaltfläschen
Panel panel1 = new Panel();
panel1.setLayout(gbl);
gbc.fill =GridBagConstraints.BOTH;
// Die Abständer der einzelnen GUI-Elemente zu den gedachten Gitterlinien festgelegen:
gbc.insets = new Insets(2,2,2,2);
//Menü
JMenuBar menueleiste =new JMenuBar();
JMenu menu1 =new JMenu("Programm");
JMenu menu2 =new JMenu("Datenbank");
JMenu menu3 =new JMenu("Weiteres");
JMenuItem item1a =new JMenuItem("Neue Cronik");
JMenuItem item2a =new JMenuItem("Cronik laden");
JMenuItem item3a =new JMenuItem("Cronik speichern");
JMenuItem item4a =new JMenuItem("Programm beenden");
item1a.addActionListener(new CMeinActionLauscher());
item2a.addActionListener(new CMeinActionLauscher());
item3a.addActionListener(new CMeinActionLauscher());
item4a.addActionListener(new CMeinActionLauscher());
menu1.add(item1a);
menu1.add(item2a);
menu1.add(item3a);
menu1.add(item4a);
JMenuItem item1b =new JMenuItem("Neue Person");
JMenuItem item2b =new JMenuItem("Aktuelle Person bearbeiten");
JMenuItem item3b =new JMenuItem("Person suchen");
JMenuItem item4b =new JMenuItem("Stammbaum anzeigen");
item1b.addActionListener(new CMeinActionLauscher());
item2b.addActionListener(new CMeinActionLauscher());
item3b.addActionListener(new CMeinActionLauscher());
item4b.addActionListener(new CMeinActionLauscher());
menu2.add(item1b);
menu2.add(item2b);
menu2.add(item3b);
menu2.add(item4b);
JMenuItem item1c =new JMenuItem("Entwickler");
item1c.addActionListener(new CMeinActionLauscher());
menu3.add(item1c);
menueleiste.add(menu1);
menueleiste.add(menu2);
menueleiste.add(menu3);
setJMenuBar(menueleiste);
// Infos
JLabel text1 =new JLabel("Name: ",JLabel.RIGHT);
JLabel text2 =new JLabel("Vorname: ",JLabel.RIGHT);
JLabel text3 =new JLabel("Geburtsname: ",JLabel.RIGHT);
JLabel text4 =new JLabel("geboren am: ",JLabel.RIGHT);
JLabel text5 =new JLabel("geboren in: ",JLabel.RIGHT);
JLabel text6 =new JLabel("Geschlecht: ",JLabel.RIGHT);
JLabel text7 =new JLabel("getauft am: ",JLabel.RIGHT);
JLabel text8 =new JLabel("getauft in: ",JLabel.RIGHT);
JLabel text9 =new JLabel("Konfession: ",JLabel.RIGHT);
JLabel text10 =new JLabel("Beruf",JLabel.RIGHT);
JLabel text11 =new JLabel("Notiz: ",JLabel.RIGHT);
JLabel text11b =new JLabel(" ",JLabel.RIGHT);
JLabel text11c =new JLabel(" ",JLabel.RIGHT);
JLabel text11d =new JLabel(" ",JLabel.RIGHT);
JLabel text11f =new JLabel(" ",JLabel.RIGHT);
JLabel text12 =new JLabel("Ehepartner: ",JLabel.RIGHT);
JLabel text13 =new JLabel("Hochzeit am: ",JLabel.RIGHT);
JLabel text14 =new JLabel("Hochzeit in: ",JLabel.RIGHT);
JLabel text15 =new JLabel("gestorben am: ",JLabel.RIGHT);
JLabel text16 =new JLabel("gestorben in: ",JLabel.RIGHT);
JLabel text17 =new JLabel("beerdigt am: ",JLabel.RIGHT);
JLabel text18 =new JLabel("beerdigt in: ",JLabel.RIGHT);
JLabel text19 =new JLabel("w. Name: ",JLabel.RIGHT);
gbc.weighty=0;
gbc.gridx = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridy = 1;
gbl.setConstraints(text1 , gbc);
panel1.add(text1 );
gbc.gridy = 2;
gbl.setConstraints(text2 , gbc);
panel1.add(text2 );
gbc.gridy = 3;
gbl.setConstraints(text19 , gbc);
panel1.add(text19 );
gbc.gridy = 4;
gbl.setConstraints(text3 , gbc);
panel1.add(text3 );
gbc.gridy = 5;
gbl.setConstraints(text4 , gbc);
panel1.add(text4 );
gbc.gridy = 6;
gbl.setConstraints(text5 , gbc);
panel1.add(text5 );
gbc.gridy = 7;
gbl.setConstraints(text6 , gbc);
panel1.add(text6 );
gbc.gridy = 8;
gbl.setConstraints(text7 , gbc);
panel1.add(text7 );
gbc.gridy = 9;
gbl.setConstraints(text8 , gbc);
panel1.add(text8 );
gbc.gridy = 10;
gbl.setConstraints(text9 , gbc);
panel1.add(text9 );
gbc.gridy = 11;
gbl.setConstraints(text10 , gbc);
panel1.add(text10 );
gbc.gridy = 12;
gbl.setConstraints(text11 , gbc);
panel1.add(text11 );
gbc.gridy = 13;
gbl.setConstraints(text11b , gbc);
panel1.add(text11b );
gbc.gridy = 14;
gbl.setConstraints(text11c , gbc);
panel1.add(text11c );
gbc.gridy = 15;
gbl.setConstraints(text11d , gbc);
panel1.add(text11d );
gbc.gridy = 16;
gbl.setConstraints(text11f , gbc);
panel1.add(text11f );
gbc.gridy = 17;
gbl.setConstraints(text12 , gbc);
panel1.add(text12 );
gbc.gridy = 18;
gbl.setConstraints(text13 , gbc);
panel1.add(text13 );
gbc.gridy = 19;
gbl.setConstraints(text14 , gbc);
panel1.add(text14 );
gbc.gridy = 20;
gbl.setConstraints(text15 , gbc);
panel1.add(text15 );
gbc.gridy = 21;
gbl.setConstraints(text16 , gbc);
panel1.add(text16 );
gbc.gridy = 22;
gbl.setConstraints(text17 , gbc);
panel1.add(text17 );
gbc.gridy = 23;
gbl.setConstraints(text18 , gbc);
panel1.add(text18 );
//Listen
JLabel p_gesch =new JLabel("Geschwister:");
JLabel p_eltern =new JLabel("Eltern:");
JLabel p_kinder =new JLabel("Kinder:");
gbc.gridy = 1;
gbc.gridx = 4;
gbc.gridheight = 1;
gbl.setConstraints(p_gesch , gbc);
panel1.add(p_gesch );
gbc.gridy = 19;
gbl.setConstraints(p_eltern , gbc);
panel1.add(p_eltern );
gbc.gridy = 10;
gbl.setConstraints(p_kinder , gbc);
panel1.add(p_kinder);
gbc.gridy = 2;
gbc.gridheight = 8;
gbc.gridwidth = 1;
gbl.setConstraints(list, gbc);
panel1.add(list);
gbc.gridy = 11;
gbc.gridheight = 8;
gbl.setConstraints(list3, gbc);
panel1.add(list3);
gbc.gridy =20;
gbc.gridheight = 3;
gbl.setConstraints(list2, gbc);
panel1.add(list2);
list.addMouseListener(new ActionJList(list));
list2.addMouseListener(new ActionJList(list2));
list3.addMouseListener(new ActionJList(list3));
list.setBorder(BorderFactory.createLineBorder(Color.BLACK));
list2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
list3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
//Eintrageflächen
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridwidth = 2;
gbc.gridy = 1;
gbl.setConstraints(p_name, gbc);
p_name.setColumns(6);
panel1.add(p_name);
gbc.gridy = 2;
gbl.setConstraints(p_vorname, gbc);
panel1.add(p_vorname);
gbc.gridy = 3;
gbl.setConstraints(p_weitername , gbc);
panel1.add(p_weitername );
gbc.gridy = 4;
gbl.setConstraints(p_gebname, gbc);
panel1.add(p_gebname);
gbc.gridy = 5;
gbl.setConstraints(p_gebam, gbc);
panel1.add(p_gebam);
gbc.gridy = 6;
gbl.setConstraints(p_gebin, gbc);
panel1.add(p_gebin);
gbc.gridy = 7;
gbl.setConstraints(p_geschlecht, gbc);
panel1.add(p_geschlecht);
gbc.gridy = 8;
gbl.setConstraints(p_getauftam, gbc);
panel1.add(p_getauftam);
gbc.gridy = 9;
gbl.setConstraints(p_getauftin, gbc);
panel1.add(p_getauftin);
gbc.gridy = 10;
gbl.setConstraints(p_konfession, gbc);
panel1.add(p_konfession);
gbc.gridy = 11;
gbc.gridwidth = 1;
gbl.setConstraints(p_beruf, gbc);
p_beruf.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXX");
panel1.add(p_beruf);
gbc.gridx = 2;
gbc.gridheight = 3;
JButton p_beruf_ändern = new JButton("Ändern");
p_beruf_ändern.setVerticalAlignment(SwingConstants.TOP);
Font drehen_von_schrift = p_beruf_ändern.getFont();
AffineTransform at = new AffineTransform();
at.rotate( 1.57d );
p_beruf_ändern.setFont(drehen_von_schrift.deriveFont( at ));
gbl.setConstraints(p_beruf_ändern , gbc);
panel1.add(p_beruf_ändern);
gbc.gridx = 1;
gbc.gridwidth = 1;
gbc.gridy = 12;
gbc.gridheight = 5;
p_notiz.setEditable(true);
gbl.setConstraints(p_notiz, gbc);
p_notiz.setLineWrap(true);
JScrollPane scroll_notiz =new JScrollPane();
scroll_notiz.getViewport().add(p_notiz);
scroll_notiz.getViewport().setView(p_notiz);
panel1.add(p_notiz);
gbc.gridy = 17;
gbc.gridheight = 1;
gbc.gridwidth = 1;
p_ehe.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXX");
gbl.setConstraints(p_ehe, gbc);
panel1.add(p_ehe);
p_ehe.addItemListener(new CMeinActionLauschercombo());
p_ehe.addMouseListener(new Actioncombo(p_ehe));
gbc.gridx = 2;
gbc.gridheight = 3;
JButton p_ehe_ändern = new JButton("Ändern");
p_ehe_ändern.setVerticalAlignment(SwingConstants.TOP);
drehen_von_schrift = p_ehe_ändern.getFont();
p_ehe_ändern.setFont(drehen_von_schrift.deriveFont( at ));
gbl.setConstraints(p_ehe_ändern , gbc);
panel1.add(p_ehe_ändern);
gbc.gridheight = 1;
gbc.gridx = 1;
gbc.gridwidth = 1;
gbc.gridy = 18;
gbl.setConstraints(p_hocham, gbc);
panel1.add(p_hocham);
gbc.gridy = 19;
gbl.setConstraints(p_hochin, gbc);
panel1.add(p_hochin);
gbc.gridy = 20;
gbc.gridwidth = 2;
gbl.setConstraints(p_totam, gbc);
panel1.add(p_totam);
gbc.gridy = 21;
gbl.setConstraints(p_totin, gbc);
panel1.add(p_totin);
gbc.gridy = 22;
gbl.setConstraints(p_beerdigtam, gbc);
panel1.add(p_beerdigtam);
gbc.gridy = 23;
gbl.setConstraints(p_beerdigtin, gbc);
panel1.add(p_beerdigtin);
JLabel text21 =new JLabel("....................................................................................................................................................................................................................................................................................................................... ");
gbc.gridy = 24;
gbc.gridwidth = 6;
gbc.gridx=0;
gbl.setConstraints(text21 , gbc);
panel1.add(text21 );
//Bildfläsche
m_bildanzeige = new CBildLeinwand();
m_dateiname = null;
m_aktBild = null;
gbc.gridx=3;
gbc.gridy=1;
gbc.gridwidth = 1;
gbc.gridheight = 22;
m_bildanzeige.setPreferredSize(new Dimension(50, 100));
gbl.setConstraints(m_bildanzeige, gbc);
panel1.add(m_bildanzeige);
gbc.gridx=3;
gbc.gridy=23;
gbc.gridheight = 1;
gbl.setConstraints(bildlaufleiste, gbc);
bildlaufleiste.addAdjustmentListener(this);
panel1.add(bildlaufleiste);
// Kopf schaltfläschen
Panel panel2 = new Panel();
panel2.setLayout(new FlowLayout());
gbc.gridx=0;
gbc.gridy=0;
gbl.setConstraints(p_num , gbc);
panel2.add(p_num);
gbc.gridx=1;
gbl.setConstraints(p_nummer , gbc);
panel2.add(p_nummer);
gbc.gridx=2;
gbc.gridheight = 1;
JButton f1 = new JButton("V");
gbl.setConstraints(f1 , gbc);
panel2.add(f1);
gbc.gridx=3;
JButton f2 = new JButton("M");
gbl.setConstraints(f2 , gbc);
panel2.add(f2);
f1.addActionListener(new CMeinActionLauscher());
f2.addActionListener(new CMeinActionLauscher());
gbc.gridwidth = 1;
gbc.gridheight = 1;
JLabel breite1 =new JLabel("...............................");
gbc.gridy = 0;
gbc.gridx=0;
gbl.setConstraints(breite1 , gbc);
panel1.add(breite1);
JLabel breite2 =new JLabel("............................................................................");
gbc.gridy = 0;
gbc.gridx=1;
gbc.gridwidth = 2;
gbl.setConstraints(breite2 , gbc);
panel1.add(breite2 );
JLabel breite3 =new JLabel("...........................................................................................................................................");
gbc.gridy = 0;
gbc.gridx=3;
gbc.gridwidth = 1;
gbl.setConstraints(breite3 , gbc);
panel1.add(breite3 );
JLabel breite4 =new JLabel("........................................................");
gbc.gridy = 0;
gbc.gridx=4;
gbl.setConstraints(breite4 , gbc);
panel1.add(breite4 );
JLabel breite5 =new JLabel("....");
gbc.gridy = 0;
gbc.gridx=5;
gbl.setConstraints(breite5 , gbc);
panel1.add(breite5 );
//allgemeinenotizen
gbc.gridy = 25;
gbc.gridx = 0;
gbc.gridheight = 3;
gbc.gridwidth = 6;
gbc.weighty =1;
p_notallg.setEditable(true);
p_notallg.setLineWrap(true);
JScrollPane scroll_notiz2 =new JScrollPane();
scroll_notiz2.getViewport().add(p_notallg);
scroll_notiz2.getViewport().setView(p_notallg);
p_notallg.setBorder(BorderFactory.createLineBorder(Color.BLACK));
gbl.setConstraints(p_notallg, gbc);
panel1.add(p_notallg);
//Alles aufnehmen
setLayout(new BorderLayout());
add(panel2, BorderLayout.NORTH);
add(panel1, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}