From 089fcbfccd75b77daf843b22e128beeee1d1dc07 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 9 Jan 2019 15:38:41 +0100 Subject: [PATCH] Properly add all view JComponents to the JPanel in the main JFrame. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit And implement one view properly. Signed-off-by: Jérôme Benoit --- src/Eye.java | 5 ----- src/EyeView.java | 10 ++++++---- src/FirstnameView.java | 14 +++++++++++--- src/LevenshteinDistance.java | 2 -- src/MainWindowsView.java | 24 +++++++++++++++++++----- src/OriginView.java | 7 ++++++- src/PersonView.java | 20 ++++++++++---------- src/SizeView.java | 7 ++++++- src/WeightView.java | 7 ++++++- 9 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/Eye.java b/src/Eye.java index c3b836d..6edc8a5 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -7,7 +7,6 @@ public class Eye implements Comparable { private List colorList = Arrays.asList("black", "green", "blue", "brown"); /** - * * @param color */ Eye(String color) { @@ -15,7 +14,6 @@ public class Eye implements Comparable { } /** - * * @return */ public Color getColor() { @@ -23,7 +21,6 @@ public class Eye implements Comparable { } /** - * * @param color */ public void setColor(String color) { @@ -39,7 +36,6 @@ public class Eye implements Comparable { } /** - * * @param color * @return */ @@ -48,7 +44,6 @@ public class Eye implements Comparable { } /** - * * @param eye * @return */ diff --git a/src/EyeView.java b/src/EyeView.java index 3866493..8fbad26 100644 --- a/src/EyeView.java +++ b/src/EyeView.java @@ -1,8 +1,12 @@ -public class EyeView { +import javax.swing.*; + +public class EyeView extends JComponent { private Eye eyeObj; + EyeView() { + } + /** - * * @param eyeObj */ EyeView(Eye eyeObj) { @@ -10,7 +14,6 @@ public class EyeView { } /** - * * @return */ public Eye getEyeObj() { @@ -18,7 +21,6 @@ public class EyeView { } /** - * * @param eyeObj */ public void setEyeObj(Eye eyeObj) { diff --git a/src/FirstnameView.java b/src/FirstnameView.java index 18323cf..e9c387b 100644 --- a/src/FirstnameView.java +++ b/src/FirstnameView.java @@ -1,8 +1,16 @@ -public class FirstnameView { +import javax.swing.*; + +public class FirstnameView extends JComponent { private Firstname firstnameObj; + private JTextField textField; + private JLabel label; - FirstnameView(Firstname firstnameObj) { - setFirstnameObj(firstnameObj); + FirstnameView(int length) { + this.label = new JLabel(); + this.label.setText("Firstname"); + this.textField = new JTextField(length); + add(label); + add(textField); } public Firstname getFirstnameObj() { diff --git a/src/LevenshteinDistance.java b/src/LevenshteinDistance.java index 6a56650..3706772 100644 --- a/src/LevenshteinDistance.java +++ b/src/LevenshteinDistance.java @@ -1,7 +1,6 @@ public class LevenshteinDistance { /** - * * @param a * @param b * @param c @@ -12,7 +11,6 @@ public class LevenshteinDistance { } /** - * * @param lhs * @param rhs * @return diff --git a/src/MainWindowsView.java b/src/MainWindowsView.java index f4f837f..065c90d 100644 --- a/src/MainWindowsView.java +++ b/src/MainWindowsView.java @@ -1,18 +1,32 @@ import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; public class MainWindowsView extends JFrame { MainWindowsView(String title) { //Create and set up the window. setTitle(title); + setSize(300, 300); + setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new PersonView(); - //TODO: Add content to the panel - //JLabel label = new JLabel("Hello World"); - //panel.add(label); + //Get all Swing/AWT primitive components in the views and add them to the panel. + ArrayList components = new ArrayList<>(); + for (int i = 0; i < panel.getComponentCount(); i++) { + if ((panel.getComponent(i) instanceof Container)) { + Container subContainer = (Container) panel.getComponent(i); + for (int j = 0; j < subContainer.getComponentCount(); j++) { + components.add(subContainer.getComponent(j)); + } + } + } + for (Component component : components) { + panel.add(component); + } - getContentPane().add(panel); + setContentPane(panel); } /** @@ -23,7 +37,7 @@ public class MainWindowsView extends JFrame { public void showGUI() { //Display the window. - this.pack(); + //this.pack(); this.setVisible(true); } diff --git a/src/OriginView.java b/src/OriginView.java index 58dec5d..2fc4189 100644 --- a/src/OriginView.java +++ b/src/OriginView.java @@ -1,6 +1,11 @@ -public class OriginView { +import javax.swing.*; + +public class OriginView extends JComponent { private Origin originObj; + OriginView() { + } + OriginView(Origin originObj) { setOriginObj(originObj); } diff --git a/src/PersonView.java b/src/PersonView.java index 9621e47..a328f47 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -8,7 +8,17 @@ public class PersonView extends JPanel { private EyeView eyeView; PersonView() { + setFirstnameView(new FirstnameView(10)); + setOriginView(new OriginView()); + setSizeView(new SizeView()); + setWeightView(new WeightView()); + setEyeView(new EyeView()); + add(firstnameView); + add(originView); + add(sizeView); + add(weightView); + add(eyeView); } PersonView(FirstnameView firstnameView, OriginView originView, SizeView sizeView, WeightView weightView, EyeView eyeView) { @@ -20,7 +30,6 @@ public class PersonView extends JPanel { } /** - * * @return */ public FirstnameView getFirstnameView() { @@ -28,7 +37,6 @@ public class PersonView extends JPanel { } /** - * * @param firstnameView */ public void setFirstnameView(FirstnameView firstnameView) { @@ -36,7 +44,6 @@ public class PersonView extends JPanel { } /** - * * @return */ public OriginView getOriginView() { @@ -44,7 +51,6 @@ public class PersonView extends JPanel { } /** - * * @param originView */ public void setOriginView(OriginView originView) { @@ -52,7 +58,6 @@ public class PersonView extends JPanel { } /** - * * @return */ public SizeView getSizeView() { @@ -60,7 +65,6 @@ public class PersonView extends JPanel { } /** - * * @param sizeView */ public void setSizeView(SizeView sizeView) { @@ -68,7 +72,6 @@ public class PersonView extends JPanel { } /** - * * @return */ public WeightView getWeightView() { @@ -76,7 +79,6 @@ public class PersonView extends JPanel { } /** - * * @param weightView */ public void setWeightView(WeightView weightView) { @@ -84,7 +86,6 @@ public class PersonView extends JPanel { } /** - * * @param eyeView */ public void setEyeView(EyeView eyeView) { @@ -92,7 +93,6 @@ public class PersonView extends JPanel { } /** - * * @return */ public EyeView getEyeView() { diff --git a/src/SizeView.java b/src/SizeView.java index 4e7e4a0..1d280c5 100644 --- a/src/SizeView.java +++ b/src/SizeView.java @@ -1,6 +1,11 @@ -public class SizeView { +import javax.swing.*; + +public class SizeView extends JComponent { private Size sizeObj; + SizeView() { + } + SizeView(Size sizeObj) { setSizeObj(sizeObj); } diff --git a/src/WeightView.java b/src/WeightView.java index e6b69c5..3e6d00f 100644 --- a/src/WeightView.java +++ b/src/WeightView.java @@ -1,6 +1,11 @@ -public class WeightView { +import javax.swing.*; + +public class WeightView extends JComponent { private Weight weightObj; + WeightView() { + } + WeightView(Weight weightObj) { setWeightObj(weightObj); } -- 2.34.1