我的Swing应用程序可能不是线程安全的 - 不知道为什么

时间:2012-06-16 05:33:50

标签: java multithreading swing

根据评论者的要求编辑。我希望这是合规的。

第一篇文章!试图理解为什么我的Swing应用程序不会从一个面板前进到下一个面板。以下是代码的一般流程:

public class MainWindow {

JFrame mainFrame;
ChangeablePanel currentScreen; // abstract and extends JPanel, has getters &
setters for a Timer (swing timer), a String (nextScreen), and an Image 
(background image).  also has a close(AWTEvent e) method that simply calls 
"this.setVisible(false);"

public MainWindow() {
    mainFrame = new JFrame("New Arcana");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    setTitleFrame();
} // MainFrame constructor

public void changeFrame(String frameType, String frameName) {               
    switch (frameType) {
        case "Title":
            setTitleFrame();
            break;
        case "Town":
            setTownFrame(frameName);
            break;
        case "Movie":
            setMovieFrame(frameName);               
            break;
        default:
            break;
    } // switch     
} // changeFrame

private void setTitleFrame() {      
    currentScreen = new TitlePanel();
    currentScreen.addComponentListener(new ScreenChangeListener());
    ...             
    mainFrame.setContentPane(currentScreen);
    mainFrame.setSize(titleScreenLength, titleScreenHeight); // put constants here if you want
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible(true);
} // setTitleFrame

private void setTownFrame(String townName) {        
    currentScreen = new TownPanel(townName);
    currentScreen.addComponentListener(new ScreenChangeListener());
    ...     
    mainFrame.setContentPane(currentScreen);        
    mainFrame.setSize(townScreenLength, townScreenHeight); // put constants here if you want
    mainFrame.setVisible(true);     
} // setTownFrame

private void setMovieFrame(String movieName) {      
    currentScreen = new MoviePanel(movieName);
    currentScreen.addComponentListener(new ScreenChangeListener());
    ...
    mainFrame.setContentPane(currentScreen);
    mainFrame.setSize(titleScreenLength, titleScreenHeight); // put constants here if you want      
    mainFrame.setVisible(true);     
} // setMovieFrame

private class ScreenChangeListener implements ComponentListener {
    @Override
    public void componentHidden(ComponentEvent e) {
        gotoNextScreen(e);
    }

    public void componentMoved(ComponentEvent e) {}

    public void componentResized(ComponentEvent e) {}

    public void componentShown(ComponentEvent e) {}     
} // ScreenChangeListener

public void gotoNextScreen(ComponentEvent e) {        
    changeFrame(currentScreen.getNextScreen(), null);   
}
} // MainWindow

public class Start {
...
public static void main(String[] args) {
    initialize();   

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new MainWindow();
        }
    });   
} // main
...
} // Start

public class TitlePanel extends ChangeablePanel implements ActionListener {

JButton newGame, continueGame;

public TitlePanel() {       
    setFocusable(true);     
    ...

    newGame = new JButton("New Game");
    continueGame = new JButton("Continue");     

    newGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setNextScreen("Movie");
            close(e);               
        }       
    });

    add(newGame);
    add(continueGame);

    createTimer(10, this);
    getTimer().start();
} // TitlePanel constructor

@Override
public void actionPerformed(ActionEvent e) {
    repaint();      
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;        
    drawTitleScreen(g2d);       
} // paintComponent

private void drawTitleScreen(Graphics2D g2d) {
    g2d.drawImage(getBGImage(), 0, 0, null);
    newGame.setLocation(170, 550);
    continueGame.setLocation(605, 550);
} // drawTitleScreen
} // TitlePanel

public class MoviePanel extends ChangeablePanel implements ActionListener { 

public MoviePanel(String movieName) {       
    setFocusable(true); 

    addKeyListener(new AnyKeyActionListener());
    ...
    createTimer(10, this);
    getTimer().start();
} // TitlePanel constructor

@Override
public void actionPerformed(ActionEvent e) {
    repaint();
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;        
    drawMovie(g2d);     
} // paintComponent 

private void drawMovie(Graphics2D g2d) {
    g2d.drawImage(getBGImage(), 0, 0, null);        
} // drawTitleScreen    

private class AnyKeyActionListener extends KeyAdapter {     
    public void keyTyped(KeyEvent e) {
        setNextScreen("Town");
        close(e);
    } // keyPressed     
} // listener to check for keystrokes   
} // MoviePanel

当应用程序根据用户输入(当前只编码MoviePanel和TownPanel)进行前进时,MainFrame将填充更多帧,并且它们的代码非常类似于此 - 我也粘贴了MoviePanel。 / p>

执行在上面基于KeyAdapter的侦听器之后发生故障。但是,当我在Eclipse中使用断点在调试模式下运行我的应用程序时,这确实做了它应该做的事情并从MoviePanel前进到TownPanel。正因为如此,我怀疑线程是这里的罪魁祸首。请注意,我在上面的代码块中尝试了许多不同的SwingUtilities.invokeLater()技术组合,但它没有改变任何东西。任何帮助,将不胜感激;谢谢!

1 个答案:

答案 0 :(得分:1)

执行以下操作:

  • invokeLater用于创建GUI事件调度线程
  • 施工期间没有重绘()
  • setVisible last

特别是在事件监听器上,再次使用invokeLater,让按钮等响应,然后对响应采取行动。

public static void main(String[] args) {
    ...
    SwingUtilities.invokeLater() {
        @Override()
        new Runnable() {
            new MainFrame().setVisible(true);
        }
    };        
}

代码审核

TitlePanel.TitlePanel中更好地使用绝对布局(即null),而不是在绘画代码中使用setLocation

    setLayout(null);
    newGame = new JButton("New Game");
    continueGame = new JButton("Continue");
    newGame.setBounds(170, 550, 120, 24);
    continueGame.setBounds(605, 550, 120, 24);

ChangeablePanel.close中也确保timer.stop()

MainWindow中使用invokeLater

public void gotoNextScreen(ComponentEvent e) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            changeFrame(currentScreen.getNextScreen(), null);
        }
    });   
}

MoviePanel 中,我看不到addKeyListener可以发挥作用;也许左边的代码?或者这可能是你看到的错误? 此外,我发现一个简单的repaint()可疑;本来应该有这样的事情:

public void actionPerformed(ActionEvent e) {
    invalidate();
    repaint(10L);
}