۱۳۸۸ شهریور ۶, جمعه

اضافه کردن لوگو

با اضافه کردن کد زیر در فایل manifest.mf پس از اجرای برنامه چند لحظه عکس nacl که در مسیر
src/images کپی شده است به نمایش در می آید.
SplashScreen-Image: images/nacl.jpg

۱۳۸۸ تیر ۱۲, جمعه

تبدیل سانتی گراد به فارنهایت

این نرم افزار رو فارسی کرده وتغییرات کوچکی در آن ایجاد شده!

/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
* CelsiusConverterGUI.java
*
*/

package learn;

import java.awt.event.ActionEvent;

public class CelsiusConverterGUI extends javax.swing.JFrame {

/** Creates new form CelsiusConverterGUI */
public CelsiusConverterGUI() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// //GEN-BEGIN:initComponents
private void initComponents() {
tempTextField = new javax.swing.JTextField();
celsiusLabel = new javax.swing.JLabel();
convertButton = new javax.swing.JButton();
fahrenheitLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("مبدل سانتی گراد");
setLocationRelativeTo(null);
celsiusLabel.setFont(new java.awt.Font("Tahoma", 0, 13));
celsiusLabel.setText("سانتی گراد");

convertButton.setFont(new java.awt.Font("Tahoma", 0, 13));
convertButton.setText("تبدیل");

convertButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButtonActionPerformed(evt);

}
});

tempTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tempTextFieldActionPerformed(evt);

}

private void tempTextFieldActionPerformed(ActionEvent evt) {
int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
* 1.8 + 32);
fahrenheitLabel.setText(tempFahr + " فارنهایت");
}
});

fahrenheitLabel.setFont(new java.awt.Font("Tahoma", 0, 13));
fahrenheitLabel.setText("فارنهایت");


javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(celsiusLabel))
.addGroup(layout.createSequentialGroup()
.addComponent(convertButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fahrenheitLabel)))
.addContainerGap(27, Short.MAX_VALUE))
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertButton, tempTextField});

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(celsiusLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(convertButton)
.addComponent(fahrenheitLabel))
.addContainerGap(21, Short.MAX_VALUE))
);
pack();
}//
//GEN-END:initComponents

private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed
//Parse degrees Celsius as a double and convert to Fahrenheit
int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
* 1.8 + 32);
fahrenheitLabel.setText(tempFahr + " فارنهایت");
}//GEN-LAST:event_convertButtonActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CelsiusConverterGUI().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel celsiusLabel;
private javax.swing.JButton convertButton;
private javax.swing.JLabel fahrenheitLabel;
private javax.swing.JTextField tempTextField;
// End of variables declaration//GEN-END:variables

}

0

۱۳۸۸ اردیبهشت ۱۹, شنبه

شیمی بازی

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

کلاس اول:

package com.toy.anagrams.ui;

import com.toy.anagrams.lib.ChemLibrary;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JFrame;

/**
* Main window of the Anagram Game application.
*/
public class Chembazi extends JFrame {

public static void main(String[] args) {
new Chembazi().setVisible(true);
}

private int wordIdx = 0;
private ChemLibrary wordLibrary;

/** Creates new form Anagrams */
public Chembazi() {
wordLibrary = ChemLibrary.getDefault();

initComponents();
getRootPane().setDefaultButton(guessButton);
scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
pack();
guessedWord.requestFocusInWindow();
// Center in the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation(new Point((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.width) / 2));
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

mainPanel = new javax.swing.JPanel();
scrambledLabel = new javax.swing.JLabel();
scrambledWord = new javax.swing.JTextField();
guessLabel = new javax.swing.JLabel();
guessedWord = new javax.swing.JTextField();
feedbackLabel = new javax.swing.JLabel();
buttonsPanel = new javax.swing.JPanel();
guessButton = new javax.swing.JButton();
nextTrial = new javax.swing.JButton();
mainMenu = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
aboutMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();

setTitle("شیمی بازی");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
mainPanel.setLayout(new java.awt.GridBagLayout());

scrambledLabel.setText("نام ترکیب");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
mainPanel.add(scrambledLabel, gridBagConstraints);

scrambledWord.setColumns(20);
scrambledWord.setEditable(false);
scrambledWord.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
mainPanel.add(scrambledWord, gridBagConstraints);

guessLabel.setDisplayedMnemonic('Y');
guessLabel.setLabelFor(guessedWord);
guessLabel.setText("فرمول ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
mainPanel.add(guessLabel, gridBagConstraints);

guessedWord.setColumns(20);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
mainPanel.add(guessedWord, gridBagConstraints);

feedbackLabel.setText(" ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
mainPanel.add(feedbackLabel, gridBagConstraints);

buttonsPanel.setLayout(new java.awt.GridBagLayout());

guessButton.setMnemonic('G');
guessButton.setText("حدس");
guessButton.setToolTipText("Guess the scrambled word.");
guessButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
guessedWordActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
buttonsPanel.add(guessButton, gridBagConstraints);

nextTrial.setMnemonic('N');
nextTrial.setText("ترکیب جدید");
nextTrial.setToolTipText("Fetch a new word.");
nextTrial.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextTrialActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
gridBagConstraints.weighty = 1.0;
buttonsPanel.add(nextTrial, gridBagConstraints);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
mainPanel.add(buttonsPanel, gridBagConstraints);

getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);

mainMenu.setFont(new java.awt.Font("AlArabiya", 0, 13)); // NOI18N

fileMenu.setMnemonic('F');
fileMenu.setText("فایل");

aboutMenuItem.setMnemonic('A');
aboutMenuItem.setText("درباره");
aboutMenuItem.setToolTipText("About");
aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutMenuItemActionPerformed(evt);
}
});
fileMenu.add(aboutMenuItem);

exitMenuItem.setMnemonic('E');
exitMenuItem.setText("خروج");
exitMenuItem.setToolTipText("Quit Team, Quit!");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});
fileMenu.add(exitMenuItem);

mainMenu.add(fileMenu);

setJMenuBar(mainMenu);
}//


private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
new About(this).setVisible(true);
}

private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {
wordIdx = (wordIdx + 1) % wordLibrary.getSize();

feedbackLabel.setText(" ");
scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
guessedWord.setText("");
getRootPane().setDefaultButton(guessButton);

guessedWord.requestFocusInWindow();
}

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {
if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
feedbackLabel.setText("درست است، بعدی!");
getRootPane().setDefaultButton(nextTrial);
} else {
feedbackLabel.setText("غلط! دوباره سعی کن!");
guessedWord.setText("");
}

guessedWord.requestFocusInWindow();
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

// Variables declaration - do not modify
private javax.swing.JMenuItem aboutMenuItem;
private javax.swing.JPanel buttonsPanel;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JLabel feedbackLabel;
private javax.swing.JMenu fileMenu;
private javax.swing.JButton guessButton;
private javax.swing.JLabel guessLabel;
private javax.swing.JTextField guessedWord;
private javax.swing.JMenuBar mainMenu;
private javax.swing.JPanel mainPanel;
private javax.swing.JButton nextTrial;
private javax.swing.JLabel scrambledLabel;
private javax.swing.JTextField scrambledWord;
// End of variables declaration

}
۰

کلاس دوم:

package com.toy.anagrams.lib;

/**
* Implementation of the logic for the Anagram Game application.
*/
final class StaticChemLibrary extends ChemLibrary {

private static final String[] FORMULA = {
"Na2SO4",
"K2CO3",
"LiNO3",
"CaCl2",
"Ba3(PO4)2",
"MgF2",
"Al(NO2)3",
"BeCl2",
"ZnCl2",
"SO3",
"SO2",
"CO2",
"CO",
"CCl4",
"NCl3",
"NO",
"NO2",
"N2O3",
"N2O4",
"N2O5",
"Al2(CO3)3",
"PBr3",
"PBr5",
"PbO2",
"KI",
"H2SO4",
"HCl",
"HNO3",
"H3PO4",
"H2CO3",
"H2O2",
"Fe(OH)3",
"NiSO4",
"CS2",
"HNO2",
"H2SO3",
"HClO",
"HClO2",
"HClO3",
"HClO4",
"CuCl",
"CuCl2",
"AgNO3",
"NaHCO3",
"Cr2O3"};

private static final String[] PERSIAN_NAME = {
"سدیم سولفات",
"پتاسیم کربنات",
"لیتیم نیترات",
"کلسیم کلرید",
"باریم فسفات",
"منیزیم فلورید",
"آلومینیم نیتریت",
"برلیم کلرید",
"روی کلرید",
"گوگرد تری اکسید",
"گوگرد دی اکسید",
"کربن دی اکسید",
"کربن منو اکسید",
"کربن تترا کلرید",
"نیتروژن تری کلرید",
"نیتروژن منو اکسید",
"نیتروژن دی اکسید",
"دی نیتروژن تری اکسید",
"دی نیتروژن تترا اکسید",
"دی نیتروژن پنتا اکسید",
"آلومینیم کربنات",
"فسفر تری برمید",
"فسفر پنتا برمید",
"اکسید (IV) سرب",
"پتاسیم یدید",
"سولفوریک اسید",
"هیدرو کلریک اسید",
"نیتریک اسید",
"فسفریک اسید",
"کربنیک اسید",
"هیدروژن پر اکسید",
"هیدرواکسید (III) آهن",
"سولفات (II) نیکل ",
"کربن دی سولفید",
"نیترو اسید",
"سولفورو اسید",
"هیپوکلرو اسید",
"کلرو اسید",
"کلریک اسید",
"پر کلریک اسید",
"کلرید (I) مس",
"کلرید (II) مس",
"نقره نیترات",
"سدیم هیدروژن کربنات",
"اکسید (III) کروم"
};

final static ChemLibrary DEFAULT = new StaticChemLibrary();

/**
* Singleton class.
*/
private StaticChemLibrary() {
}

/**
* Gets the word at a given index.
* @param idx index of required word
* @return word at that index in its natural form
*/
public String getWord(int idx) {
return FORMULA[idx];
}

/**
* Gets the word at a given index in its scrambled form.
* @param idx index of required word
* @return word at that index in its scrambled form
*/
public String getScrambledWord(int idx) {
return PERSIAN_NAME[idx];
}

/**
* Gets the number of words in the library.
* @return the total number of plain/scrambled word pairs in the library
*/
public int getSize() {
return FORMULA.length;
}

/**
* Checks whether a user's guess for a word at the given index is correct.
* @param idx index of the word guessed
* @param userGuess the user's guess for the actual word
* @return true if the guess was correct; false otherwise
*/
public boolean isCorrect(int idx, String userGuess) {
return userGuess.equals(getWord(idx));
}

}
۰

کلاس سوم:



package com.toy.anagrams.lib;

/**
* Interface defining logic for the Anagram Game application.
*/
public abstract class ChemLibrary {
/**
* Constructor for subclasses.
*/
protected ChemLibrary() {
}

/** Getter for the default implementation of the WordLibrary.
* @return some default implementation of WordLibrary
*/
public static ChemLibrary getDefault() {
return StaticChemLibrary.DEFAULT;
}

/**
* Gets the word at a given index.
* @param idx index of required word
* @return word at that index in its natural form
*/
public abstract String getWord(int idx);

/**
* Gets the word at a given index in its scrambled form.
* @param idx index of required word
* @return word at that index in its scrambled form
*/
public abstract String getScrambledWord(int idx);

/**
* Gets the number of words in the library.
* @return the total number of plain/scrambled word pairs in the library
*/
public abstract int getSize();

/**
* Checks whether a user's guess for a word at the given index is correct.
* @param idx index of the word guessed
* @param userGuess the user's guess for the actual word
* @return true if the guess was correct; false otherwise
*/
public abstract boolean isCorrect(int idx, String userGuess);
}
۰

کلاس چهارم:





package com.toy.anagrams.lib;

import java.util.Arrays;
import junit.framework.TestCase;

/**
* Test of the functionality of {@link WordLibrary}.
*/
public class ChemLibraryTest extends TestCase {
ChemLibrary wordLibrary;

public ChemLibraryTest(String testName) {
super(testName);
}

protected void setUp() throws Exception {
wordLibrary = ChemLibrary.getDefault();
}



/**
* Test of {@link WordLibrary#isCorrect}.
*/
public void testIsCorrect() {
for (int i = 0; i < clearword =" wordLibrary.getWord(i);" scrambledword =" wordLibrary.getScrambledWord(i);" cleararray =" clearWord.toCharArray();" scrambledarray =" scrambledWord.toCharArray();">
کلاس پنجم:


package com.toy.anagrams.ui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
* About dialog of the Anagram Game application.
*/
public class About extends JDialog {

/** Creates new form About */
public About(JFrame parent) {
super(parent,true);
initComponents();
pack();
Rectangle parentBounds = parent.getBounds();
Dimension size = getSize();
// Center in the parent
int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
setLocation(new Point(x, y));
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

mainPanel = new javax.swing.JPanel();
copyrightTextArea = new javax.swing.JTextArea();
closeButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("About Anagrams");
getContentPane().setLayout(new java.awt.GridBagLayout());

mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(11, 11, 12, 12));
mainPanel.setLayout(new java.awt.GridBagLayout());

copyrightTextArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
copyrightTextArea.setColumns(25);
copyrightTextArea.setEditable(false);
copyrightTextArea.setLineWrap(true);
copyrightTextArea.setRows(8);
copyrightTextArea.setText("Created by Kian Kiani\nkiankiani1@yahoo.com\t\n09131005154\n\nSpecial thanks for \nSayesteh Alavi");
copyrightTextArea.setWrapStyleWord(true);
copyrightTextArea.setBorder(null);
copyrightTextArea.setFocusable(false);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(24, 0, 24, 0);
mainPanel.add(copyrightTextArea, gridBagConstraints);

closeButton.setMnemonic('C');
closeButton.setText("بستن");
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
mainPanel.add(closeButton, gridBagConstraints);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(mainPanel, gridBagConstraints);
}//


private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
dispose();
}


// Variables declaration - do not modify
private javax.swing.JButton closeButton;
private javax.swing.JTextArea copyrightTextArea;
private javax.swing.JPanel mainPanel;
// End of variables declaration

}
۰

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


package com.toy.anagrams.ui;

import com.toy.anagrams.lib.ChemLibrary;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JFrame;

/**
* Main window of the Chem Game application.
*/
public class Chembazi extends JFrame {

public static void main(String[] args) {
new Chembazi().setVisible(true);
}

private int wordIdx = 0;
private ChemLibrary wordLibrary;

/** Creates new form Anagrams */
public Chembazi() {
wordLibrary = ChemLibrary.getDefault();

initComponents();
getRootPane().setDefaultButton(guessButton);
scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
pack();
guessedWord.requestFocusInWindow();
// Center in the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation(new Point((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.width) / 2));
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

mainPanel = new javax.swing.JPanel();
scrambledLabel = new javax.swing.JLabel();
scrambledWord = new javax.swing.JTextField();
guessLabel = new javax.swing.JLabel();
guessedWord = new javax.swing.JTextField();
feedbackLabel = new javax.swing.JLabel();
buttonsPanel = new javax.swing.JPanel();
guessButton = new javax.swing.JButton();
nextTrial = new javax.swing.JButton();
mainMenu = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
aboutMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();

setTitle("شیمی بازی");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
mainPanel.setLayout(new java.awt.GridBagLayout());

scrambledLabel.setText("نام ترکیب");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
mainPanel.add(scrambledLabel, gridBagConstraints);

scrambledWord.setColumns(20);
scrambledWord.setEditable(false);
scrambledWord.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
mainPanel.add(scrambledWord, gridBagConstraints);

guessLabel.setDisplayedMnemonic('Y');
guessLabel.setLabelFor(guessedWord);
guessLabel.setText("فرمول ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
mainPanel.add(guessLabel, gridBagConstraints);

guessedWord.setColumns(20);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
mainPanel.add(guessedWord, gridBagConstraints);

feedbackLabel.setText(" ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
mainPanel.add(feedbackLabel, gridBagConstraints);

buttonsPanel.setLayout(new java.awt.GridBagLayout());

guessButton.setMnemonic('G');
guessButton.setText("حدس");
guessButton.setToolTipText("Guess the scrambled word.");
guessButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
guessedWordActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
buttonsPanel.add(guessButton, gridBagConstraints);

nextTrial.setMnemonic('N');
nextTrial.setText("ترکیب جدید");
nextTrial.setToolTipText("Fetch a new word.");
nextTrial.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextTrialActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
gridBagConstraints.weighty = 1.0;
buttonsPanel.add(nextTrial, gridBagConstraints);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
mainPanel.add(buttonsPanel, gridBagConstraints);

getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);

mainMenu.setFont(new java.awt.Font("AlArabiya", 0, 13)); // NOI18N

fileMenu.setMnemonic('F');
fileMenu.setText("فایل");

aboutMenuItem.setMnemonic('A');
aboutMenuItem.setText("درباره");
aboutMenuItem.setToolTipText("About");
aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutMenuItemActionPerformed(evt);
}
});
fileMenu.add(aboutMenuItem);

exitMenuItem.setMnemonic('E');
exitMenuItem.setText("خروج");
exitMenuItem.setToolTipText("Quit Team, Quit!");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});
fileMenu.add(exitMenuItem);

mainMenu.add(fileMenu);

setJMenuBar(mainMenu);
}//


private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
new About(this).setVisible(true);
}

private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {
wordIdx = (wordIdx + 1) % wordLibrary.getSize();

feedbackLabel.setText(" ");
scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
guessedWord.setText("");
getRootPane().setDefaultButton(guessButton);

guessedWord.requestFocusInWindow();
}

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {
if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
feedbackLabel.setText("درست است، بعدی!");
getRootPane().setDefaultButton(nextTrial);
} else {
feedbackLabel.setText("غلط! دوباره سعی کن!");
guessedWord.setText("");
}

guessedWord.requestFocusInWindow();
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

// Variables declaration - do not modify
private javax.swing.JMenuItem aboutMenuItem;
private javax.swing.JPanel buttonsPanel;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JLabel feedbackLabel;
private javax.swing.JMenu fileMenu;
private javax.swing.JButton guessButton;
private javax.swing.JLabel guessLabel;
private javax.swing.JTextField guessedWord;
private javax.swing.JMenuBar mainMenu;
private javax.swing.JPanel mainPanel;
private javax.swing.JButton nextTrial;
private javax.swing.JLabel scrambledLabel;
private javax.swing.JTextField scrambledWord;
// End of variables declaration

}
این نرم افزار را با استفاده از یکی از سمپل های نت بینز ساخته شده که شامل پنج کلاس است!

کلاس اول:


package com.toy.anagrams.ui;

import com.toy.anagrams.lib.ChemLibrary;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JFrame;

/**
* Main window of the Chem Game application.
*/
public class Chembazi extends JFrame {

public static void main(String[] args) {
new Chembazi().setVisible(true);
}

private int wordIdx = 0;
private ChemLibrary wordLibrary;

/** Creates new form Anagrams */
public Chembazi() {
wordLibrary = ChemLibrary.getDefault();

initComponents();
getRootPane().setDefaultButton(guessButton);
scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
pack();
guessedWord.requestFocusInWindow();
// Center in the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation(new Point((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.width) / 2));
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

mainPanel = new javax.swing.JPanel();
scrambledLabel = new javax.swing.JLabel();
scrambledWord = new javax.swing.JTextField();
guessLabel = new javax.swing.JLabel();
guessedWord = new javax.swing.JTextField();
feedbackLabel = new javax.swing.JLabel();
buttonsPanel = new javax.swing.JPanel();
guessButton = new javax.swing.JButton();
nextTrial = new javax.swing.JButton();
mainMenu = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
aboutMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();

setTitle("شیمی بازی");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
mainPanel.setLayout(new java.awt.GridBagLayout());

scrambledLabel.setText("نام ترکیب");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
mainPanel.add(scrambledLabel, gridBagConstraints);

scrambledWord.setColumns(20);
scrambledWord.setEditable(false);
scrambledWord.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
mainPanel.add(scrambledWord, gridBagConstraints);

guessLabel.setDisplayedMnemonic('Y');
guessLabel.setLabelFor(guessedWord);
guessLabel.setText("فرمول ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
mainPanel.add(guessLabel, gridBagConstraints);

guessedWord.setColumns(20);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
mainPanel.add(guessedWord, gridBagConstraints);

feedbackLabel.setText(" ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
mainPanel.add(feedbackLabel, gridBagConstraints);

buttonsPanel.setLayout(new java.awt.GridBagLayout());

guessButton.setMnemonic('G');
guessButton.setText("حدس");
guessButton.setToolTipText("Guess the scrambled word.");
guessButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
guessedWordActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
buttonsPanel.add(guessButton, gridBagConstraints);

nextTrial.setMnemonic('N');
nextTrial.setText("ترکیب جدید");
nextTrial.setToolTipText("Fetch a new word.");
nextTrial.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextTrialActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
gridBagConstraints.weighty = 1.0;
buttonsPanel.add(nextTrial, gridBagConstraints);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
mainPanel.add(buttonsPanel, gridBagConstraints);

getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);

mainMenu.setFont(new java.awt.Font("AlArabiya", 0, 13)); // NOI18N

fileMenu.setMnemonic('F');
fileMenu.setText("فایل");

aboutMenuItem.setMnemonic('A');
aboutMenuItem.setText("درباره");
aboutMenuItem.setToolTipText("About");
aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutMenuItemActionPerformed(evt);
}
});
fileMenu.add(aboutMenuItem);

exitMenuItem.setMnemonic('E');
exitMenuItem.setText("خروج");
exitMenuItem.setToolTipText("Quit Team, Quit!");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});
fileMenu.add(exitMenuItem);

mainMenu.add(fileMenu);

setJMenuBar(mainMenu);
}//


private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
new About(this).setVisible(true);
}

private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {
wordIdx = (wordIdx + 1) % wordLibrary.getSize();

feedbackLabel.setText(" ");
scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
guessedWord.setText("");
getRootPane().setDefaultButton(guessButton);

guessedWord.requestFocusInWindow();
}

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {
if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
feedbackLabel.setText("درست است، بعدی!");
getRootPane().setDefaultButton(nextTrial);
} else {
feedbackLabel.setText("غلط! دوباره سعی کن!");
guessedWord.setText("");
}

guessedWord.requestFocusInWindow();
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

// Variables declaration - do not modify
private javax.swing.JMenuItem aboutMenuItem;
private javax.swing.JPanel buttonsPanel;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JLabel feedbackLabel;
private javax.swing.JMenu fileMenu;
private javax.swing.JButton guessButton;
private javax.swing.JLabel guessLabel;
private javax.swing.JTextField guessedWord;
private javax.swing.JMenuBar mainMenu;
private javax.swing.JPanel mainPanel;
private javax.swing.JButton nextTrial;
private javax.swing.JLabel scrambledLabel;
private javax.swing.JTextField scrambledWord;
// End of variables declaration

}


کلاس دوم:

package com.toy.anagrams.lib;

/**
* Implementation of the logic for the Anagram Game application.
*/
final class StaticChemLibrary extends ChemLibrary {

private static final String[] FORMULA = {
"Na2SO4",
"K2CO3",
"LiNO3",
"CaCl2",
"Ba3(PO4)2",
"MgF2",
"Al(NO2)3",
"BeCl2",
"ZnCl2",
"SO3",
"SO2",
"CO2",
"CO",
"CCl4",
"NCl3",
"NO",
"NO2",
"N2O3",
"N2O4",
"N2O5",
"Al2(CO3)3",
"PBr3",
"PBr5",
"PbO2",
"KI",
"H2SO4",
"HCl",
"HNO3",
"H3PO4",
"H2CO3",
"H2O2",
"Fe(OH)3",
"NiSO4",
"CS2",
"HNO2",
"H2SO3",
"HClO",
"HClO2",
"HClO3",
"HClO4",
"CuCl",
"CuCl2",
"AgNO3",
"NaHCO3",
"Cr2O3"};

private static final String[] PERSIAN_NAME = {
"سدیم سولفات",
"پتاسیم کربنات",
"لیتیم نیترات",
"کلسیم کلرید",
"باریم فسفات",
"منیزیم فلورید",
"آلومینیم نیتریت",
"برلیم کلرید",
"روی کلرید",
"گوگرد تری اکسید",
"گوگرد دی اکسید",
"کربن دی اکسید",
"کربن منو اکسید",
"کربن تترا کلرید",
"نیتروژن تری کلرید",
"نیتروژن منو اکسید",
"نیتروژن دی اکسید",
"دی نیتروژن تری اکسید",
"دی نیتروژن تترا اکسید",
"دی نیتروژن پنتا اکسید",
"آلومینیم کربنات",
"فسفر تری برمید",
"فسفر پنتا برمید",
"اکسید (IV) سرب",
"پتاسیم یدید",
"سولفوریک اسید",
"هیدرو کلریک اسید",
"نیتریک اسید",
"فسفریک اسید",
"کربنیک اسید",
"هیدروژن پر اکسید",
"هیدرواکسید (III) آهن",
"سولفات (II) نیکل ",
"کربن دی سولفید",
"نیترو اسید",
"سولفورو اسید",
"هیپوکلرو اسید",
"کلرو اسید",
"کلریک اسید",
"پر کلریک اسید",
"کلرید (I) مس",
"کلرید (II) مس",
"نقره نیترات",
"سدیم هیدروژن کربنات",
"اکسید (III) کروم"
};

final static ChemLibrary DEFAULT = new StaticChemLibrary();

/**
* Singleton class.
*/
private StaticChemLibrary() {
}

/**
* Gets the word at a given index.
* @param idx index of required word
* @return word at that index in its natural form
*/
public String getWord(int idx) {
return FORMULA[idx];
}

/**
* Gets the word at a given index in its scrambled form.
* @param idx index of required word
* @return word at that index in its scrambled form
*/
public String getScrambledWord(int idx) {
return PERSIAN_NAME[idx];
}

/**
* Gets the number of words in the library.
* @return the total number of plain/scrambled word pairs in the library
*/
public int getSize() {
return FORMULA.length;
}

/**
* Checks whether a user's guess for a word at the given index is correct.
* @param idx index of the word guessed
* @param userGuess the user's guess for the actual word
* @return true if the guess was correct; false otherwise
*/
public boolean isCorrect(int idx, String userGuess) {
return userGuess.equals(getWord(idx));
}

}


کلاس سوم:



package com.toy.anagrams.lib;

/**
* Interface defining logic for the Anagram Game application.
*/
public abstract class ChemLibrary {
/**
* Constructor for subclasses.
*/
protected ChemLibrary() {
}

/** Getter for the default implementation of the WordLibrary.
* @return some default implementation of WordLibrary
*/
public static ChemLibrary getDefault() {
return StaticChemLibrary.DEFAULT;
}

/**
* Gets the word at a given index.
* @param idx index of required word
* @return word at that index in its natural form
*/
public abstract String getWord(int idx);

/**
* Gets the word at a given index in its scrambled form.
* @param idx index of required word
* @return word at that index in its scrambled form
*/
public abstract String getScrambledWord(int idx);

/**
* Gets the number of words in the library.
* @return the total number of plain/scrambled word pairs in the library
*/
public abstract int getSize();

/**
* Checks whether a user's guess for a word at the given index is correct.
* @param idx index of the word guessed
* @param userGuess the user's guess for the actual word
* @return true if the guess was correct; false otherwise
*/
public abstract boolean isCorrect(int idx, String userGuess);
}


کلاس چهارم:


۱۳۸۷ اسفند ۲۲, پنجشنبه

کلاس Math

برخی از توابع ریاضی را می تونید در این کلاس پیدا کنید!
خلاصه مطلب را می تونید اینجا و اینجا بخونید!

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

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

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

۱۳۸۷ دی ۱۹, پنجشنبه

مراحل ساخت منو menu

۱- یک نوار منو ایجاد کنید.
۲- یک منو ایجاد کنید.
۳- آیتم ها را به منو اضافه کنید.
۴- منو را به نوار منو اضافه کنید.
۵-اگر لازم است مراحل ۲ تا ۴ را تکرار کنید.
۶- نوار منو را به فریم اضافه کنید.

با دستور زیر یک نوار منو ایجاد می شود.

MenuBar myMenubar = new MenuBar(); 0

برای ساختن یک منو از سازنده ( Menu (String title استفاده کنید. مثلآ اگر می خواهید منو های File و Edit را بسازید به روش زیر عمل کنید.

Menu fileMenu = new Menu("File");
Menu editMenu = new Menu("Edit");
0
و به همین صورت منو آیتم ها ساخته می شوند.( MenuItem(String menutext
می توانید منو آیتم ها را داخل منو هایی ایجاد کنید که به آنها تعاق دارند.
MenuItem cut = new MenuItem("Cut");
0
می توانید منو آیتم ها را داخل منو هایی ایجاد کنید که به آنها تعاق دارند.

 Menu editMenu = new Menu("Edit");
MenuItem undo = new MenuItem("Undo")
editMenu.add(undo);
editMenu.addSeparator();
MenuItem cut = new MenuItem("Cut")
editMenu.add(cut);
MenuItem copy = new MenuItem("Copy")
editMenu.add(copy);
MenuItem paste = new MenuItem("Paste")
editMenu.add(paste);
MenuItem clear = new MenuItem("Clear")
editMenu.add(clear);
editMenu.addSeparator();
MenuItem selectAll = new MenuItem("Select All");
editMenu.add(selectAll);
0

متد () addSeparetor یک خط افقی در منو ایجاد می کند تا به طور منطقی وظایف جدا گانه از هم جدا شوند.
بعد از آنکه منو ها ساخته شدند ، آنها را به نوار منو اضافه کنید.
      myMenubar.add(fileMenu);
myMenubar.add(editMenu);
0
در پایان وقتی تمام منو ها با نوار منو اضافه شد، نوار منو را به فریم (مثلآ f ) با استفاده از متد زیر اضافه کنید.

f.setMenuBar(myMenuBar);
0

همه چیز در باره لیبل ها(متد های لیبل ها)

لیبل ها اشیاء ساده ای هستند که فقط تعداد کمی سازنده و متد دارند.
    public final static int LEFT
public final static int CENTER
public final static int RIGHT

public Label()
public Label(String text)
public Label(String text, int alignment)

public void addNotify()
public int getAlignment()
public synchronized void setAlignment(int alignment)
public String getText()
public synchronized void setText(String text)
0
شما قبلآ سازنده های اصلی برای یک لیبل را دیدید. همچنین شما می توانید با استفاده از دستور زیر یک لیبل بدون متن بسازید.
Label()
0
همچنین میتوان موقعیت لیبل را با استفاده از دستورات زیر مشخص کرد.
Label center = new Label("This label is centered", Label.CENTER);
Label left = new Label("This label is left-aligned", Label.LEFT);
Label right = new Label("This label is right-aligned", Label.RIGHT);
0

دو متد از java.awt.Label که گاهی ممکن است گاهی به دلایلی آنها را صدا بزنید عبارتند از
getText()
setText(String s)
0
اینها با شما اجازه میدهند تا متن داخل لیبل را تغییر دهید.
به مثال زیر توجه کنید!
String s = l.getText();
l.setText("Here's the new label");
0
لینک انگلیسی

فرض کنید می خواهیم یک اپلتی بنویسیم که در آن لیبلی استفاده می شود که متن آن ۲۴ کاراکتر گنجایش دارد ، فونت آن SansSerif است و رنگ فونت آبی است و زمینه آن زرد است
import java.awt.*;
import java.applet.*;


public class CubScouts extends Applet {

public void init() {

Label cubScouts = new Label("Cub Scouts!");
cubScouts.setForeground(Color.blue);
cubScouts.setBackground(Color.yellow);
cubScouts.setFont(new Font("Sans", Font.BOLD, 24));
this.add(cubScouts);

}

}
0

همه چیز در باره لیبل ها


ساده ترین جزء بر روی یک فریم لیبل ها هستند. یک لیبل یک متنی است که فقط قابل خواندن است.
java.awt.Label;
0
به مثال زیر توجه کنید!
import java.applet.*;  
import java.awt.*;

public class HelloContainer extends Applet {

public void init() {
Label label;
label = new Label("Hello Container");
this.add(label);
}

}
0


در مثال فوق برنامه با یک کلاس شروع می شود برای این منظور ما به دو پکیج اول نیاز داریم.
در خط چهارم کلاس بسط داده شده با اپلت را اعلان می کنیم.
در خط ششم متد init شروع می شود. این متد سه کار انجام می دهد. ابتدا در خط هفتم ، اعلان می کند که label یک Label است. سپس label را با( Label(String s مقدار دهی می کند. و در آخر به layout اضافه می کند.
لینک انگلیسی

سه مرحله برای اضافه کردن یک جزء
۱- اعلان آن
۲- مقدار دهی
۳- اضافه کردن آن به یک layout

دو مرحله اول می تواند به صورت زیر در هم ادقام شوند.
Label label = new Label("Hello Container");
this.add(label);
0

۱۳۸۷ دی ۱۸, چهارشنبه

یک نرم افزار ساده شیمی ( نسخه جدید)

این نسخه جدید به راحتی در لینوکس و ویندوز کار می کند( با تشکر از سرکار خانم شایسته علوی)



package TestChem;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
*
* @author kian
*/

public class Chem3 extends JFrame implements ItemListener {

JComboBox cb;
JLabel lbl;
Icon i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, i34, i35;
//Main

public static void main(String ar[]) {
JFrame f = new Chem3();
f.setTitle("Created By Kian Kiani");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setSize(200, 160);
f.setVisible(true);
}
//Frame

public Chem3() {
i01 = new ImageIcon(getClass().getResource("/images/ca.jpg"));
i02 = new ImageIcon(getClass().getResource("/images/k.jpg"));
i03 = new ImageIcon(getClass().getResource("/images/So.jpg"));

i04 = new ImageIcon(getClass().getResource("/images/Li.jpg"));
i05 = new ImageIcon(getClass().getResource("/images/mg.jpg"));
i06 = new ImageIcon(getClass().getResource("/images/fe.jpg"));
i07 = new ImageIcon(getClass().getResource("/images/ba.jpg"));
i08 = new ImageIcon(getClass().getResource("/images/cu.jpg"));
i09 = new ImageIcon(getClass().getResource("/images/shi.jpg"));
i10 = new ImageIcon(getClass().getResource("/images/p.jpg"));
i11 = new ImageIcon(getClass().getResource("/images/cr.jpg"));
i12 = new ImageIcon(getClass().getResource("/images/z.jpg"));
i13 = new ImageIcon(getClass().getResource("/images/a.jpg"));
i14 = new ImageIcon(getClass().getResource("/images/km.jpg"));
i15 = new ImageIcon(getClass().getResource("/images/km1.jpg"));
i16 = new ImageIcon(getClass().getResource("/images/am.jpg"));
i17 = new ImageIcon(getClass().getResource("/images/soc.jpg"));
i18 = new ImageIcon(getClass().getResource("/images/cl.jpg"));
i19 = new ImageIcon(getClass().getResource("/images/clc.jpg"));
i20 = new ImageIcon(getClass().getResource("/images/hcl.jpg"));
i21 = new ImageIcon(getClass().getResource("/images/sa.jpg"));
i22 = new ImageIcon(getClass().getResource("/images/su.jpg"));
i23 = new ImageIcon(getClass().getResource("/images/pa.jpg"));
i24 = new ImageIcon(getClass().getResource("/images/ppo.jpg"));
i25 = new ImageIcon(getClass().getResource("/images/si.jpg"));
i26 = new ImageIcon(getClass().getResource("/images/pb.jpg"));
i27 = new ImageIcon(getClass().getResource("/images/cc.jpg"));
i28 = new ImageIcon(getClass().getResource("/images/hip.jpg"));
i29 = new ImageIcon(getClass().getResource("/images/nia.jpg"));
i30 = new ImageIcon(getClass().getResource("/images/nio.jpg"));
i31 = new ImageIcon(getClass().getResource("/images/nis.jpg"));
i32 = new ImageIcon(getClass().getResource("/images/cb.jpg"));
i33 = new ImageIcon(getClass().getResource("/images/amo.jpg"));
i34 = new ImageIcon(getClass().getResource("/images/pc.jpg"));
i35 = new ImageIcon(getClass().getResource("/images/hg.jpg"));

cb = new JComboBox();
String[] strmenu = {"CaCO3", "KNO3", "Na2SO4", "LiCl", "MgO", "Fe(OH)3", "BaSO3", "Cu(NO2)2", "NaOH", "KOH", "Cr2S3",
"Zn3(PO4)2", "AgI", "KMno4", "K2MnO4", "(NH4)2Cr2O7", "Na2CrO4", "NaClO2", "Ca(ClO3)2", "HCl", "H2SO4", "H2SO3", "H3PO4",
"H3PO3", "Na2SiO3", "PbO2", "CaC2", "KClO", "HNO3", "HNO2", "NiSO4", "Ca(HCO3)2", "NH4NO3", "KClO4", "Hg(NO3)2"
};
for (int i = 0; i < strmenu.length; i++) {
cb.addItem(strmenu[i]);
}
cb.addItemListener(this);
lbl = new JLabel("", i01, JLabel.CENTER);
Container c = getContentPane();
c.add(cb, BorderLayout.NORTH);
c.add(lbl, BorderLayout.CENTER);
c.setBackground(Color.yellow);
}
//Event

public void itemStateChanged(ItemEvent ie) {
Container c = getContentPane();
int imgidx = cb.getSelectedIndex();
if (imgidx == 0) {
lbl.setIcon(i01);

} else if (imgidx == 1) {
lbl.setIcon(i02);
c.setBackground(Color.red);
} else if (imgidx == 2) {
lbl.setIcon(i03);
c.setBackground(Color.blue);
} else if (imgidx == 3) {
lbl.setIcon(i04);
c.setBackground(Color.green);
} else if (imgidx == 4) {
lbl.setIcon(i05);
c.setBackground(Color.orange);
} else if (imgidx == 5) {
lbl.setIcon(i06);
c.setBackground(Color.yellow);
} else if (imgidx == 6) {
lbl.setIcon(i07);
c.setBackground(Color.red);
} else if (imgidx == 7) {
lbl.setIcon(i08);
c.setBackground(Color.blue);
} else if (imgidx == 8) {
lbl.setIcon(i09);
c.setBackground(Color.green);
} else if (imgidx == 9) {
lbl.setIcon(i10);
c.setBackground(Color.orange);
} else if (imgidx == 10) {
lbl.setIcon(i11);
c.setBackground(Color.red);
} else if (imgidx == 11) {
lbl.setIcon(i12);
c.setBackground(Color.blue);
} else if (imgidx == 12) {
lbl.setIcon(i13);
c.setBackground(Color.orange);
} else if (imgidx == 13) {
lbl.setIcon(i14);
c.setBackground(Color.yellow);
} else if (imgidx == 14) {
lbl.setIcon(i15);
c.setBackground(Color.red);
} else if (imgidx == 15) {
lbl.setIcon(i16);
c.setBackground(Color.blue);
} else if (imgidx == 16) {
lbl.setIcon(i17);
c.setBackground(Color.green);
} else if (imgidx == 17) {
lbl.setIcon(i18);
c.setBackground(Color.orange);
} else if (imgidx == 18) {
lbl.setIcon(i19);
c.setBackground(Color.red);
} else if (imgidx == 19) {
lbl.setIcon(i20);
c.setBackground(Color.blue);
} else if (imgidx == 20) {
lbl.setIcon(i21);
c.setBackground(Color.orange);
} else if (imgidx == 21) {
lbl.setIcon(i22);
c.setBackground(Color.yellow);
} else if (imgidx == 22) {
lbl.setIcon(i23);
c.setBackground(Color.red);
} else if (imgidx == 23) {
lbl.setIcon(i24);
c.setBackground(Color.orange);
} else if (imgidx == 24) {
lbl.setIcon(i25);
c.setBackground(Color.red);
} else if (imgidx == 25) {
lbl.setIcon(i26);
c.setBackground(Color.blue);
} else if (imgidx == 26) {
lbl.setIcon(i27);
c.setBackground(Color.yellow);
} else if (imgidx == 27) {
lbl.setIcon(i28);
c.setBackground(Color.green);
} else if (imgidx == 28) {
lbl.setIcon(i29);
c.setBackground(Color.red);
} else if (imgidx == 29) {
lbl.setIcon(i30);
c.setBackground(Color.orange);
} else if (imgidx == 30) {
lbl.setIcon(i31);
c.setBackground(Color.blue);
} else if (imgidx == 31) {
lbl.setIcon(i32);
c.setBackground(Color.yellow);
} else if (imgidx == 32) {
lbl.setIcon(i33);
c.setBackground(Color.green);
} else if (imgidx == 33) {
lbl.setIcon(i34);
c.setBackground(Color.red);
} else if (imgidx == 34) {
lbl.setIcon(i35);
c.setBackground(Color.blue);
}
}
}
0