Java Programming
Results 1 to 4 of 4

Thread: Java Programming

  1. #1
    Member
    Join Date
    Jan 2003
    Posts
    42

    Java Programming

    What are these layout managers and action listners in java can some one give some example code regarding these two
    and suggest some good books on java

  2. #2
    Senior Member
    Join Date
    May 2003
    Posts
    226
    Layout managers control how the components are placed in the GUI of the application for example BoxLayout places components on top of each other of your choice etc...

    ActionListeners are "listeners" the listens for particular action for a component in the GUI. for example, i have a "start" button i can perform several actions on it example when user moves his mouse over, i can use on mouse over mouse listener to create effects on the button, and when the user presses the buttons, what are the actions gonna be perform by the application

    Here is some good sites on Java Swing

  3. #3
    Senior Member
    Join Date
    Nov 2003
    Posts
    285
    "Java 2 complete refrence" is a good book on java

    here is a calander program i made in java hope this helps

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Calendar;

    public class MyCalender extends Frame implements ActionListener{

    TextField txtMonthName; /* holds the month name */
    TextField txtYear; /* hods the year e.g. 2002 */
    Button btnShow; /* Caption : SHOW, displays the calendar */
    Button btnDates[]; /* display the calendar dates */

    MyCalender (String title) {
    super(title); /* Setting the title of the frame */

    addWindowListener(new MyWindowAdapter());

    setFont(new Font("Dialog", Font.BOLD+Font.ITALIC, 30));

    /* setting the layout of the frame as Border layout */
    setLayout(new BorderLayout(6,6));

    // Here we are adding the fields for specifying the month & year.

    txtMonthName = new TextField(20);

    txtYear = new TextField(6);

    btnShow = new Button("Show");

    btnShow.addActionListener(this);


    // add a new Panel to add all the fields

    Panel pFields = new Panel();
    pFields.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    pFields.setFont(new Font("SansSerif", Font.BOLD, 20));
    pFields.add(txtMonthName); pFields.add(txtYear);
    pFields.add(btnShow);
    add(pFields, BorderLayout.NORTH);

    Button btnDays[] = new Button[7];


    btnDays[0] = new Button("Sun");
    btnDays[1] = new Button("Mon");
    btnDays[2] = new Button("Tue");
    btnDays[3] = new Button("Wed");
    btnDays[4] = new Button("Thu");
    btnDays[5] = new Button("Fri");
    btnDays[6] = new Button("Sat");



    Panel pDates = new Panel();
    pDates.setLayout(new GridLayout(6, 7, 1, 1));

    for(int i=0; i<7; i++)
    {
    btnDays[i].setBackground(Color.white);
    btnDays[i].setFont(new Font("SansSerif", Font.BOLD+Font.ITALIC, 20));
    pDates.add(btnDays[i]);
    }

    btnDays[0].setForeground(Color.red);

    btnDates = new Button[35];

    for( int j=0; j<35; j++)
    {
    btnDates[j] = new Button();
    btnDates[j].setBackground(Color.white);
    btnDates[j].setFont(new Font("SansSerif", Font.BOLD+Font.ITALIC, 20));

    if ( (j%7) == 0 ) {
    btnDates[j].setForeground(Color.red);
    }

    pDates.add(btnDates[j]);
    }

    add(pDates, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent ae){

    String str = ae.getActionCommand();


    if (str.equals("Show")) {
    generateCalender();
    }
    }

    public void generateCalender(){

    Calendar dtDate = Calendar.getInstance();
    int iMonth = 13;
    int iWeekDay;
    int iDates = 1;
    int iDays = 0;

    dtDate.set(Calendar.DATE, 1);



    /* get the first three letter of the month name entered
    and change the case to upper case */

    String s = txtMonthName.getText().substring(0,3).toUpperCase();

    txtMonthName.setText(s);

    if (s.equals("JAN")) {
    iMonth = 0;
    txtMonthName.setText("January");
    }

    else if (s.equals("FEB")) {
    iMonth = 1;
    txtMonthName.setText("February");
    }

    else if (s.equals("MAR")) {
    iMonth = 2;
    txtMonthName.setText("March");
    }

    else if (s.equals("APR")) {
    iMonth = 3;
    txtMonthName.setText("April");
    }



    else if (s.equals("MAY")) {
    iMonth = 4;
    txtMonthName.setText("May");
    }

    else if (s.equals("JUN")) {
    iMonth = 5;
    txtMonthName.setText("June");
    }

    else if (s.equals("JUL")) {
    iMonth = 6;
    txtMonthName.setText("July");
    }

    else if (s.equals("AUG")) {
    iMonth = 7;
    txtMonthName.setText("August");
    }

    else if (s.equals("SEP")) {
    iMonth = 8;
    txtMonthName.setText("September");
    }

    else if (s.equals("OCT")) {
    iMonth = 9;
    txtMonthName.setText("October");
    }

    else if (s.equals("NOV")) {
    iMonth = 10;
    txtMonthName.setText("November");
    }

    else if (s.equals("DEC")) {
    iMonth = 11;
    txtMonthName.setText("December");
    }

    switch(iMonth) {
    case 0:
    case 2:
    case 4:
    case 6:


    case 7:
    case 9:
    case 11:
    iDays = 31; break;

    case 3:
    case 5:
    case 8:
    case 10:

    iDays = 30; break;

    case 1:

    if ((Integer.parseInt(txtYear.getText()) % 4) == 0) {
    iDays = 29;
    }
    else { iDays = 28; }
    break;
    }

    /* Set the month of the calendar object */
    dtDate.set(Calendar.MONTH, iMonth);

    /* Set the caledar year */
    dtDate.set(Calendar.YEAR, Integer.parseInt(txtYear.getText()));

    /* get the week day number, 0 = SUNDAY, 1 = MONDAY etc. */
    iWeekDay = dtDate.get(Calendar.DAY_OF_WEEK) - 1;



    /* clear all the date buttons
    for (int i=0; i<35; i++)
    btnDates[i].setLabel("");

    int iCounter = iWeekDay;

    /* display all the calendar dates */

    do{
    btnDates[iCounter].setLabel(Integer.toString(iDates));
    iDates++;
    iCounter++;


    if ((iCounter == 35) && (iDates <= iDays))
    {
    iCounter = 0;
    }

    }while(iDates <= iDays);
    }

    public static void main(String args[]){

    MyCalender calender = new MyCalender("Calender");

    calender.setSize(425, 275);
    calender.setVisible(true);
    }
    }

    class MyWindowAdapter extends WindowAdapter {

    public void windowClosing(WindowEvent ae) {
    System.exit(0);
    }
    }

  4. #4
    Senior Member
    Join Date
    Dec 2003
    Location
    LA, CA
    Posts
    292
    Introduction to Java Programming
    Y. Daniel Liang
    Prentice Hall
    ISBN 0-13-100225-2
    covers programming basics - multithreading, networking
    A mind full of questions has no room for answers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides