import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class Tutorial extends JPanel
{
JTable jt;
public Tutorial()
{
String[] columns = {"Name", "Age", "Gender"};
String[][] data = {{"John", "18", "Male"},
{"Daisy", "19", "Female"},
{"Dave", "23", "Male"},
{"Jake", "30", "Male"}};
jt = new JTable(data, columns)
{
public boolean isCellEditable(int data, int columns)
{
return false;
}
public Component prepareRenderer(TableCellRenderer r, int data, int columns)
{
Component c = super.prepareRenderer(r, data, columns);
if (data % 2 == 0)
c.setBackground(Color.WHITE);
else
c.setBackground(Color.LIGHT_GRAY);
return c;
}
};
jt.setPreferredScrollableViewportSize(new Dimension(450, 63));
jt.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(jt);
add(jps);
}
public static void main(String[] args)
{
JFrame jf = new JFrame("Tutorial");
Tutorial t = new Tutorial();
jf.setSize(500, 500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(t);
}
}
Thursday, December 20, 2012
Java Tutorial 39 Source Code
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Tutorial extends JFrame
{
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField jt = new JTextField(30);
JButton jb = new JButton("Enter");
public Tutorial()
{
setTitle("Tutorial");
setVisible(true);
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp.add(jt);
jt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = jt.getText();
jl.setText(input);
}
});
jp.add(jb);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = jt.getText();
jl.setText(input);
}
});
jp.add(jl);
add(jp);
}
public static void main(String[] args)
{
Tutorial t = new Tutorial();
}
}
Java Tutorial 36 Source Code
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Tutorial extends JFrame
{
JPanel jp = new JPanel();
JLabel jl = new JLabel();
public Tutorial()
{
setTitle("Tutorial");
setVisible(true);
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jl.setIcon(new ImageIcon("C:\\Your image directory goes here"));
jp.add(jl);
add(jp);
validate();
}
public static void main(String[] args)
{
Tutorial t = new Tutorial();
}
}
Java Tutorial 27 Source Code
import java.awt.Graphics;
import javax.swing.JFrame;
public class Tutorial extends JFrame
{
public Tutorial()
{
setTitle("Tutorial");
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
g.drawRect(480, 480, 200, 100);
g.fillRect(240, 240, 200, 100);
}
public static void main(String[] args)
{
Tutorial t = new Tutorial();
t.paint(null); // Not a proper way, but it still works.
}
}
Java Tutorial 26 Source Code
import javax.swing.JFrame;
public class Tutorial extends JFrame
{
public Tutorial()
{
setTitle("Tutorial");
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
Tutorial t = new Tutorial();
}
}
Friday, December 14, 2012
Java Tutorial 7 Source Code
import java.lang.Math;
public class Tutorial
{
public static void main(String[] args)
{
byte a = (byte) Math.pow(9,2);
System.out.println(a);
double b = Math.sqrt(82);
System.out.println(b);
byte c = (byte) Math.max(82, 127);
System.out.println(c);
byte d = (byte) Math.min(82, 127);
System.out.println(d);
byte e = (byte) Math.ceil(1.001);
System.out.println(e);
byte f = (byte) Math.floor(1.99);
System.out.println(f);
byte g = (byte) ((Math.random() * 100) + 1);
System.out.println(g);
byte h = (byte) Math.abs(5-100);
System.out.println(h);
}
}
Java Tutorial 6 Source Code
public class Tutorial
{
public static void main(String[] args)
{
byte age = 18;
switch (age)
{
case 18:
System.out.println("You are 18.");
break;
case 10:
System.out.println("You are young.");
break;
default:
System.out.println("What are you?");
}
}
}
Java Tutorial 5 Source Code
public class Tutorial
{
public static void main(String[] args)
{
byte age = 20;
if (age >= 18 && age < 21)
System.out.println("I welcomed you to this program.");
else if (age >= 21)
System.out.println("You're a grown person!");
else
System.out.println("You are a minor. Get out!");
}
}
Java Tutorial 4 Source Code
import java.util.Scanner;
// Made by Mr. Gizdich
public class Tutorial
{
/*
* Made by John Gizdich
*/
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
double a = 7, c;
int b;
System.out.println("How many ticket(s) will you buy? ");
b = kb.nextDouble();
c = b * a;
System.out.println("You want " + b + " ticket(s). The total cost will be $" + c);
}
}
Java Tutorial 3 Source Code
import java.util.Scanner;
public class Tutorial
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
byte age;
String name;
System.out.println("How old are you? ");
age = kb.nextByte();
Scanner kb1 = new Scanner(System.in);
System.out.println("What's your name? ");
name = kb1.nextLine();
char c = name.charAt(0);
System.out.println("You are " + age + " years old. Your name is "
+ name + ".");
}
}
Java Tutorial 2 Source Code
public class Tutorial
{
public static void main(String[] args)
{
byte dog = 127;
short cat = 32000;
int turtle = 2000000;
long mouse = 100028383;
double grams = 7.9;
float f = 7.9;
boolean open = true;
String Sent = "I want to go to the pet store.";
char signal = 'c';
System.out.println(Sent + " I want " + dog + " dogs.");
}
}
Java Tutorial 1 Source Code
public class Tutorial
{
public static void main(String[] args)
{
System.out.println("Output anything here.");
}
}
Subscribe to:
Posts (Atom)