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

مراحل ساخت منو 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