الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Thread with GUI

مغلق
بدأه السكب في 13 أبريل 2004 · 4 رد · 739 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

أود السؤال عن إمكانية عرض الآوت بت لأى ثريد فى JFrame

وشكراُ

#2

يمكنك إضافة حقلTextField لل JFrame و بعدين تعرض الأوت بت بعدين

سوف اضع مثال على ذلك ولكن في وقت لا حق

#3

أنا فى إنتظار أمثلتك

#4

after writing the code clike inside the frame ..

or change its size ..

then clike on the button ...

you can change the Text by type the new text in the field ...

i hope the code below is what you asking for ...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;

public class MyThread extends JFrame {

  private JButton button;
  private JTextField field;
  private String string;
  private JLabel label;
  private Point point;

  public MyThread()
  {
  getContentPane().setLayout(new FlowLayout());

         // button
    button = new JButton("Move");
    button.addActionListener(//S1
        new ActionListener(){//S2
           public void actionPerformed (ActionEvent event)
            {move(field.getText());}
        } //E2
        );//E1
    getContentPane().add(button);

        //field
   field = new JTextField("Enter the text hear");

   getContentPane().add(field);


        // label
  label = new JLabel("OK");
  getContentPane().add(label);

       //Point
  point = new Point(0,50);
  setSize(200,100);
  setVisible(true);

  //repaint();
  }

  // paint the label------------------------------------------------------------
  public void paint(Graphics g)
  {
    label.setLocation(point);
  }// end method paint

  // move-----------------------------------------------------------------------
  private void move(String string)
  {
    label.setText(string);
    Thread thrd =new Thread(new Runnable(){
              public void run(){
                while (true)
                {

                  if((point.x + getContentPane().getWidth()/8) > getContentPane().getWidth()) {point.x = 0;}
                  point.x++;
                  repaint();
                  try{
                  Thread.sleep(10);
                  }catch(InterruptedException exception){exception.printStackTrace();}
                }
              }
          });

          thrd.start();

  }

// the main method------------------------------------------------------------
  public static void main(String[] args) {

    MyThread t1 = new MyThread(); // <- 1st string

  }//end the main method

}// end class Thread_GUI

more over ... in the programm there are few bugs .. try to fix it

#5

to fix the bugs

try to replace paint method and move method with those new methods..

but first of all ... add the following variable to the code

  private boolean start = true;

and change the methods

 // paint the label------------------------------------------------------------
  public void paint(Graphics g)
  {
    label.setLocation(point);
    field.setSize(150,20);
  }// end method paint

  // move-----------------------------------------------------------------------
  private void move(String string)
  {
    label.setText(string);

    System.out.println("\nfield.getWidth()="+field.getWidth());

    if(start){ start = false;
    Thread thrd =new Thread(new Runnable(){
              public void run(){
                while (true)
                {
                  if((point.x + getContentPane().getWidth()/8) > getContentPane().getWidth()) {point.x = 0;}
                  point.x++;
                  repaint();
                  try{
                  Thread.sleep(10);
                  }catch(InterruptedException exception){exception.printStackTrace();}
                }
              }
          });

          thrd.start();

  }}// end method move

تم تعديل هذه المشاركة بواسطة المبرمج1 في 18 أبريل 2004 في 14:41

هذا الموضوع مغلق.

مواضيع مشابهة