我有一个MessageWindow类,必须通过for循环初始化4次,所以我可以在JWindow中看到4条消息3秒钟。
问题是,我只看到最后一个因为Timer( 3000, new ActionListener() { ... });
不能为其他人工作。我应该改变什么?
import javax.swing.Timer;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class Test extends JFrame {
private int playerInAction;
public Test() {
setVisible( true );
setSize( 800, 500 );
setLocationRelativeTo( null );
for( int i = 1; i < 5; i++ ) {
playerInAction = i;
System.out.println( "Player in Action: " + playerInAction );
showJWindow();
}
}
public static void main(String[] args) {
Test c = new Test();
c.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void showJWindow() {
final MessageWindow window = new MessageWindow( "John", playerInAction );
window.setVisible( true );
Timer timer = new Timer( 3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
window.dispose();
}
});
timer.setRepeats( false );
timer.start();
}
class MessageWindow extends JWindow {
private Message m;
private String[] player;
MessageWindow( String playerName, int p ) {
setSize( 320, 100 );
setLocationRelativeTo( null );
player = new String[2];
player[0] = "";
player[1] = "";
switch( p ) {
case 1: player[0] += playerName; break;
case 2: player[0] += "Brian"; break;
case 3: player[0] += "Elisabeth"; break;
case 4: player[0] += "Jack"; break;
}
player[0] += "'s turn.";
// player[1] += ...
m = new Message( player );
m.setBackground( Color.DARK_GRAY );
// m.setOpaque( true );
getContentPane().add( m );
}
private class Message extends JPanel {
private Graphics2D graphic;
private String[] player;
Message( String[] player ) {
this.player = player;
}
private void drawString( Graphics g, String[] text, int x, int y ) {
int stringLen1 = (int) graphic.getFontMetrics().getStringBounds( text[0], graphic ).getWidth();
int start1 = ( getSize().width - stringLen1 ) / 2 - 30;
int stringLen2 = (int) graphic.getFontMetrics().getStringBounds( text[1], graphic ).getWidth();
int start2 = ( getSize().width - stringLen2 ) / 2;
g.drawString( text[0], start1 + x, y += g.getFontMetrics().getHeight() );
g.drawString( text[1], start2, y += g.getFontMetrics().getHeight() + 10 );
}
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
graphic = ( Graphics2D ) g;
Font font2 = new Font( "Segoe Print", Font.BOLD, 20 );
graphic.setFont( font2 );
graphic.setColor( Color.WHITE );
drawString( g, player, 30, 0 );
// player[1] = "";
graphic.setColor( Color.RED );
graphic.drawRect( 0, 0, getSize().width - 1, getSize().height - 1 );
}
}
}
}
答案 0 :(得分:1)
显示JWindow是非阻塞的,所以你的代码应该产生4个JWindows,一个在另一个之上,所有这些都显示最后一个玩家(当你改变了int变量时)。
如果你想一个接一个地显示4个JWindows(为什么?重用窗口是不是更容易?),那么你应该显示新窗口并在前一个触发后启动一个新的计时器。