السلام عليكم
أود السؤال عن إمكانية عرض الآوت بت لأى ثريد فى JFrame
وشكراُ
السلام عليكم
أود السؤال عن إمكانية عرض الآوت بت لأى ثريد فى JFrame
وشكراُ
يمكنك إضافة حقلTextField لل JFrame و بعدين تعرض الأوت بت بعدين
سوف اضع مثال على ذلك ولكن في وقت لا حق
أنا فى إنتظار أمثلتك
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_GUImore over ... in the programm there are few bugs .. try to fix it
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
هذا الموضوع مغلق.