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

أدخل عددين فى نافذه رسوميه واحده

بدأه دنية القوى في 19 مايو 2011 · 37 رد · 1,760 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#26

:)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication23;

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

public class TextFieldDemo extends JFrame
  implements ActionListener {
  private JTextField jtfCelsius = new JTextField(10);
  private JTextField jtfFahrenheit = new JTextField(10);
  private JButton button1;
  private JButton button2 ;
  private JLabel cel;
  private JLabel feh;

  /** Main method */
  public static void main(String[] args) {
    TextFieldDemo frame = new TextFieldDemo();
    frame.pack();
    frame.setTitle("TextFieldDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  public TextFieldDemo() {
          button1= new JButton("convert");
      button2= new JButton("clear");

    // Panel p1 to hold labels
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(2, 2));
     p1.add(button1);
     p1.add(new JLabel("Celsius"));
     p1.add(button2);

    p1.add(new JLabel("Fahrenheit"));


    // Panel p2 to hold text fields
    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(2, 1));
    p2.add(jtfCelsius);
    p2.add(jtfFahrenheit);


    // Add p1 and p3 to the frame
    getContentPane().add(p1, BorderLayout.WEST);
    getContentPane().add(p2, BorderLayout.CENTER);

    // Set horizontal alignment to RIGHT for text fields
    jtfCelsius.setHorizontalAlignment(JTextField.RIGHT);
    jtfFahrenheit.setHorizontalAlignment(JTextField.RIGHT);

    // Register listener
   button1.addActionListener(this);
   button2.addActionListener(this);
  }

  /** Handle ActionEvent */
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button1) {
     int celsius =
        (int) Double.parseDouble(jtfCelsius.getText().trim());
      int fahrenheit = ((int)(9 / 5) * celsius + 32);
      jtfFahrenheit.setText(new Double(fahrenheit).toString());
      jtfFahrenheit.requestFocusInWindow();
    }
 else{
    jtfCelsius.setText("");
    jtfFahrenheit.setText("");
 }
  }
}

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#27

مشكور الله يعطيك العافيه تعبتك معى بس ممكن طلب أخير أبقى يكون الا أزار تحت الخانت الادخال

#28

تفضلي

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication23;

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

public class TextFieldDemo extends JFrame
        implements ActionListener {

    private JTextField jtfCelsius = new JTextField(10);
    private JTextField jtfFahrenheit = new JTextField(10);
    private JButton button1;
    private JButton button2;
    private JLabel cel;
    private JLabel feh;

    /** Main method */
    public static void main(String[] args) {
        TextFieldDemo frame = new TextFieldDemo();
        frame.pack();
        frame.setTitle("TextFieldDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public TextFieldDemo() {
        button1 = new JButton("convert");
        button2 = new JButton("clear");

        // Panel p1 to hold labels
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(2, 1));

        p1.add(new JLabel("Celsius"));



        p1.add(new JLabel("Fahrenheit"));


        // Panel p2 to hold text fields
        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(2, 1));
        p2.add(jtfCelsius);
        p2.add(jtfFahrenheit);

        JPanel p3 = new JPanel();
        p3.add(button1);
        p3.add(button2);
        p3.setLayout(new GridLayout(1, 2));

        // Add p1 and p3 to the frame
        getContentPane().add(p1, BorderLayout.WEST);
        getContentPane().add(p2, BorderLayout.EAST);
        getContentPane().add(p3, BorderLayout.SOUTH);
        // Set horizontal alignment to RIGHT for text fields
        jtfCelsius.setHorizontalAlignment(JTextField.RIGHT);
        jtfFahrenheit.setHorizontalAlignment(JTextField.RIGHT);

        // Register listener
        button1.addActionListener(this);
        button2.addActionListener(this);
    }

    /** Handle ActionEvent */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button1) {
            int celsius =
                    (int) Double.parseDouble(jtfCelsius.getText().trim());
            int fahrenheit = ((int) (9 / 5) * celsius + 32);
            jtfFahrenheit.setText(new Double(fahrenheit).toString());
            jtfFahrenheit.requestFocusInWindow();
        } else {
            jtfCelsius.setText("");
            jtfFahrenheit.setText("");
        }
    }
}

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#29

مشكور جزاك الله ألف خير عساك بالجنه الفردوس مع النبياء ولصالحين

بس أخر شى كيف يكون الرقم المحول عدد صحيح من غير فاصله وكيف أضع أول شى الخانه الفرقه ثم أسمها

#30

اعملي casting الى int

x= (int) (......);

لم افهم ما قصدك بالفرقه

:)

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#31

يعنى خانه الدخل تكون فى يسار الشاشه المخرجات ثم الاسم

#32

ها غير في الاستدعاءات

getContentPane().add(p1, BorderLayout.WEST);
        getContentPane().add(p2, BorderLayout.EAST);
        getContentPane().add(p3, BorderLayout.SOUTH);

اقلبي السطر الثاني مع الاول يعني خلي الاول BorderLayout.EAST

والثاني BorderLayout.WEST

بس لا تغيري الp1 او اي حاجه ثانية

تم تعديل هذه المشاركة بواسطة mental-driller في 23 مايو 2011 في 00:42

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#33

الحمد الله أشتغل تمام بس لسع يعطنى أعدد عشريه منى عارفه وين أضع التعريف العدد الصحيح

#34

غيري هذا السطر

jtfFahrenheit.setText(new Double(fahrenheit).toString());

لهذا

jtfFahrenheit.setText(new Integer(fahrenheit).toString());

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#35

:ty:

#36

يوجد خطأ فى البرنامج وهوى عمليه التحويل الما أدخل الرقم 100 يحوله الى 132

وهذى خطأ المفروض يحوله الى 212 هذى صحيح وهذى المثلEX5CS105.jar

وأذا ما أدخل شىْ راح يحسبه كانه أدخل صفر وعندى عمليه التحويل يخرج الناتج 0.32 يجيث يكون التعريف double

وهذى البرنامج يوجد به أخطأ أريد أن يطبق مثل المخرجات فى الا أعلى

package javaapplication31; 

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

/** 
 * 
 * @author PSYCHO_ MANTIS 
 */ 
public class Main extends JFrame { 

    private JButton button1; 
    private JButton button2 ; 
    private JTextField field1; 
    private JTextField field2;
    private JLabel cel; 
         private JLabel feh;  


    public Main() { 
        button1= new JButton("convert"); 
        button2= new JButton("clear"); 

        button1.addActionListener(new ActionListener() { 

         try { public void actionPerformed(ActionEvent e) { 
                    double x,y; 
                    x = Integer.parseInt(field1.getText()); 
                    field2.setText(Integer.toString( ((5.0/9 *(x -32))))); 

            }

        }); } catch(InterruptedException e) {}

X=0;
   field2.setText(Integer.toString( ""+((5.0/9 *(x -32))))); 

        button2.addActionListener(new ActionListener() { 

            public void actionPerformed(ActionEvent e) { 
                field1.setText(""); 
                field2.setText(""); 
            } 
        }); 
       field1 = new JTextField(); 
       field2 = new JTextField(); 

       field1.setColumns(10); 
       field2.setColumns(10); 


       setLayout(new FlowLayout()); 
       add(field1); 
       add(field2); 
      add(button1); 
      add(button2); 
       setVisible(true); 
       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       setSize(300,200); 
       setResizable(false);
// Add p1 and p3 to the frame 
        getContentPane().add(p1, BorderLayout. EAST);
        getContentPane().add(p1, BorderLayout. WEST); 
        getContentPane().add(p1, BorderLayout.SOUTH); 
        // Set horizontal alignment to RIGHT for text fields 
        jtfCelsius.setHorizontalAlignment(JtextField1.RIGHT); 
        jtfFahrenheit.setHorizontalAlignment(JtextField2.RIGHT); 
    } 

    /** 
     * @param args the command line arguments 
     */ 
    public static void main(String[] args) { 
        Main app = new Main(); 



    } 
}

وشكرا.

#37

اعتذر ، ربما لن استطيع دخول المنتدى لفتره بسبب ضروف خارجة عن ارادتي

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#38

بسم الله الرحمن الرحيم

الاخت السائلة في الموضوع اذا كان الهدف من الاسئلة هو عمل برنامج لتحويل بين درجات الحرارة فانا عملت مثال متواضع يوضح لك كيفية استخدام الخصائص الخاصة ب JFrame

وان شاء الله يكون عون لك الكود في المرفقات

Converter.java

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