You are on page 1of 9

Gui

import javax.swing.*;
import java.awt.*;

public class BorderLayoutDemo extends JFrame


{
JButton jButton1 = new JButton("I am Button 1");
JButton jButton2 = new JButton("Button 2");
JButton jButton3 = new JButton("Button 3!!!!!!!!!");
JButton jButton4 = new JButton("I am Button 4");
JButton jButton5 = new JButton("I am Button 5");
JButton jButton6= new JButton("I am Button 6");

public BorderLayoutDemo()
{
add(jButton1, BorderLayout.NORTH);
add(jButton2, BorderLayout.SOUTH);
add(jButton3, BorderLayout.EAST);
add(jButton4, BorderLayout.WEST);
add(jButton5, BorderLayout.CENTER);
}

public static void main(String[] args)


{
BorderLayoutDemo frame = new BorderLayoutDemo();
frame.setTitle("Using default Layout");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Buttons with panel
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
public class Display extends JFrame
{
private JButton jbButton1 = new JButton("Button1");
private JButton jbButton2 = new JButton("Button2");
private JButton jb1 = new JButton("1");
private JButton jb2 = new JButton("2");
private JButton jb3 = new JButton("3");
private JButton jb4 = new JButton("4");
private JButton jb5 = new JButton("5");
private JButton jb6 = new JButton("6");

public Display()
{
JPanel panel1 = new JPanel(new GridLayout(3,2));
panel1.setBorder(new TitledBorder("Panel 1"));
panel1.add(jb1);
panel1.add(jb2);
panel1.add(jb3);
panel1.add(jb4);
panel1.add(jb5);
panel1.add(jb6);

JPanel panel2 = new JPanel(new BorderLayout());


panel2.setBorder(new TitledBorder("Panel 2"));
panel2.add(jbButton2, BorderLayout.NORTH);
panel2.add(panel1, BorderLayout.CENTER);

add(jbButton1, BorderLayout.WEST);
add(panel2, BorderLayout.CENTER);
}

public static void main(String [] args)


{
Display f = new Display();
f.pack();
f.setTitle("Exercise");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
/* This example shows how panels are used as subcontainers.
* First, two buttons and a panel are created.
* Then, the buttons are added to the panel.
* Finally, the panel is added to the frame.
*/

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class PanelDemo extends JFrame


{
private JButton jbt1;
private JButton jbt2;

public PanelDemo()
{
jbt1 = new JButton(" 1 "); // Create a button
jbt1.setBackground(Color.MAGENTA);
jbt2 = new JButton(" 2 "); // Create a button
jbt2.setBackground(new Color(255, 255, 255));
jbt2.setForeground(Color.RED);
JPanel panel = new JPanel(); // Create a panel (FlowLayout)
TitledBorder panelBorder = new TitledBorder("Panel");
panel.setBorder(panelBorder);
panel.add(jbt1); // Add jbt1 to panel
panel.add(jbt2); // Add jbt2 to panel
add(panel); // Add panel to frame
}

public static void main(String[] args)


{
PanelDemo frame = new PanelDemo();
frame.setTitle("JPanel Demo");
frame.setSize(200, 75);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
import java.io.*; // for File and PrintWriter
import java.util.*; // for Scanner
public class PrintExample
{
public static void main(String[] args)
throws Exception
{
File file = new File("text.txt");
PrintWriter output = new PrintWriter(file);
output.println("Hello there");
output.print("How are you");
output.close();

Scanner input = new Scanner(file);


while( input.hasNext() )
{
String s = input.next();
System.out.println(s);
}
input.close();
}

}
Write and edit notepad shit

import java.io.*;
import java.util.*;
import javax.swing.*;

public class WritingReadingIntegers


{
public static void main(String[] args) throws Exception
{
int i;
String stringInput = JOptionPane.showInputDialog("Enter an int:");
File file = new File("integer.txt");
PrintWriter out = new PrintWriter(file);
out.print(stringInput);
out.close();

File file1 = new File("H.txt");


Scanner in = new Scanner(file1);
i = in.nextInt();
System.out.println(i + " * 2 = " + i*2);
}

}
import javax.swing.*;
import java.awt.*;

public class GridLayoutDemo extends JFrame


{
JButton jButton1 = new JButton("I am Button 1");
JButton jButton2 = new JButton("Button 2");
JButton jButton3 = new JButton("Button 3!!!!!!!!!");
JButton jButton4 = new JButton("I am Button 4");
JButton jButton5 = new JButton("I am Button 5");

public GridLayoutDemo()
{
setLayout(new GridLayout(3, 2));
add(jButton1);
add(jButton2);
add(jButton3);
add(jButton4);
add(jButton5);
}

public static void main(String[] args)


{
GridLayoutDemo frame = new GridLayoutDemo();
frame.setTitle("Using grid Layout");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Frame With 1 button
import javax.swing.*;

public class MyFrame extends JFrame


{
JButton jButton;

public MyFrame()
{
jButton = new JButton("Button 1!");
add(jButton);
}

public static void main(String[] args)


{
MyFrame frame = new MyFrame();
frame.setTitle("Using MyFrame");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Make new notepad file

import java.io.*;
import java.util.*;

public class FileWriterExample


{
public static void main(String[] args) throws Exception
{
File f = new File("gar.txt");
FileWriter out = new FileWriter(f, true);
out.write(104);
out.close();
}
}
import javax.swing.*;
import java.awt.*;

public class GUIExample extends JFrame


{
private JButton jbStart = new JButton("Start");
private JButton jbStop = new JButton("Stop");
private JButton jbNext = new JButton("Next");
private JTextArea jta = new JTextArea("Welcome to Programming 1");

public GUIExample()
{
JPanel panel = new JPanel();
panel.add(jbStart);
panel.add(jbStop);
panel.add(jbNext);
add(panel, BorderLayout.SOUTH);

add(jta);
}

public static void main(String [] args)


{
GUIExample f = new GUIExample();
f.setSize(400,300);
f.setTitle("An example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

You might also like