From 8429d5d7ae177acb8de5f3ee51492bdee2416499 Mon Sep 17 00:00:00 2001 From: Matti Date: Thu, 6 Jun 2024 22:06:33 +0200 Subject: [PATCH] Add Maker methods --- src/part5/aufg.java | 79 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/src/part5/aufg.java b/src/part5/aufg.java index fc3e1ee..4dad796 100644 --- a/src/part5/aufg.java +++ b/src/part5/aufg.java @@ -2,6 +2,7 @@ package part5; import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; public class aufg { @@ -33,7 +34,7 @@ public class aufg { } public void loadDesign(){ - addLabeledTextField("TextBox"); + addLabeledTextField("TextBox", false); addMenuBar(); addPasswordField(); addImage("Medien/Bilder/info_I.png"); @@ -76,45 +77,88 @@ public class aufg { } public void addLabel(String text){ + this.contentPane.add(makeLabel(text)); + } + public JLabel makeLabel(String text){ JLabel label = new JLabel(text); - this.contentPane.add(label); + return label; } public void addTextField(int columns){ + this.contentPane.add(makeTextField(columns)); + } + public JTextField makeTextField(int columns){ JTextField textField = new JTextField(columns); - this.contentPane.add(textField); + return textField; } - public void addLabeledTextField(String labelText){ - addLabel(labelText); - addTextField(10); + public void addLabeledTextField(String labelText, boolean makeVertical){ + this.contentPane.add(makeLabeledTextField(labelText,makeVertical)); + } + public Box makeLabeledTextField(String labelText, boolean makeVertical){ + Box b; + + if (makeVertical) { + b = Box.createVerticalBox(); + }else{ + b = Box.createHorizontalBox(); + } + + b.add(makeLabel(labelText)); + b.add(makeTextField(10)); + return b; } public void addPasswordField(){ + this.contentPane.add(makePasswordField()); + } + public JPasswordField makePasswordField(){ JPasswordField passwordField = new JPasswordField(10); - this.contentPane.add(passwordField); + return passwordField; } - public void addLabeledPasswordField(String label){ - addLabel(label); - addPasswordField(); + public void addLabeledPasswordField(String label, boolean makeVertical){ + this.contentPane.add(makeLabeledPasswordField(label,makeVertical)); + } + public Box makeLabeledPasswordField(String label, boolean makeVertical){ + Box b; + + if (makeVertical) { + b = Box.createVerticalBox(); + }else{ + b = Box.createHorizontalBox(); + } + + b.add(makeLabel(label)); + b.add(makePasswordField()); + + return b; } public void addTextArea(int rows, int cols){ + this.contentPane.add(makeTextArea(rows, cols)); + } + public JTextArea makeTextArea(int rows, int cols){ JTextArea textArea = new JTextArea(rows, cols); - this.contentPane.add(textArea); + return textArea; } public void addImage(String link){ - Icon i = new ImageIcon(link); - JButton grafik = new JButton(i); - this.contentPane.add(grafik); + this.contentPane.add(makeImage(link)); + } + public JButton makeImage(String link){ + Icon i = new ImageIcon(link); + return new JButton(i); } - public void addRadioButtons(String[] options){ + this.contentPane.add(makeRadioButtons(options)); + } + public Box makeRadioButtons(String[] options){ Box b = Box.createVerticalBox(); ButtonGroup group = new ButtonGroup(); - this.contentPane.setLayout(new FlowLayout()); + // this.contentPane.setLayout(new FlowLayout()); Don't know what this does, + // Can't be bothered to look it up + // Can be bothered to type this tho for (String option: options){ JRadioButton button = new JRadioButton(option); @@ -123,6 +167,7 @@ public class aufg { } b.add(Box.createVerticalStrut(10)); - this.contentPane.add(b); + return b; } + } \ No newline at end of file