所以这就是我的情况:我有一个计时器,它会在后台更新值,直到某个值达到某个点。
但是为了让我看到每个值的更新,我目前必须添加一个“new ListWindow();” 我的 for 循环中的语句。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/**
* Write a description of class Timing here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Timing
{
/**
* Constructor for objects of class Timing
*/
public static void Timer()
{
Timer timer = new Timer(250, new TimerListener());
timer.start();
}
private static class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
while(valX != 100)
{
Timer();
MainProg.valX += 5;
// new ListWindow(); //statement goes here. it creates a new window
//each time it updates the value.
}
}
}
}
我的问题是:我不想要一个新窗口。我希望该值来自的列表(见下文)更新其值并刷新当前窗口中的列表。也许点击按钮。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
/**
* Write a description of class ListWindow here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ListWindow extends JFrame
{
private JPanel StatsPanel; //holds stats
private JPanel ButtonPanel; //holds stats
private JList StatList; //inventory
private JButton RefreshButton; // a button
private String[] Stats =
{"Stuff " + Stuff, "Value " + valX, "Test " + per};
/**
* Constructor
*/
public ListWindow()
{
setTitle("STATS");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildStatsPanel();
buildButtonPanel();
RefreshButton.addActionListener(new ButtonListener());
add(StatsPanel, BorderLayout.CENTER);
add(ButtonPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public void buildStatsPanel()
{
StatsPanel = new JPanel();
StatList = new JList(Stats);
StatList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
StatsPanel.add(StatList);
}
public void buildButtonPanel()
{
ButtonPanel = new JPanel();
RefreshButton = new JButton("Refresh");
ButtonPanel.add(RefreshButton);
}
private class ButtonListener
implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if(actionCommand.equals("Refresh"))
{
pack();
invalidate();
validate();
}
}
}
public void RunMain(String[] args)
{
new ListWindow();
}
}
另外:这是我运行东西的主要方法。您还应该注意到,我在此处粘贴的另外两个片段中有主要方法。我这样做是为了可以单独运行,以便快速调试和将来使用。
/**
* Write a description of class MainProg here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MainProg
{
public static int stuff = 100;
public static int valX = 0;
public static int per = 100;
/**
* Constructor for objects of class Game
*/
public static void main()
{
new MoveWindow();
new ListWindow();
while (valX != 100)
{
Timing.Timer();
}
}
}
我对 java 很陌生,我一直在寻找其他解决方案,但无法找到我认为我需要的东西。也许我确实找到了它,并且没有将其视为解决方案。
感谢所有帮助,在此先感谢。