gpt4 book ai didi

java - 尝试不断更新一个jframe

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:25 25 4
gpt4 key购买 nike

所以我创建了一个程序,当你运行它时,你可以输入足球比赛的得分。每当分数更新时,我希望程序显示一个带有分数的 JFrame。当我启动程序时,它会显示分数,但每当分数更新时,它都会创建一个包含更新分数的全新窗口。是否可以将分数保留在一个 JFrame 上?

 package com.football.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.RootPaneContainer;
import javax.swing.SwingConstants;

public class Main {




public static void main(String args[]) throws InterruptedException{




String b;

Scanner in = new Scanner(System.in);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");


JOptionPane.showMessageDialog (null, "College? If yes type College. If Pro type Pro.", "Pick a type", JOptionPane.INFORMATION_MESSAGE);
b = in.nextLine();
if(b.equals("College")){
Thread.sleep(1000);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");

College();
}
private static void College() throws InterruptedException {
String t;
String r;
int s = 0;
int s1 = 0;
int to = 7;
int f = 3;
String w;
String h;
String u;
String i;
int y = 1;
boolean isRunning = true;
int sa = 2;

String g;
Scanner in = new Scanner(System.in);
JOptionPane.showMessageDialog (null, "You chose College!", "You chose college", JOptionPane.INFORMATION_MESSAGE);
Thread.sleep(300);
JOptionPane.showMessageDialog (null, "Please enter the first teams name!", "You chose college", JOptionPane.INFORMATION_MESSAGE);
t = in.nextLine();
JOptionPane.showMessageDialog (null, "Please enter the second teams name!", "You chose college", JOptionPane.INFORMATION_MESSAGE);
r = in.nextLine();
while(isRunning = true){
w = (t + ": " + s );

h = (r + ": " + s1);
JFrame frame = new JFrame("Game Score");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
JLabel textLabel = new JLabel(w);
textLabel.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
u = in.nextLine();
if(u.equals(t)){
System.out.println("If " + t + " got a touchdown type T. If they got a field goal type F. If they got a safety type S. If you need to add one point just because type +1. If you need to subtract one point just because type -1.");
i = in.nextLine();
if(i.equals("T")){

s = s + to;
frame.remove(frame);
}

if(i.equals("F")){
s = s + f;
frame.getContentPane().removeAll();
}
if(i.equals("S")){
s = s + sa;
frame.getContentPane().removeAll();
}
if(i.equals("+1")){
s = s + y;
frame.getContentPane().removeAll();
}
if(i.equals("-1")){
s = s - y;
frame.getContentPane().removeAll();
}
}
else if(u.equals(r)) {
System.out.println("If " + r + " got a touchdown type T. If they got a field goal type F. If they got a safety type S. If you need to add one point just because type +1. If you need to subtract one point just because type -1.");
i = in.nextLine();
if(i.equals("T")){

s1 = s1 + to;
}

if(i.equals("F")){
s1 = s1 + f;
}
if(i.equals("S")){
s1 = s + sa;
}
if(i.equals("+1")){
s1 = s1 + y;
}
if(i.equals("-1")){
s1 = s1 - y;
}
}
else if(u.equals("Done")){
System.exit(0);
}

else {
System.out.println("That is not a valid team.");
}
}

}

最佳答案

创建一个new JFrame()一次,将其记住为变量,并不断更新该 JFrame 上的分数。

例如:

public JFrame frame = new JFrame("Score");

然后在该帧上放置一个 JLabel 或任何您想要的内容,并在分数更新时更新它

关于java - 尝试不断更新一个jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27808645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com