Thursday 29 September 2011

Day 1

8.44 am Achievement
woke up at 7.30, the first two GUI Components i mastered:
The Button and The Label.
here's my first Button+Label xD


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Button extends JFrame
{ static private JButton button; static private JLabel label;
 public Button()
 {super("Buttoning");
  Container c=getContentPane();
  c.setLayout(new FlowLayout());
  label=new JLabel("Will you marry me?");
  c.add(label);
  button=new JButton("Yes");
  c.add(button);
  ButtonHandler handler = new ButtonHandler();
  button.addActionListener(handler);
  setSize(275, 100);
  show();
 }
  public static void main (String[] args)
 {new Button().addWindowListener(
  new WindowAdapter()
  {public void WindowClosing(WindowEvent e)
   {System.exit(0);}
  }
  );
 }
 private class ButtonHandler implements ActionListener
 {public void actionPerformed(ActionEvent e)
  {if(e.getActionCommand()=="Yes") System.out.println("JK LOL");}
 }
}

on to JLists now.
*******
11.59am achievement 
JList and setBackground conquered!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class List extends JFrame
{JList colourList; Container c;
String colours[]={"yellow", "red", "blue", "orange", "black"};
Color coloursPaint[]={Color.yellow, Color.red, Color.blue, Color.orange, Color.black};
public List()
{c= getContentPane();
c.setLayout(new FlowLayout());
colourList=new JList(colours);
colourList.setVisibleRowCount(3);
colourList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
c.add(new JScrollPane(colourList));
setSize(350, 150);
show();
colourList.addListSelectionListener(
new ListSelectionListener()
{public void valueChanged(ListSelectionEvent e)
{c.setBackground(coloursPaint[colourList.getSelectedIndex()]);}
}
);
}
public static void main(String args[])
{new List().addWindowListener(
new WindowAdapter()
{public void close(WindowEvent e) {System.exit(0);}}
);
}
}
i shall go read a good book now.
 

1 comment:

Anonymous said...

Java code amidst all of this emo pinkness is highly disconcerting
and you say its a hobby!!

Related Posts Plugin for WordPress, Blogger...