2017-02-21 72 views
0

所以,我在我的Java Swing項目中遇到了這個問題,我正在製作一個bot,它會自動運行while循環並輸出用戶可以在文本字段中定義的文本。Java之後沒有UI更新線程

在這裏你可以看到我的代碼添加一個TextLine到一個JLabel到我的框架:

btnStartTalking = new JButton("Start AutoTalk"); 
    btnStartTalking.setBounds(144, 143, 146, 23); 
    btnStartTalking.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 

      int robotDelay = Integer.parseInt(timeTalk.getText()); 

      Talk.action = "start"; 

      robotTalk = txtTalk.getText(); 

      if (rdbtnRed.isSelected() == true) { 

       robotTalk = "red:" + txtTalk.getText(); 

      } else if (rdbtnFlash.isSelected() == true) { 

       robotTalk = "flash1:" + txtTalk.getText(); 

      } else if (rdbtnGlow.isSelected() == true) { 

       robotTalk = "glow1:" + txtTalk.getText(); 

      } 

      autoTalkStart = new Runnable() { 
       public void run() { 
        try { 

         btnStartTalking.setEnabled(false); 
         btnStopTalking.setEnabled(true); 
         btnAddTalking.setEnabled(false); 
         Talk test = new Talk(robotDelay); 
        } catch (AWTException | InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      }; 
      new Thread(autoTalkStart).start(); 




     } 
    }); 

通過該按鈕:

btnAddTalking = new JButton("Add"); 
    btnAddTalking.setBounds(144, 211, 146, 23); 
    btnAddTalking.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      if(!txtTalk.getText().equals("")) { 


      JLabel newLabel = new JLabel(txtTalk.getText()); 
      newLabel.addMouseListener(new MouseAdapter() { 

      public void mouseClicked(MouseEvent arg0) { 

        frame.remove(newLabel); 
        frame.repaint(); 

       } 
      });    

      newLabel.setHorizontalAlignment(SwingConstants.CENTER); 

      newLabel.setBounds(159, yLabel, 101, 20); 

      frame.getContentPane().add(newLabel); 

      yLabel = yLabel + 20; 

      frame.getContentPane().setLayout(null); 

      frame.repaint(); 
      Talk.addTalk(txtTalk.getText()); 

     } 
     } 
    }); 

    frame.getContentPane().add(btnAddTalking); 
    frame.getContentPane().add(lblNewLabel); 
    frame.getContentPane().add(lblDelayInMs); 

之後,我按下面的按鈕,開始我的機器人我開始一個線程,它貫穿輸出字符串的代碼。但是在運行該線程之後,我的框架不會再顯示新添加的jlabel,而是通過單擊btnStartTalking按鈕運行線程後單擊btnAddTalking按鈕來添加的!所以,我的問題是:現在是否有人會這樣做(爲什麼我的UI在運行線程後不再更新)?

格爾茨,

Jarnov

在這裏你可以看到我的整個代碼兩班(1級):

import java.awt.EventQueue; 
import java.awt.event.InputEvent; 

import javax.swing.JFrame; 
import javax.swing.JTextField; 

import com.sun.glass.events.KeyEvent; 
import com.sun.glass.ui.Robot; 

import java.awt.AWTException; 
import java.awt.BorderLayout; 
import javax.swing.BoxLayout; 
import javax.swing.ButtonGroup; 
import javax.swing.ButtonModel; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JLabel; 
import javax.swing.GroupLayout; 
import javax.swing.ImageIcon; 
import javax.swing.GroupLayout.Alignment; 
import javax.swing.SpringLayout; 
import javax.swing.SwingConstants; 
import javax.swing.JRadioButton; 

public class AutoTalker { 

private static JFrame frame; 
private JTextField txtTalk; 
private JTextField timeTalk; 
public String robotTalk; 
private JButton btnStopTalking; 
private JButton btnAddTalking; 
private JButton btnStartTalking; 
private int yLabel = 266; 
private Runnable autoTalkStart; 


/** 
* Launch the application. 
*/ 


public static void NewFrame() { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       AutoTalker window = new AutoTalker(); 
       window.frame.setVisible(true); 
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
* @return 
*/ 


public AutoTalker() { 
    initialize(); 
} 




/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 



    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 415); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setTitle("AutoTalker"); 
    frame.getContentPane().setLayout(null); 


    JRadioButton rdbtnGlow = new JRadioButton("Glow"); 
    rdbtnGlow.setBounds(6, 113, 64, 23); 
    rdbtnGlow.setActionCommand("glow"); 
    frame.getContentPane().add(rdbtnGlow); 


    JRadioButton rdbtnFlash = new JRadioButton("Flash"); 
    rdbtnFlash.setBounds(182, 113, 69, 23); 
    rdbtnGlow.setActionCommand("flash"); 
    frame.getContentPane().add(rdbtnFlash); 


    JRadioButton rdbtnRed = new JRadioButton("Red"); 
    rdbtnRed.setBounds(364, 113, 64, 23); 
    rdbtnGlow.setActionCommand("red"); 
    frame.getContentPane().add(rdbtnRed); 


    final ButtonGroup rdbtnPressed = new ButtonGroup(); 
    rdbtnPressed.add(rdbtnRed); 
    rdbtnPressed.add(rdbtnFlash); 
    rdbtnPressed.add(rdbtnGlow); 

    txtTalk = new JTextField(); 
    txtTalk.setBounds(130, 28, 173, 20); 
    txtTalk.setColumns(10); 

    btnStopTalking = new JButton("Stop AutoTalk"); 
    btnStopTalking.setBounds(144, 177, 146, 23); 
    btnStopTalking.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      Talk.action = "stop"; 
      btnStartTalking.setEnabled(true); 
      btnStopTalking.setEnabled(false); 
      btnAddTalking.setEnabled(true); 
      frame.repaint(); 

     } 
    }); 





    btnStartTalking = new JButton("Start AutoTalk"); 
    btnStartTalking.setBounds(144, 143, 146, 23); 
    btnStartTalking.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 

      int robotDelay = Integer.parseInt(timeTalk.getText()); 

      Talk.action = "start"; 

      robotTalk = txtTalk.getText(); 

      if (rdbtnRed.isSelected() == true) { 

       robotTalk = "red:" + txtTalk.getText(); 

      } else if (rdbtnFlash.isSelected() == true) { 

       robotTalk = "flash1:" + txtTalk.getText(); 

      } else if (rdbtnGlow.isSelected() == true) { 

       robotTalk = "glow1:" + txtTalk.getText(); 

      } 

      autoTalkStart = new Runnable() { 
       public void run() { 
        try { 

         btnStartTalking.setEnabled(false); 
         btnStopTalking.setEnabled(true); 
         btnAddTalking.setEnabled(false); 
         Talk test = new Talk(robotDelay); 
        } catch (AWTException | InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      }; 
      new Thread(autoTalkStart).start(); 
      frame.repaint(); 




     } 
    }); 

    timeTalk = new JTextField(); 
    timeTalk.setBounds(174, 71, 86, 20); 
    timeTalk.setColumns(10); 

    JLabel lblNewLabel = new JLabel("Text:"); 
    lblNewLabel.setBounds(24, 28, 46, 20); 

    JLabel lblDelayInMs = new JLabel("Delay in ms:"); 
    lblDelayInMs.setBounds(24, 71, 101, 20); 
    frame.getContentPane().add(txtTalk); 
    frame.getContentPane().add(btnStartTalking); 
    frame.getContentPane().add(btnStopTalking); 
    frame.getContentPane().add(timeTalk); 

    btnAddTalking = new JButton("Add"); 
    btnAddTalking.setBounds(144, 211, 146, 23); 
    btnAddTalking.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      if(!txtTalk.getText().equals("")) { 

      JLabel newLabel = new JLabel(txtTalk.getText()); 
      newLabel.addMouseListener(new MouseAdapter() { 

      public void mouseClicked(MouseEvent arg0) { 

        frame.remove(newLabel); 
        frame.repaint(); 

       } 
      });    

      newLabel.setHorizontalAlignment(SwingConstants.CENTER); 

      newLabel.setBounds(159, yLabel, 101, 20); 

      frame.getContentPane().add(newLabel); 

      yLabel = yLabel + 20; 

      frame.getContentPane().setLayout(null); 

      frame.repaint(); 
      Talk.addTalk(txtTalk.getText()); 

     } 
     } 
    }); 

    frame.getContentPane().add(btnAddTalking); 
    frame.getContentPane().add(lblNewLabel); 
    frame.getContentPane().add(lblDelayInMs); 
    btnStopTalking.setEnabled(false); 








} 
} 

類2:

import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.InputEvent; 
import java.awt.event.KeyEvent; 
import java.io.IOException; 
import java.util.ArrayList; 

public class Talk extends AutoTalker { 

Robot robot = new Robot(); 
public static String action; 
public static ArrayList<String> Talks = new ArrayList<String>(); 

public Talk(int wait) throws AWTException, InterruptedException {  

robot.delay(5000); 
while(action == "start") { 
for(int i = 0; i < Talks.size();i++) { 


String text = Talks.get(i); 



    type(text); 
    robot.delay(wait); 



} 
} 




} 

public static void addTalk(String text) { 

Talks.add(text); 

} 


private void type(String s) 

{ 
for (int i = 0; i < s.length(); i++){ 

     char c = s.charAt(i); 
switch(c) { 

case '!': 
robot.keyPress(KeyEvent.VK_SHIFT); 
    robot.keyPress(KeyEvent.VK_1); 
    robot.keyRelease(KeyEvent.VK_SHIFT); 
    robot.keyRelease(KeyEvent.VK_1); 



break; 
case ':': 
robot.keyPress(KeyEvent.VK_SHIFT); 
robot.keyPress(KeyEvent.VK_SEMICOLON); 
robot.keyRelease(KeyEvent.VK_SEMICOLON); 
robot.keyRelease(KeyEvent.VK_SHIFT); 





break; 
default:   

robot.keyPress(Character.toUpperCase(c)); 


break; 




} 
robot.delay(10); 

} 

robot.keyPress(KeyEvent.VK_ENTER); 
robot.keyRelease(KeyEvent.VK_ENTER); 
robot.delay(10); 


} 



} 
+0

您在問題文本中提到了JavaFX,但這不是JavaFX代碼。另外,不建議使用'com.sun.glass'類,因爲它們不是公開導出的JRE API的一部分,它們的接口可能會在未來的Java版本中更改或刪除。 – jewelsea

+0

感謝您的回覆。我認爲這是JavaFX代碼,但我可能是錯的:)。你知道我究竟做錯了什麼嗎? –

+0

我不是Swing開發的專家,所以我會保留回答這個問題。我編輯了您的問題以刪除JavaFX引用並添加一個Swing標籤,這可能會幫助您選擇願意查看它的Swing專家。如果你提供了一個[mcve](http://stackoverflow.com/help/mcve)(注意它必須是* minimal *和* complete *),你可能會得到更好的迴應機會,而不是傾銷你的整個代碼。它可以編譯並執行以運行)。此外,用正確的縮進格式化代碼並刪除無用的空格。 – jewelsea

回答

0

改用

invalidate(); 
validate();