0% found this document useful (0 votes)
40 views13 pages

Chapter 1 LAB

3. The code segment shows using input streams to read user input, but is incomplete as it is missing the code demonstrating the input stream usage.

Uploaded by

Israel Kifle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views13 pages

Chapter 1 LAB

3. The code segment shows using input streams to read user input, but is incomplete as it is missing the code demonstrating the input stream usage.

Uploaded by

Israel Kifle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Demonstrates construction of a Container and a Button


import java.awt.*;
class GUI extends Frame {
public GUI (String s) { //constructor
setTitle(" Graphical User Interface");
setBackground(Color.white);
setLayout(new FlowLayout());
Button pushButton = new Button("press me");
add(pushButton);
}} //class Gui
class Ex_1 //Creates an instance of class Gui
{ public static void main(String[] args){
GUI screen = new GUI("Example 1");
screen.setSize(400,400);
screen.setVisible(true);
} } //class Ex_1

Output:

pg. 1
2. Demonstrates construction of a Frame and a Button

import java.awt.*;
import javax.swing.*;
public class FrameExample{
public static void main(String[] args) {
// TODO code application logic here
JFrame f= new JFrame();
f.setVisible(true);
f.setSize(400,400);
f.setResizable(true);
f.setLocation(200,200);
f.setTitle("Harambe University computer Science Departement");
Container c= f.getContentPane(); //creating container object
c.setBackground(Color.green); //calling setBackground method
//button appears in window on screen
//setVisible() creates peer objects for myFrame & myButton
JButton b=new JButton("Submit"); //creating instance of JButton
b.setBounds(130,100,100, 40); //x axis, y axis, width, height
f.add(b); //adding button in Jframe
f.setSize(500,500); //400 width and 500 height
f.setLayout(null); //using no layout managers
f.setVisible(true); //making the frame visible
}}
Output:

pg. 2
3. Example for Frame with Buttons and Checkboxs

import java.awt.*;
import javax.swing.*;
public class FrameExample{
public static void main(String args[]) {
JFrame f= new JFrame();
f.setVisible(true);
f.setSize(400,400);
f.setResizable(true);
//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//specifying close operation

f.setLocation(200,200);
f.setTitle("Harambe University Computer Science Departement");
f.setBackground(Color.green);
Container c= f.getContentPane(); //creating container object

c.setBackground(Color.green); //calling setBackground method

//button appears in window on screen


//setVisible() creates peer objects for myFrame & myButton

JButton b=new JButton("Submit"); //creating instance of JButton

b.setBounds(130,250,100, 40); //x axis, y axis, width, height

f.add(b); //adding button in JFrame

f.setSize(500,500); //400 width and 500 height

f.setLayout(null); //using no layout managers

f.setVisible(true); //making the frame visible

Checkbox checkbox1 = new Checkbox("C++");


checkbox1.setBounds(100,100, 80,80);
Checkbox checkbox2 = new Checkbox("Java", true);

pg. 3
checkbox2.setBounds(180,100, 80,80);
Checkbox checkbox3 = new Checkbox("Database", true);
checkbox3.setBounds(260,100, 80,80);
f.add(checkbox1);
f.add(checkbox2);
f.add(checkbox3);
} }

Output:

4. Example for Java Swing Control

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo1{

pg. 4
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo1(){
prepareGUI();
}
public static void main(String args[]){
SwingControlDemo1 swingControlDemo=new SwingControlDemo1();
swingControlDemo.showTextFieldDemo();
}
private void prepareGUI(){
mainFrame=new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3,1));
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel=new JLabel("",JLabel.CENTER);
statusLabel=new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel=new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);

pg. 5
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showTextFieldDemo(){
headerLabel.setText("Control in action: JTextField");
JLabel namelabel=new JLabel("User ID:",JLabel.RIGHT);
JLabel passwordLabel=new JLabel("Password:",JLabel.CENTER);
final JTextField userText=new JTextField(6);
final JPasswordField passwordText=new JPasswordField(6);
JButton loginButton=new JButton("Login");
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String data="Username"+" "+userText.getText();
data+=",Password:"+" "+new String(passwordText.getPassword());
statusLabel.setText(data);
}
});
controlPanel.add(namelabel);
controlPanel.add(userText);
controlPanel.add(passwordLabel);
controlPanel.add(passwordText);
controlPanel.add(loginButton);
mainFrame.setVisible(true);
}
}

pg. 6
Output:

5. JToolBar Examples

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

public class JToolBarExample {


public static void main(final String args[]) {
JFrame myframe = new JFrame("JToolBar Example");

myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
JButton button = new JButton("File");
toolbar.add(button);

pg. 7
toolbar.addSeparator();
toolbar.add(new JButton("Home"));
toolbar.addSeparator();

toolbar.add(new JButton("Inser"));
toolbar.addSeparator();
toolbar.add(new JButton("Page Layout"));
toolbar.addSeparator();
toolbar.add(new JButton("View"));
toolbar.addSeparator();

// toolbar.add(new JComboBox(new String[] { "Opt-1", "Opt-2", "Opt-3", "Opt4"


}));
Container contentPane = myframe.getContentPane();
contentPane.add(toolbar, BorderLayout.NORTH);
JTextArea textArea = new JTextArea();
JScrollPane mypane = new JScrollPane(textArea);
contentPane.add(mypane, BorderLayout.EAST);
myframe.setSize(500, 500);
myframe.setVisible(true);
}
}

Output:

pg. 8
Note:

pg. 9
Chapter Two Stream and File IO
1. Input and Output stream Example

import java.io.DataInputStream;
class Welcome1{
public static void main(String args[]){
DataInputStream k=new DataInputStream(System.in);
try{
System.out.println("Enter a text 'welcome' ");
String e="4th Year Computer Science"+" "+k.readLine();
System.out.println(e);
for(int i=17;i>=14;i--)
{
System.out.print(e.charAt(i));
}
System.out.println();
System.out.println(e.toUpperCase());
StringBuffer word=new StringBuffer(e);
word.setCharAt(12,'B');
System.out.println(word);
word.append(" ");
word.append('t');
word.append('o');
word.append(" ");
word.append('j');
word.append('a');
word.append('v');
word.append('a');
word.insert(30," students"+" ");
word.setCharAt(12,'I');
System.out.println(word);
word.deleteCharAt(0);
System.out.println(word);
word.reverse();
System.out.println(word);
}
catch(Exception e){
}
}}

pg. 10
2. Example for Input Stream by DataInput Stream:

import java.io.DataInputStream;
class ByDataInputStreams{
public static void main(String args[]){
DataInputStream m=new DataInputStream(System.in);
try{
System.out.println("Enter the first integer");
int a=Integer.parseInt(m.readLine());
System.out.println("Enter the second integer");
int b=Integer.parseInt(m.readLine());
System.out.println("Enter the third integer");
int c=Integer.parseInt(m.readLine());
int d=a*b*c;
System.out.println("The product of the three integer is"+" "+d);
}
catch(Exception e){
}}
}

3. Example for Input Stream by using Scanner

import java.util.Scanner;
class product{
public static void main(String args[]){
int a,b,c,d;
Scanner m=new Scanner(System.in);
System.out.println("Enter the three integers");
a=m.nextInt();
b=m.nextInt();
c=m.nextInt();
d=a*b*c;
System.out.println("The product of the three integer is"+" "+d);
}
}

pg. 11
4. Read file from sorce file stream examples:

import java.io.*;
public class ReadFileDemo{
public static void main(String agrs[]){
File file=new File("C://Local C Volume/File//mm.docx");
BufferedInputStream bis=null;
FileInputStream fis=null;
try{
fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
while(bis.available()>0){
System.out.print((char)bis.read());
}
}
catch(FileNotFoundException fnfe){
System.out.println("The specified file not found"+fnfe);
}
catch(IOException ioe){
System.out.println("I/O Exception:"+ioe);
}
finally{
try{
if(bis!=null&&fis!=null){
fis.close();
bis.close();
}
}
catch(IOException ioe){
System.out.println("Error in InputStream close():"+ioe);
}
}
}

pg. 12
5. Write file from sorce file stream examples:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileDemo{
public static void main(String args[]){
FileOutputStream fos=null;
File file;
String mycontent="This is my Data which needs to be written into the file";
try{
file=new File("C://Local C Volume/File//Computer.docx");
fos=new FileOutputStream(file);
if(!file.exists()){
file.createNewFile();}
byte[] bytesArray=mycontent.getBytes();
fos.write(bytesArray);
fos.flush();
System.out.println("File Written Successfully");
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
try{
if(fos!=null){
fos.close();
}
}
catch(IOException ioe){
System.out.println("Error in closing the Stream");
}
}

pg. 13

You might also like