| |

VerySource

 Forgot password?
 Register
Search
View: 1465|Reply: 5

Jtextfield display problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-4-22 17:00:01
| Show all posts |Read mode
Please help me see the following code When the number of TEXT in the time of the time, the number in the text is not changed.
Will it be a problem with a scope? If so, how should it be changed to change after the point is determined? Thank you!
Import javax.swing. *;
Import java.awt. *;
Import java.awt.event. *;
Public Class Text Extends Jframe Implements ActionListener
{JBUTTON TB = New JButton ("Start");
INT A = 0;
    Jtextfield text = new jtextfield (", 10);
Jtextfield text1 = new jtextfield ("5", 10);
Text ()
{
Super (Text Test ");
Setsize (100, 150);
SetLocation (200, 300);
GetContentPane (). setLayout (New flowLayout ());
GetContentPane (). add (text);
GetContentPane (). add (tb);
getContentPane (). Add (text1);
Tb.addActionListener (this);
Pack ();
SetDefaultCloseOperation (jframe.exit_on_close);
SetVisible (TRUE);
Begin ();
}
Public void begin ()
{
INT i = 0;
       A = integer.parseint (Text1.getText (). Trim ());
While (a> 0)
{
A -; i = 50;
While (i> 0)
{
I-;
System.out.print ("+ i);
Text.setText ("+ i);
Try {
Thread.sleep (50);
     } catch (Exception E)
{}
}
}
}
Public Void ActionPerformed (ActionEvent EE)
{
IF (EE.GETSOURCE () == TB)
{begin ();

       }
  Public static void main (String SRGS [])
  {new text ();
  }
}
Reply

Use magic Report

0

Threads

23

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2021-4-23 10:45:01
| Show all posts
The second is a thread blocking problem. You stop the main thread, so you can't show it. In order to make you have a lot of changes, I use this method to modify:
Import javax.swing. *;
Import java.awt. *;
Import java.awt.event. *;

Public class text extends JFrame IMPLEments ActionListener, runnable {// adds a thread interface.
  JButton TB = New JButton ("Start");
  INT A = 0;
  Jtextfield text = new jtextfield (", 10);
  Jtextfield text1 = new jtextfield ("5", 10);
  // Thread TH = New Thread (this);
  TEXT () {
    Super (Text Test ");
    Setsize (100, 150);
    SETLOCATION (200, 300);
    GetContentPane (). setLayout (New flowLayout ());
    GetContentPane (). add (text);
    GetContentPane (). add (tb);
    getContentPane (). Add (text1);
    Tb.addActionListener (this);
    Pack ();
    SetDefaultCloseOperation (jframe.exit_on_close);
    SetVisible (TRUE);
    TEXT.SETENABLED (FALSE);
    Run (); // ------------------> It is equivalent to executing your Begin method.
  }

  Public void run () {// -------> BeGin is changed to RUN, which is for overload.
    INT i = 0;
    A = integer.parseint (Text1.getText (). Trim ());
    While (a> 0) {
      A-;
      i = 50;
      While (i> 0) {
        I-;
        System.out.print ("+ i);
        Text.setText ("+ i);
        Try {
          Thread.sleep (50);
        }
        Catch (Exception E) {}
      }
    }
  }

  Public Void ActionPerformed (ActionEvent EE) {

     New thread (this) .start (); // ---> Mainly here, create new threads, this will not affect the main thread


  }

  Public static void main (String SRGS []) {
    NEW text ();
  }
}
Reply

Use magic Report

0

Threads

23

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2021-4-23 11:00:01
| Show all posts
The error should be aware that you don't jump like your button, indicating that it is the main thread.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2021-4-23 12:30:01
| Show all posts
Thank you, I will then change it later. I don't understand what it means?
Reply

Use magic Report

0

Threads

1

Posts

0.00

Credits

Newbie

Rank: 1

Credits
0.00

 Great Britain

Post time: 2021-7-25 08:55:39
| Show all posts
sindbad 2021-4-23 11:00
The error should be aware that you don't jump like your button, indicating that it is the main threa ...

ok thanks
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-8-4 11:16:15
| Show all posts
Thank you, thank you, I will also change it later.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list