From 981b31dd99ff1a4242b079a8b17558c2a06ff6be Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 22 Jan 2022 14:50:29 +0100 Subject: [PATCH] leaderboard: added gui layout --- .../java/de/tims/leaderboard/Leaderboard.java | 54 ++++++++++++++++++- .../tims/leaderboard/LeaderboardGUITest.java | 2 +- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/leaderboard/Leaderboard.java b/src/main/java/de/tims/leaderboard/Leaderboard.java index 3e0d377..54fe06a 100644 --- a/src/main/java/de/tims/leaderboard/Leaderboard.java +++ b/src/main/java/de/tims/leaderboard/Leaderboard.java @@ -1,5 +1,8 @@ package de.tims.leaderboard; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; import java.util.List; import javax.swing.*; @@ -44,11 +47,60 @@ public class Leaderboard { public JPanel buildLeaderboard() { JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new GridBagLayout()); + + GridBagConstraints gbc = new GridBagConstraints(); Player[] top10Players = getTop10Players(); + gbc.gridx = 0; + gbc.gridy = 0; + gbc.weightx = 0.5; + gbc.weighty = 0.1; + gbc.anchor = GridBagConstraints.LINE_END; + gbc.insets = new Insets(5, 0, 5, 5); + mainPanel.add(new JLabel("Platzierung"), gbc); + + gbc.gridx = 1; + gbc.gridy = 0; + gbc.weightx = 0.0; + gbc.weighty = 0.1; + gbc.anchor = GridBagConstraints.LINE_START; + gbc.insets = new Insets(5, 5, 5, 10); + mainPanel.add(new JLabel("Name"), gbc); + + gbc.gridx = 2; + gbc.gridy = 0; + gbc.weightx = 0.5; + gbc.weighty = 0.1; + gbc.anchor = GridBagConstraints.LINE_START; + gbc.insets = new Insets(5, 10, 5, 0); + mainPanel.add(new JLabel("Punkte"), gbc); + for (int i = 0; i < top10Players.length; i++) { - mainPanel.add(new JLabel(i + 1 + ". " + top10Players[i].getName() + " " + top10Players[i].getPoints())); + gbc.gridx = 0; + gbc.gridy = i + 1; + gbc.weightx = 0.5; + gbc.weighty = 0.1; + gbc.anchor = GridBagConstraints.LINE_END; + gbc.insets = new Insets(5, 0, 5, 5); + mainPanel.add(new JLabel((i + 1) + "."), gbc); + + gbc.gridx = 1; + gbc.gridy = i + 1; + gbc.weightx = 0.0; + gbc.weighty = 0.1; + gbc.anchor = GridBagConstraints.LINE_START; + gbc.insets = new Insets(5, 5, 5, 10); + mainPanel.add(new JLabel(top10Players[i].getName()), gbc); + + gbc.gridx = 2; + gbc.gridy = i + 1; + gbc.weightx = 0.5; + gbc.weighty = 0.1; + gbc.anchor = GridBagConstraints.LINE_START; + gbc.insets = new Insets(5, 10, 5, 0); + mainPanel.add(new JLabel("" + top10Players[i].getPoints()), gbc); } return mainPanel; diff --git a/src/main/java/de/tims/leaderboard/LeaderboardGUITest.java b/src/main/java/de/tims/leaderboard/LeaderboardGUITest.java index 2e25051..0e8b826 100644 --- a/src/main/java/de/tims/leaderboard/LeaderboardGUITest.java +++ b/src/main/java/de/tims/leaderboard/LeaderboardGUITest.java @@ -21,7 +21,7 @@ public class LeaderboardGUITest { panel = leaderboard.buildLeaderboard(); frame.add(panel); - frame.setSize(240, 240); + frame.setSize(640, 400); frame.setVisible(true); }