Jump to content
Sign in to follow this  
eddry

Java

Recommended Posts

nak tanya pasal java ni cam mana nak buat bila guna GUI interface dia tak selari ,

bila run dalam GUI dia berselerak jer tak selari kalu kiri kira kiri semua dia mula...

Share this post


Link to post
Share on other sites

sekarang dah cuba guna net bean tapi lama nak install sebab pc tak power so aku guna JCreator LE sahaja

ni cth yg aku buat:

import java.awt.*;

import java.awt.event.*;

public class CutiCutiMalaysia extends EmptyFrame implements

WindowListener

{

public static void main(String arg[])

{

Frame f = new CutiCutiMalaysia("Cuti-Cuti Malaysia");

f.setSize(300,250);

f.setVisible(true);

}

public void WindowOpened(WindowEvent e){

}

public CutiCutiMalaysia(String str)

{

super(str);

addWindowListener(this);

setLayout(new FlowLayout());

setBackground(Color.yellow);

setForeground(Color.black);

Label LTitle = new Label("Cuti-Cuti Malaysia");

Label LName = new Label("Enter Name");

TextField TName = new TextField(25);

Label LLocation = new Label("Select You Location");

Button BOk = new Button("OK");

Checkbox CBHawai = new Checkbox("Hawai");

Checkbox CBParis = new Checkbox("Paris");

Checkbox CBLondon = new Checkbox("London");

add(LTitle,"Center");

add(LName,"LeftSide");

add(TName);

add(LLocation,"LeftSide");

add(BOk);

add(CBHawai);

add(CBParis);

add(CBLondon);

}

}

Share this post


Link to post
Share on other sites

Ini contoh source code aku. Sorry kalo panjang sgt, sbb aku xder cnth yg pendek dan aku xder masa nk create blk. cool.gif

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.io.*;

public class RenewBook extends JFrame {

JList lstBook;

JScrollPane sp_lstBook;

JTextArea txtDetail;

JScrollPane sp_txtDetail;

JLabel lblBook;

JLabel lblDetail;

JButton btnRenew;

JButton btnBack;

JList lstID;

JScrollPane sp_lstID;

Vector lstNow;

Borrower b;

public RenewBook(Borrower _B)

{

b=_b;

filter();

gui();

}

public void filter()

{

Vector v=new Vector();

for(int i = 0;i< b.data4.size()/9;i++)

{

String info1 = b.data4.elementAt(i*9+5).toString();

if(info1.equals(b.id.toString())==true)

{

for(int j=0;j<=8;j++)

{

v.addElement(b.data4.elementAt(i*9+j).toString());

}

}

}

lstNow=v;

}

public void gui() {

RenewBookLayout customLayout = new RenewBookLayout();

getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));

getContentPane().setLayout(customLayout);

DefaultListModel listModel_lstBook = new DefaultListModel();

DefaultListModel listModel_lstID = new DefaultListModel();

listModel_lstID.clear();

listModel_lstBook.clear();

for(int i = 0;i<lstNow.size()/9;i++)

{

listModel_lstID.addElement(lstNow.elementAt(9*i).toString());

listModel_lstBook.addElement(lstNow.elementAt(9*i+1).toString());

}

lstBook = new JList(listModel_lstBook);

sp_lstBook = new JScrollPane(lstBook);

getContentPane().add(sp_lstBook);

txtDetail = new JTextArea("");

sp_txtDetail = new JScrollPane(txtDetail);

getContentPane().add(sp_txtDetail);

lblBook = new JLabel("Book");

getContentPane().add(lblBook);

lblDetail = new JLabel("Detail");

getContentPane().add(lblDetail);

btnRenew = new JButton("Renew This Book");

getContentPane().add(btnRenew);

btnBack = new JButton("Back To Main Menu");

getContentPane().add(btnBack);

lstID = new JList(listModel_lstID);

sp_lstID = new JScrollPane(lstID);

getContentPane().add(sp_lstID);

setSize(getPreferredSize());

lstBook.setSelectedIndex(0);

lstID.hide();

lstBook.addMouseListener(new java.awt.event.MouseAdapter() {

public void mousePressed(java.awt.event.MouseEvent evt) {

int pilih=lstBook.getSelectedIndex();

lstID.setSelectedIndex(pilih);

int index;

boolean benar;

benar=false;

index=0;

while(index <lstNow.size() && benar==false)

{

benar=lstNow.elementAt(index).equals(lstID.getSelectedValue().toString());

index=index+9;

}

index=index-9;

String borrowStatus;

String reserve;

if(lstNow.elementAt(index+5).equals("") ==true)

{

borrowStatus="NO";

}else

{

if(lstNow.elementAt(index+5).equals(b.id)==true)

{

borrowStatus="SELF";

}else

{

borrowStatus="YES";

} }

if(lstNow.elementAt(index+6).equals("")==true)

{

reserve="NO";

}else

{

if(lstNow.elementAt(index+6).equals(b.id)==true)

{

reserve="SELF";

}else

{

reserve="YES";

}

}

txtDetail.setText("Title \t: "+lstNow.elementAt(index+1).toString()+"\n");

txtDetail.setText(txtDetail.getText()+ "Publisher \t: "+lstNow.elementAt(index+2).toString()+"\n");

txtDetail.setText(txtDetail.getText()+ "Author \t: "+lstNow.elementAt(index+3).toString()+"\n");

txtDetail.setText(txtDetail.getText()+ "Borrowed \t: "+borrowStatus+"\n");

txtDetail.setText(txtDetail.getText()+ "Reserved \t: "+reserve+"\n");

txtDetail.setText(txtDetail.getText()+ "\nDetail \t:\n\n"+lstNow.elementAt(index+8).toString().replace('|','\n')+"\n");

txtDetail.setCaretPosition(1);

}

});

btnRenew.addMouseListener(new java.awt.event.MouseAdapter() {

public void mousePressed(java.awt.event.MouseEvent evt) {

renew();

}

});

btnBack.addMouseListener(new java.awt.event.MouseAdapter() {

public void mousePressed(java.awt.event.MouseEvent evt) {

b.mainMenu();

hide();

}

});

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

public void renew()

{

try

{

int pilih=lstBook.getSelectedIndex();

lstID.setSelectedIndex(pilih);

int index;

boolean benar;

benar=false;

index=0;

while(index <b.data4.size() && benar==false)

{

benar=b.data4.elementAt(index).equals(lstID.getSelectedValue().toString());

index=index+9;

}

index=index-9;

if(b.data4.elementAt(index+5).toString().equals(b.id)==true && b.data4.elementAt(index+6).toString().equals("")==true)

{

int addDate;

if(b.status.equals("0")==true)

{

addDate=7;

}else

{

addDate=21;

}

Date d;

Calendar now = Calendar.getInstance();

now.add(now.DATE,addDate);

d=new Date(now.getTime().toGMTString());

b.data4.setElementAt(b.id,index+5);

b.data4.setElementAt(d.toGMTString(),index+7);

b.vectorToObject();

MySystem function =new MySystem();

function.writeInfo(b.data4,"Book.txt");

BookRenewedDialog renew = new BookRenewedDialog(this);

renew.show();

b.renewBook();

hide();

}else

{

BookRenewedFailDialog fail = new BookRenewedFailDialog(this);

fail.show();

}

}

catch(IOException e)

{

System.err.println("ERROR: IO Exception");

}finally

{

}

}

}

class RenewBookLayout implements LayoutManager {

public RenewBookLayout() {

}

public void addLayoutComponent(String name, Component comp) {

}

public void removeLayoutComponent(Component comp) {

}

public Dimension preferredLayoutSize(Container parent) {

Dimension dim = new Dimension(0, 0);

Insets insets = parent.getInsets();

dim.width = 356 + insets.left + insets.right;

dim.height = 396 + insets.top + insets.bottom;

return dim;

}

public Dimension minimumLayoutSize(Container parent) {

Dimension dim = new Dimension(0, 0);

return dim;

}

public void layoutContainer(Container parent) {

Insets insets = parent.getInsets();

Component c;

c = parent.getComponent(0);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+32,344,152);}

c = parent.getComponent(1);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+216,344,144);}

c = parent.getComponent(2);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,72,24);}

c = parent.getComponent(3);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+192,72,24);}

c = parent.getComponent(4);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+368,184,24);}

c = parent.getComponent(5);

if (c.isVisible()) {c.setBounds(insets.left+192,insets.top+368,160,24);}

c = parent.getComponent(6);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+216,344,144);}

}

}

Share this post


Link to post
Share on other sites

Ni second contoh. Aku bg utk ko compare dengan code yg kt atas aku bg. ph34r.gif

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.io.*;

public class LogIn extends JFrame {

JTextField txtUser;

JComboBox cmbStatus;

JTextField txtPassword;

JLabel lblStatus;

JLabel lblUser;

JLabel lblPassword;

JButton btnEnter;

JButton btnExit;

Vector data1;

Vector data2;

Vector data3;

Vector data4;

public LogIn(Vector _data1,Vector _data2,Vector _data3,Vector _data4) {

data1=_data1;

data2=_data2;

data3=_data3;

data4=_data4;

LogInLayout customLayout = new LogInLayout();

getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));

getContentPane().setLayout(customLayout);

txtUser = new JTextField("");

getContentPane().add(txtUser);

cmbStatus = new JComboBox();

cmbStatus.addItem("Borrower");

cmbStatus.addItem("Librarian");

cmbStatus.addItem("Chief Librarian");

getContentPane().add(cmbStatus);

txtPassword = new JPasswordField("");

getContentPane().add(txtPassword);

lblStatus = new JLabel("Status");

getContentPane().add(lblStatus);

lblUser = new JLabel("Username");

getContentPane().add(lblUser);

lblPassword = new JLabel("Password");

getContentPane().add(lblPassword);

btnEnter = new JButton("ENTER");

getContentPane().add(btnEnter);

btnExit = new JButton("Exit");

getContentPane().add(btnExit);

setSize(getPreferredSize());

btnEnter.addMouseListener(new java.awt.event.MouseAdapter() {

public void mousePressed(java.awt.event.MouseEvent evt) {

int pilih = cmbStatus.getSelectedIndex();

if (pilih==0)

{

enterAction(data1,txtUser.getText(),txtPassword.getText());

}else if(pilih==1)

{

enterAction(data2,txtUser.getText(),txtPassword.getText());

}else

{

enterAction(data3,txtUser.getText(),txtPassword.getText());

}

}

});

btnExit.addMouseListener(new java.awt.event.MouseAdapter() {

public void mousePressed(java.awt.event.MouseEvent evt) {

System.exit(0);

}

});

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

public void enterAction(Vector data, String username,String password)

{

int pilih = cmbStatus.getSelectedIndex();

int index;

Object vPassword;

index=data.indexOf(username);

vPassword=data.elementAt(index+4);

if (pilih==0)

{

if(password.equals(vPassword.toString()) == true)

{

String give[] = new String[8];

Object o;

for (int i=-2;i<=5;i++)

{

o=data.elementAt(index + i);

give[i+2]=o.toString();

}

Borrower currentUser = new Borrower(give[0],give[1],give[2],give[3],give[4],give[5],give[6],give[7],data1,data2,data3,data4);

currentUser.mainMenu();

hide();

}else

{

PasswordFailBox fail= new PasswordFailBox(this);

fail.show();

}

}else if (pilih==1)

{

if(password.equals(vPassword.toString()) == true)

{

String give[] = new String[8];

Object o;

for (int i=-2;i<=5;i++)

{

o=data.elementAt(index + i);

give[i+2]=o.toString();

}

Librarian currentUser = new Librarian(give[0],give[1],give[2],give[3],give[4],give[5],give[6],give[7],data1,data2,data3,data4);

currentUser.mainMenu();

hide();

}else

{

PasswordFailBox fail= new PasswordFailBox(this);

fail.show();

}

}else

{

if(password.equals(vPassword.toString()) == true)

{

String give[] = new String[8];

Object o;

for (int i=-2;i<=5;i++)

{

o=data.elementAt(index + i);

give[i+2]=o.toString();

}

ChiefLibrarian currentUser = new ChiefLibrarian(give[0],give[1],give[2],give[3],give[4],give[5],give[6],give[7],data1,data2,data3,data4);

currentUser.mainMenu();

hide();

}else

{

PasswordFailBox fail= new PasswordFailBox(this);

fail.show();

}

}

}

public static void main(String args[]) throws IOException {

Vector data1 = new Vector();

Vector data2 = new Vector();

Vector data3 = new Vector();

Vector data4 = new Vector();

MySystem function =new MySystem();

data1 =function.readInfo("Borrower.txt");

data2 =function.readInfo("Librarian.txt");

data3 =function.readInfo("Chief.txt");

data4 =function.readInfo("Book.txt");

LogIn window = new LogIn(data1,data2,data3,data4);

window.setTitle("Log In");

window.pack();

window.show();

}

}

class LogInLayout implements LayoutManager {

public LogInLayout() {

}

public void addLayoutComponent(String name, Component comp) {

}

public void removeLayoutComponent(Component comp) {

}

public Dimension preferredLayoutSize(Container parent) {

Dimension dim = new Dimension(0, 0);

Insets insets = parent.getInsets();

dim.width = 302 + insets.left + insets.right;

dim.height = 145 + insets.top + insets.bottom;

return dim;

}

public Dimension minimumLayoutSize(Container parent) {

Dimension dim = new Dimension(0, 0);

return dim;

}

public void layoutContainer(Container parent) {

Insets insets = parent.getInsets();

Component c;

c = parent.getComponent(0);

if (c.isVisible()) {c.setBounds(insets.left+88,insets.top+48,208,24);}

c = parent.getComponent(1);

if (c.isVisible()) {c.setBounds(insets.left+88,insets.top+16,160,24);}

c = parent.getComponent(2);

if (c.isVisible()) {c.setBounds(insets.left+88,insets.top+80,128,24);}

c = parent.getComponent(3);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+16,72,24);}

c = parent.getComponent(4);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+48,72,24);}

c = parent.getComponent(5);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+80,72,24);}

c = parent.getComponent(6);

if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+112,216,24);}

c = parent.getComponent(7);

if (c.isVisible()) {c.setBounds(insets.left+224,insets.top+112,72,24);}

}

}

Share this post


Link to post
Share on other sites

setLayout(new FlowLayout());

patut la jadik cam tu.. tongue.gif

tue flow layout punya susunan...

cuba carik camana nak guna layout2 yg lain...

aku selalu guna borderlayout, gridlayout, n flowlayout...

ada banyak lagi tapi akuselalu guna yg tue jer dah ok..

nak bagi cantik interface nie ko kena pandai2 imagine dalam kepala camna nak susunkan dia...

aku rasa baik ko try practice camna nak guna layout2... smile.gif

Share this post


Link to post
Share on other sites

a'kum....saya try run coding tuh tp ader error.saya tatau la kot² saya silap cara nak run coding tuh.lgpon saya baru lg dlm java nih.saya just nak tgk cane bentuk layout yg awak kasik tu.sesaper leh tulun xplain tak?time kasih

Share this post


Link to post
Share on other sites

ni lagi satu contoh.....

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/**

* This application provides a GUI to operate a hotel reservation system

*

* @author Ting Shih

* @version 1.0.0

*/

public class HotelReservation extends Frame

implements ActionListener, TextListener, WindowListener {

private TextField input, output;

private Panel opsPad;

private Checkbox reserveBox;

private Checkbox cancelBox;

private CheckboxGroup ops;

private Button start;

private Reservation resv;

private String customer;

/**

* The main routine.

*

* @param args String arguments. Not used.

*/

public static void main (String[] args) {

HotelReservation sc = new HotelReservation(1000, 1000);

sc.show();

}

/**

* Initializes widgets and creates layouts.

*

* @param width the width of the window

* @param height the height of the window

*/

public HotelReservation(int width, int height) {

setSize (width, height);

input = new TextField(15);

output = new TextField(30);

opsPad = new Panel();

opsPad.setLayout(new GridLayout(2,1));

CheckboxGroup ops = new CheckboxGroup();

reserveBox = new Checkbox("Reserve", ops, true);

cancelBox = new Checkbox("Cancel", ops, false);

opsPad.add(reserveBox);

opsPad.add(cancelBox);

start = new Button("Start");

setLayout(new FlowLayout());

add(input);

add(opsPad);

add(start);

add(output);

resv = new Reservation();

start.addActionListener(this);

input.addTextListener(this);

output.addTextListener(this);

addWindowListener(this);

}

private void reserve(String customer) {

if (resv.reserve (customer)) {

output.setText("Reservation has been made for " + customer + ".");

}

else output.setText ("The hotel is full.");

input.setText("");

}

private void cancel(String customer) {

if (resv.cancel (customer)) {

output.setText("Reservation has been canceled for " + customer + ".");

}

else output.setText (customer + " has no reservation at this hotel.");

input.setText("");

}

/**

* Process the button events.

*

* @param e the event generated.

*/

public void actionPerformed(ActionEvent evt) {

if (evt.getSource().equals(start)) {

if (reserveBox.getState()) {

reserve(customer);

} else {

cancel(customer);

}

}

}

/**

* Process the text event.

*

* @param e the event generated.

*/

public void textValueChanged(TextEvent evt) {

customer = input.getText();

}

/**

* method contains empty body

*

* @param e not used

*/

public void windowActivated(WindowEvent e) {}

/**

* method contains empty body

*

* @param e not used

*/

public void windowClosed(WindowEvent e) {}

/**

* When the close box is clicked or the close window menu item

* is chosen, this method causes the application to terminate

*

* @param e not used

*/

public void windowClosing(WindowEvent e) {

setVisible(false);

dispose();

System.exit(0);

}

/**

* method contains empty body

*

* @param e not used

*/

public void windowDeactivated(WindowEvent e) {}

/**

* method contains empty body

*

* @param e not used

*/

public void windowDeiconified(WindowEvent e) {}

/**

* method contains empty body

*

* @param e not used

*/

public void windowIconified(WindowEvent e) {}

/**

* method contains empty body

*

* @param e not used

*/

public void windowOpened(WindowEvent e) {}

}

Share this post


Link to post
Share on other sites

a'kum....saya try run coding tuh tp ader error.saya tatau la kot² saya silap cara nak run coding tuh.lgpon saya baru lg dlm java nih.saya just nak tgk cane bentuk layout yg awak kasik tu.sesaper leh tulun xplain tak?time kasih

coding yg mana?? yg malaysia bg tu ker???

kalau aku tak silap tu just GUI je...tak masuk class dia lagi...kan?

Share this post


Link to post
Share on other sites

oooooo sorry lama tak masuk tq pada yg ajar aku hari tu memang senang kalau ada tutorial ngan sedikit perbincangan ni kalau nak harap aku sorang alamat pening ler kepala aku. malaysia ko nga koding tu memang bagus .

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...