۱۳۸۷ بهمن ۲۶, شنبه

نرم افزار تبدیل اینچ به سانتی متر

package Intocm;


import java.awt.*;
import javax.swing.*;
import java.awt.event.*; // Needed for ActionListener

//////////////////////////////////////////////////////// class Intocm

class Intocm extends JFrame {
//==================================== constants

private static final double CM = 2.54; //Note 1

//===================== instance variables
private JTextField inch = new JTextField(7); //Note 2

private JTextField cm = new JTextField(7);

//============================= constructor
public Intocm() { //Note 3
// 1... Create/initialize components

JButton convertBtn = new JButton("Convert");

convertBtn.addActionListener(new ConvertBtnListener());

cm.addActionListener(new ConvertBtnListener());
inch.setEditable(false);


// 2... Create content panel, set layout
JPanel content = new JPanel();
content.setLayout(new FlowLayout());

// 3... Add the components to the content panel.
content.add(new JLabel("inch:"));
content.add(cm); // Add input field

content.add(convertBtn); // Add button

content.add(new JLabel("cm:"));
content.add(inch); // Add output field

// 4... Set this window's attributes, and pack it.
setContentPane(content);
pack(); // Layout components.

setTitle("In to Cm");
setSize(400, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center window.

}

////////////////////////////////////////////////// ConvertBtnListener
class ConvertBtnListener implements ActionListener { //Note 6


public void actionPerformed(ActionEvent e) {
//... Get the value from the dog years textfield.
String cmStr = cm.getText();
//int cmInt = Integer.parseInt(cmStr);

double cmd = Double.parseDouble(cmStr);

double inchValue = cmd * CM; //Note 9

//... Convert to string and set human yrs textfield
inch.setText("" + inchValue);

}
}

//=============================== method main
public static void main(String[] args) {
Intocm window = new Intocm();
window.setVisible(true);
}
}

0
این نرم افزار را با استفاده از کد های اینجا و با مساعدت سرکار خانم شایسته علوی نوشتم!

۱۳۸۷ بهمن ۱۳, یکشنبه

چگونه از یک فایل بخوانیم یا بر روی یک فایل بنویسیم!

با دو برنامه زیر می توانید بر روی یک فایل یک متن بنویسید و دوباره آن را بخوانید!

import java.io.*;

public class MyFirstFileWritingApp
{
// Main method
public static void main (String args[])
{
// Stream to write file
FileOutputStream fout;

try
{
// Open an output stream
fout = new FileOutputStream ("myfile.txt");

// Print a line of text
new PrintStream(fout).println ("hello world!");

// Close our output stream
fout.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}


}
0
این برنامه ساده یک فایل متنی میسازد و یک خط از متن روی آن مینویسد!

برای خواندن از روی یک فایل نیز از برنامه زیر استفاده کنید

import java.io.*;

public class MyFirstFileReadingApp
{
// Main method
public static void main (String args[])
{
// Stream to read file
FileInputStream fin;

try
{
// Open an input stream
fin = new FileInputStream ("myfile.txt");

// Read a line of text
System.out.println( new DataInputStream(fin).readLine() );

// Close our input stream
fin.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
}
}
0