Skip to content

Commit 8bd9b36

Browse files
committed
Added update button
1 parent b912218 commit 8bd9b36

4 files changed

Lines changed: 92 additions & 22 deletions

File tree

dependency-reduced-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.eric</groupId>
55
<artifactId>GobangGame</artifactId>
66
<name>GoBangGame</name>
7-
<version>0</version>
7+
<version>2.1</version>
88
<description>Chinese game rewritten in java</description>
99
<inceptionYear>2025</inceptionYear>
1010
<licenses>

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.eric</groupId>
44
<artifactId>GobangGame</artifactId>
5-
<version>0</version>
5+
<version>2.1</version>
66
<inceptionYear>2025</inceptionYear>
77
<organization>
88
<name>Qiuerichanru</name>
@@ -232,5 +232,11 @@
232232
<artifactId>github-api</artifactId>
233233
<version>1.319</version>
234234
</dependency>
235+
<dependency>
236+
<groupId>org.jetbrains</groupId>
237+
<artifactId>annotations</artifactId>
238+
<version>26.0.2</version>
239+
<scope>compile</scope>
240+
</dependency>
235241
</dependencies>
236242
</project>

src/main/java/com/eric/GobangGame/GobangGame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class GobangGame extends JFrame{
1515

16-
public static final double VERSION = 2.0;
16+
public static final double VERSION = 2.1;
1717

1818
// --- 棋盘常量 (仍保留在主类中,因为其他组件需要访问它们来确定尺寸) ---
1919
public static final int ROW = 15;

src/main/java/com/eric/GobangGame/GobangGameUI.java

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
import java.util.MissingResourceException;
1111
import java.util.ResourceBundle;
1212

13+
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
1315
import org.kohsuke.github.GitHub;
1416
import org.kohsuke.github.GitHubBuilder;
1517
import org.kohsuke.github.GHRelease;
1618
import org.kohsuke.github.GHRepository;
1719

20+
1821
/**
1922
* 负责构建和管理所有用户界面元素(菜单、按钮、对话框)
2023
*/
@@ -26,7 +29,7 @@ public class GobangGameUI {
2629
private JComboBox<String> languageComboBox;
2730

2831
// UI组件引用 (由GobangGame持有)
29-
private JButton undoBtn, restartBtn, closeBtn, loadBtn, saveBtn;
32+
private JButton undoBtn, restartBtn, closeBtn, loadBtn, saveBtn, updateBtn;
3033
private JMenuItem openItem, saveItem, closeItem, undoItem, restartItem, settingItem, aboutItem;
3134
private JMenu fileMenu, gameMenu, toolsMenu, aboutMenu;
3235
private JLabel aiState, version;
@@ -37,17 +40,25 @@ public GobangGameUI(GobangGame game, Locale initialLocale) {
3740
}
3841

3942
public double GetLatestVer() {
40-
GHRelease release;
41-
try {
42-
GitHub github = new GitHubBuilder().withOAuthToken("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").build();
43-
GHRepository repo = github.getRepository("QIU2014/GobangGame");
44-
release = repo.getLatestRelease();
45-
double latestVer = Double.parseDouble(String.format("%s", release.getTagName()).replace("GobangGame_v", ""));
46-
return latestVer;
47-
} catch (IOException e) {
48-
e.printStackTrace();
49-
return 0.0;
50-
}
43+
try {
44+
GitHub github = new GitHubBuilder().withOAuthToken("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").build();
45+
GHRepository repo = github.getRepository("QIU2014/GobangGame");
46+
GHRelease release = repo.getLatestRelease();
47+
double latestVer = Double.parseDouble(String.format("%s", release.getTagName()).replace("GoBangGame_v", ""));
48+
return latestVer;
49+
} catch (IOException e) {
50+
e.printStackTrace();
51+
return 0.0;
52+
}
53+
}
54+
55+
public String GetUpdateDownloadURI() throws IOException, URISyntaxException {
56+
GitHub github = new GitHubBuilder().withOAuthToken("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").build();
57+
GHRepository repo = github.getRepository("QIU2014/GobangGame");
58+
GHRelease release = repo.getLatestRelease();
59+
String updateDownloadURI = String.format("https://github.com/QIU2014/GoBangGame/releases/tag/%s", release.getTagName());
60+
System.out.println(updateDownloadURI);
61+
return updateDownloadURI;
5162
}
5263

5364
public ResourceBundle getMessages() {
@@ -109,6 +120,7 @@ public JPanel createButtonPanel() {
109120
// 回到原来的FlowLayout,但减少垂直间距
110121
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 30));
111122
buttonPanel.setPreferredSize(new Dimension(game.getBUTTON_WIDTH(), game.getHeight()));
123+
this.updateBtn = new JButton("Update");
112124
this.saveBtn = new JButton(messages.getString("button.save"));
113125
this.loadBtn = new JButton(messages.getString("button.load"));
114126
this.undoBtn = new JButton(messages.getString("button.undo"));
@@ -117,13 +129,62 @@ public JPanel createButtonPanel() {
117129

118130
this.aiState = new JLabel("AI: IDLE", SwingConstants.CENTER);
119131
try {
120-
if (GetLatestVer() > GobangGame.VERSION) {
121-
this.version = new JLabel(messages.getString("version.update") + "v" + GetLatestVer(), SwingConstants.CENTER);
122-
version.setForeground(Color.RED);
123-
} else {
124-
this.version = new JLabel("Version" + GobangGame.VERSION, SwingConstants.CENTER);
125-
version.setForeground(Color.GREEN);
126-
}
132+
// Initialize with "Please wait..."
133+
this.version = new JLabel("Please wait...", SwingConstants.CENTER);
134+
version.setForeground(Color.GRAY);
135+
136+
SwingWorker<Double, Void> versionWorker = new SwingWorker<Double, Void>() {
137+
@Override
138+
protected Double doInBackground() throws Exception {
139+
return GetLatestVer();
140+
}
141+
142+
@Override
143+
protected void done() {
144+
try {
145+
double latestVer = get(); // This gets the result from doInBackground()
146+
147+
try {
148+
if (latestVer > GobangGame.VERSION) {
149+
version.setText(messages.getString("version.update") + "v" + latestVer);
150+
version.setForeground(Color.RED);
151+
updateBtn.addActionListener(e -> {
152+
try {
153+
Desktop.getDesktop().browse(new URI(GetUpdateDownloadURI()));
154+
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "A browser window opened to the download page!");
155+
} catch (IOException | URISyntaxException ex) {
156+
throw new RuntimeException(ex);
157+
}
158+
});
159+
} else {
160+
version.setText("Version " + GobangGame.VERSION);
161+
version.setForeground(Color.GREEN);
162+
updateBtn.addActionListener(e -> {
163+
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "There are no updates Yet!", "No Update Yet!", JOptionPane.INFORMATION_MESSAGE);
164+
});
165+
}
166+
} catch (MissingResourceException e) {
167+
e.printStackTrace();
168+
System.out.println("Missing resource! Falling back to hard-coded");
169+
if (latestVer > GobangGame.VERSION) {
170+
version.setText("Update detected: v" + latestVer);
171+
version.setForeground(Color.RED);
172+
} else {
173+
version.setText("GobangGame v" + GobangGame.VERSION);
174+
version.setForeground(Color.GREEN);
175+
}
176+
}
177+
178+
} catch (Exception e) {
179+
e.printStackTrace();
180+
version.setText("Error checking version");
181+
version.setForeground(Color.ORANGE);
182+
updateBtn.disable();
183+
}
184+
}
185+
};
186+
187+
versionWorker.execute();
127188
} catch (MissingResourceException e) {
128189
e.printStackTrace();
129190
System.out.println("Missing resource! Falling back to hard-coded");
@@ -139,13 +200,15 @@ public JPanel createButtonPanel() {
139200
Dimension btnSize = new Dimension(game.getBUTTON_WIDTH(), game.getBUTTON_HEIGHT());
140201

141202
// 设置按钮属性
203+
updateBtn.setPreferredSize(btnSize);
142204
saveBtn.setPreferredSize(btnSize);
143205
loadBtn.setPreferredSize(btnSize);
144206
undoBtn.setPreferredSize(btnSize);
145207
restartBtn.setPreferredSize(btnSize);
146208
closeBtn.setPreferredSize(btnSize);
147209

148210
Font font = new Font("宋体", Font.PLAIN, 14);
211+
updateBtn.setFont(font);
149212
saveBtn.setFont(font);
150213
loadBtn.setFont(font);
151214
undoBtn.setFont(font);
@@ -165,6 +228,7 @@ public JPanel createButtonPanel() {
165228
closeBtn.addActionListener(e -> System.exit(0));
166229

167230
// 直接添加组件到buttonPanel
231+
buttonPanel.add(updateBtn);
168232
buttonPanel.add(saveBtn);
169233
buttonPanel.add(loadBtn);
170234
buttonPanel.add(undoBtn);

0 commit comments

Comments
 (0)