Monday, February 28, 2011

Java GUI

Here is a set of class that displays a graphic user interface.
These are multiple classes so you should obtain them all first at
http://www.mediafire.com/download.php?23vhz263zr696qi

Actor.java contains the main method
---------------------------------------------------------
import javax.swing.JOptionPane;
public class Actor {

static void newOpt(){
String[] Choices={"BorderLayout","Box Layout","Figures"};
int i=JOptionPane.showOptionDialog(null, "Pick your Ui", "You choose", JOptionPane.DEFAULT_OPTION , JOptionPane.CLOSED_OPTION, null, Choices, "");
System.out.println(""+i);
LaunchFrame LF=new LaunchFrame();
switch(i){
case 0: LF.BrdrL(Choices);break;
case 1: LF.BoxL();break;
case 2: LF.Figures();break;
}
}
public static void main(String[] args) {
String[] elems={"JFrame","JLabel","JTextField","JComboBox","JButton","Confirm Dialog","Input Dialog","Message dialog",
"Password","TextArea","Editor Pane","Menu","Tab","CheckBox & list"};
LaunchFrame LF=new LaunchFrame();

int i=JOptionPane.showOptionDialog(null, "Pick your Ui", "You choose", JOptionPane.DEFAULT_OPTION , JOptionPane.CLOSED_OPTION, null, elems, "");
System.out.println(""+i);
switch(i){
case 0: LF.Act1(); break;
case 1: LF.FrameLabel();break;
case 2: LF.TxtBx();break;
case 3: LF.Options();break;
case 4: LF.Buttons();break;
case 5: System.out.println("Returned "+JOptionPane.showConfirmDialog(null, elems[5])); break;
case 6: System.out.println("Returned "+JOptionPane.showInputDialog("Type something"));break;
case 7: JOptionPane.showMessageDialog(null, elems[7]);break;//returns void
case 8: LF.Password();break;
case 9: LF.TxtArea(); break;
case 10: LF.Ep();break;
case 11: LF.MenuB(); break;
case 12: LF.showTab(); break;
case 13: LF.Listing(elems);break;
default: newOpt();break;
}

}

}
---------------------------------------------------------
The launch frame has methods that displays the GUI component to be displayed
________________________
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
________________________
It has the following methods





























Method codeOutput when called

public class LaunchFrame{
public void Act1(){
JFrame JF=new JFrame("This is a frame"); // Creates a Frame with a titile on the title bar
JF.setSize(400, 350); //Sets the initial size of the frame
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // The application will close and free up memory space
JF.setVisible(true); //Makes the Frame visible on the monitor
}

public String FileSrch(){
JFileChooser pick = new JFileChooser();
int i=pick.showOpenDialog(null);
if(i==pick.APPROVE_OPTION)
return pick.getSelectedFile().toString();
else
return null;
}
A file dialog box

public void FrameLabel(){
JFrame JF=new JFrame("This is a frame");
JF.setSize(400, 350);
JLabel lbl=new JLabel("Message",JLabel.CENTER); //A JLabel is created stating Message alligned at the center
JF.add(lbl); //The JFrame will contain the JLabel
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
public void TxtBx(){
JFrame JF=new JFrame("This is a frame");
JF.setSize(350,300);
JF.getContentPane().setLayout(new FlowLayout()); //Change the Layouof the Frame
JF.add(new JLabel("Add text: ")); //JF will now contain a JLabel
JTextField txtbx=new JTextField(10); //Create a TextField with a specific width
JF.add(txtbx); //JF will now contain the TextBox
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
public void Options(){
String[] members={"Nickleus","Yao","Jake","Jul"}; //Create an Array of Strings with values
JFrame JF=new JFrame("Pick an option");
JF.setSize(250,200);
JF.getContentPane().setLayout(new FlowLayout());
JF.add(new JLabel("List of members"));
JComboBox cb=new JComboBox(); //Create a JCombobox
cb.addItem("Members"); //Add an Item in the JComboBox
for(int ctr=0;ctr
public void Buttons(){
JFrame JF=new JFrame("Pick an option");
JF.getContentPane().setLayout(new FlowLayout());
JF.add(new JLabel("Click"));
//Set an image to be an Icon
ImageIcon ic = new ImageIcon("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioi5DYPzAvh4M3L5QVAXIj4A84BmNEdHArTME8f_XE1CGQvlTOF6s7eprDUIHuJ0iyaYzmLVNGjdzFsLeThybqRIgAsTHE0l55rSkDcCBtUwkumn-VRfZMZSGcSAJuHg5SQgQ9P_yYt0o/");
JButton bt=new JButton("Click me",ic);
bt.setSize(150, 100); //Set the size of the button
bt.setToolTipText("You can click this"); //Create a message when mouse hovers on the button
JF.add(bt);
JF.pack();
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
public void Password(){
JPasswordField pf=new JPasswordField(10); //Create a password field with a width of 10 pixels
JFrame JF=new JFrame("pass");
JF.getContentPane().setLayout(new GridLayout(2,2)); //set the Frame with a GridLayout
JF.add(new JLabel("User name: "));
JTextField txtbx=new JTextField(10);
JF.add(txtbx);
JF.add(new JLabel("Password: "));
JF.add(pf);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.pack();
JF.setVisible(true);
}

public void TxtArea(){ //Show a text Area
JFrame JF=new JFrame("This is a frame");
JF.setLayout(new FlowLayout());
JF.add(new JLabel("Add text: "));
JTextArea txtbx=new JTextArea(10,15); // A text area with the specified text, rows, and columns
JF.add(txtbx);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.pack(); //Frame authomatically has an initial size base on its contents
JF.setVisible(true);
}
Image

Thursday, February 3, 2011

Immigration from C to java[to be finished]
















In CIn java

main(){
/*Code here*/
}

public static void main(String[] args) {
/*Code here*/
}

int num;
printf("Enter a number: ");
scanf("%d",&num);

Scanner scn=new Scanner(System.in);
System.out.print("Enter a number to a variable: ");
int num=scn.nextInt();
//end of solution 1

//Start of solution 2
int num;
Scanner scn=new Scanner(System.in);
System.out.print("Enter a number to a variable: ");
num=scn.nextInt();

int x=0;
printf("x+1= %d\n",x+1);

int x=0;
System.out.println("x+1= "+(x+1));

char word[10];
printf("Enter a word: ");
scanf("%s",word);
String word;
System.out.print("Enter a word: ");
word=scn.next();
//scn is a defined scanner object from the code above

int ArrInt[5];
int[] ArrInt=new int[5]