调用`setText()`时JTextfield不会更新

时间:2015-10-27 10:43:37

标签: java swing jframe jtextfield midi

这是一个音乐播放器。它工作正常,但当我尝试创建一个Jframe我失败。

我有3个jTextfields,它应该呈现我的游戏时间和剩余的游戏时间。但是在程序中他们没有更新。

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
//
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequencer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.*;
import javax.swing.event.*;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 26.10.2015
  * @author 
  */

public class test extends JFrame  {
  // Anfang Attribute
  private static File chossen = new File("");
  private static int x=1;

  private static JTextField jTextField1 = new JTextField();
  private static JTextField jTextField2 = new JTextField();
  private static JTextField jTextField3 = new JTextField();
  private JLabel jLabel1 = new JLabel();
  private JLabel jLabel2 = new JLabel();
  private JLabel jLabel3 = new JLabel();
  private static JProgressBar jProgressBar1 = new JProgressBar();
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  // Ende Attribute

  public test(String title) throws MidiUnavailableException { 
    // Frame-Initialisierung

    super(title);

    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 574; 
    int frameHeight = 412;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);
    // Anfang Komponenten

    jTextField1.setBounds(400, 8, 150, 20);
    cp.add(jTextField1);
    jTextField2.setBounds(400, 32, 150, 20);
    cp.add(jTextField2);
    jTextField3.setBounds(400, 56, 150, 20);
    cp.add(jTextField3);
    jLabel1.setBounds(288, 8, 110, 20);
    jLabel1.setText("Gesamte Dauer");
    cp.add(jLabel1);
    jLabel2.setBounds(288, 32, 110, 20);
    jLabel2.setText("Aktuelle Dauer");
    cp.add(jLabel2);
    jLabel3.setBounds(288, 56, 110, 20);
    jLabel3.setText("Verbleibend");
    cp.add(jLabel3);
    jProgressBar1.setBounds(280, 88, 270, 16);
    cp.add(jProgressBar1);
    jButton1.setBounds(480, 344, 75, 25);
    jButton1.setText("Start");
    jButton1.setMargin(new Insets(2, 2, 2, 2));
    jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        try {
            jButton1_ActionPerformed(evt);
        } catch (MidiUnavailableException | InvalidMidiDataException |                      IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
    });
    cp.add(jButton1);
    jButton2.setBounds(400, 344, 75, 25);
    jButton2.setText("Stop");
    jButton2.setMargin(new Insets(2, 2, 2, 2));
    jButton2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        try {
            jButton2_ActionPerformed(evt);
        } catch (MidiUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidMidiDataException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
    });
    cp.add(jButton2);
    jButton3.setBounds(8, 8, 267, 73);
    jButton3.setText("Dateiauswahl");
    jButton3.setMargin(new Insets(2, 2, 2, 2));
    jButton3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton3_ActionPerformed(evt);
      }
    });
    cp.add(jButton3);
    // Ende Komponenten

    setVisible(true);
  } // end of public test

  // Anfang Methoden

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

    new test("test");

  } // end of main

  public void test(int zahl) throws MidiUnavailableException,     InvalidMidiDataException, IOException, InterruptedException{
      final Sequencer sequencer = MidiSystem.getSequencer();
      sequencer.open();
      if(zahl==2){
          sequencer.setSequence(MidiSystem.getSequence(chossen));


            sequencer.setTempoFactor(1);
            sequencer.start();
            System.out.println("start");
            Thread.sleep(5000);
            System.out.println("tettstdt");

            x=1;
            while(x==1){
                long z = sequencer.getMicrosecondLength()/1000/1000;
                long y=sequencer.getMicrosecondPosition()/1000/1000;
                jProgressBar1.setMinimum(0);
                jProgressBar1.setMaximum((int) z);
                jTextField1.setText(z+"s");


                z = sequencer.getMicrosecondLength()/1000/1000;
                y=sequencer.getMicrosecondPosition()/1000/1000; 
                System.out.println(z+" "+y);
                jProgressBar1.setValue((int) y);
                jTextField2.setText(y+"s");
                jTextField3.setText("s");
                if(z-y==0){
                    x=0;
                }


            }    
      }else if(zahl==3){
          System.out.println("stop fehlerhaft");
          sequencer.stop();
      }else{
          System.out.println("fehler");
      }


  }    


  public void jButton1_ActionPerformed(ActionEvent evt) throws MidiUnavailableException, InvalidMidiDataException, IOException, InterruptedException {
      System.out.println(chossen);


      test(2);






  } 

  public void jButton2_ActionPerformed(ActionEvent evt) throws     MidiUnavailableException, InvalidMidiDataException, IOException {
     // test(3);

      }     
  public void jButton3_ActionPerformed(ActionEvent evt) {
      File chossen2 = new File("C:/Users/Eric/workspace/Komplex/midi/"+    (dic()));
      chossen = chossen2;
  } 



  public static String dic() {

    File f = new File("C:/Users/Eric/workspace/Komplex/midi");
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(f);


    int state = fc.showOpenDialog( null );

    if ( state == JFileChooser.APPROVE_OPTION )
    {    
      File file = fc.getSelectedFile();

      return file.getName();
    }
    else

    return "";


  }    
}

当我将信息打印到控制台时,它可以工作。

1 个答案:

答案 0 :(得分:-2)

调用repaint()方法强制JTextField使用当前选项集重新绘制自己。