Guest User

Untitled

a guest
Jul 28th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. import java.awt.Color;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.File;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.awt.event.*;
  9.  
  10. import javax.imageio.ImageIO;
  11. import javax.swing.*;
  12.  
  13. import sun.awt.image.PixelConverter.Bgrx;
  14.  
  15. import javax.xml.parsers.*;
  16. import javax.xml.transform.*;
  17. import javax.xml.transform.dom.*;
  18. import javax.xml.transform.stream.*;
  19.  
  20. import org.xml.sax.*;
  21. import org.w3c.dom.*;
  22.  
  23.  
  24. public class Layout1 {
  25.  
  26. public static void main(String[] args) {
  27.  
  28.  
  29. //DECLARATION OF A FRAME
  30. JFrame f1 = new JFrame ("LOGIN PAGE");
  31. JLabel gender = new JLabel("Gender : ");
  32. gender.setBounds(100, 150, 70, 40);
  33. f1.add(gender);
  34.  
  35. final JRadioButton rb1 = new JRadioButton("Male");
  36. final JRadioButton rb2 = new JRadioButton("Female");
  37. rb1.setBounds(150,150,70,40);
  38. rb2.setBounds(150,180,70,40);
  39.  
  40.  
  41. ButtonGroup bg=new ButtonGroup();
  42. bg.add(rb1);bg.add(rb2);
  43.  
  44.  
  45.  
  46. //COMBO BOX COUNTRY
  47. String country [] = {"Select Country..","Iran","Afghanistan","America","Germany","Pakistan","Spain","France"};
  48.  
  49. JLabel place = new JLabel("Country : ");
  50. place.setBounds(90, 220, 120, 20);
  51. f1.add(place);
  52.  
  53. final JComboBox cb = new JComboBox(country);
  54. cb.setBounds(150,220,120,20);
  55. f1.add(cb);
  56.  
  57. //COMBO BOX HEIGHT
  58.  
  59. JLabel length = new JLabel("Height : ");
  60. length.setBounds(100, 250, 100, 20);
  61. f1.add(length);
  62.  
  63. final JTextField height = new JTextField();
  64. height.setBounds(150, 250, 100, 20);
  65. f1.add(height);
  66.  
  67.  
  68. //COMBO BOX AGE
  69. JLabel old = new JLabel("Age : ");
  70. old.setBounds(100, 280, 100, 20);
  71. f1.add(old);
  72.  
  73. final JTextField age = new JTextField();
  74. age.setBounds(150, 280, 100, 20);
  75. f1.add(age);
  76.  
  77.  
  78. //COMBO BOX NATIONALITY
  79. String nationality [] = {"Select Nationality","Irani","Afghani","American","German","Pakistani","Spanish","French"};
  80. JLabel nation = new JLabel("Nationality : ");
  81. nation.setBounds(80, 320, 100, 20);
  82. f1.add(nation);
  83.  
  84. final JComboBox cb3 = new JComboBox(nationality);
  85. cb3.setBounds(150, 320, 100, 20);
  86. f1.add(cb3);
  87.  
  88.  
  89.  
  90. //SAVE BUTTON WORK
  91. JButton save = new JButton ("Save ");
  92. save.setBackground(Color.CYAN);
  93. save.setBounds(150,350,100,30);
  94.  
  95. //LOAD BUTTON WORK
  96. JButton load = new JButton ("Load ");
  97. load.setBackground(Color.CYAN);
  98. load.setBounds(150,400,100,30);
  99.  
  100.  
  101. //TEXTFIELD WORK
  102.  
  103. JLabel name = new JLabel("Name : ");
  104. name.setBounds(100, 120, 200, 30);
  105. f1.add(name);
  106. final JTextField box = new JTextField();
  107. box.setBounds(150, 120, 200, 30);
  108. f1.add(box);
  109.  
  110. //ACTION EVENTS WORK
  111. save.addActionListener(new ActionListener() {
  112.  
  113. @Override
  114. public void actionPerformed(ActionEvent e) {
  115.  
  116. FileWriter file = null;
  117. try {
  118. file = new FileWriter("C:\\Users\\Home\\Desktop\\Brother.txt");
  119.  
  120. //NAME TEXTFIELD WORK
  121. try {
  122. box.getText();
  123.  
  124. } catch (Exception e2) {
  125. System.out.println("There is an error in Name TextField");
  126. }
  127. final String data1 = box.getText();
  128. if(box.getText().isEmpty()){
  129. JOptionPane.showMessageDialog(null,
  130. "Error you did not enter a name, please try again.",
  131. "Error", JOptionPane.ERROR_MESSAGE);
  132.  
  133. }
  134.  
  135. try {
  136. file.write(" Name : " + data1+"\n");
  137. } catch (IOException e3) {
  138. e3.printStackTrace();
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. //AGE TEXTFIELD WORK
  147. try {
  148. age.getText();
  149.  
  150. }
  151. catch (Exception e2) {
  152. System.out.println("There is and error in Age TextField");
  153. }
  154.  
  155. final String data4 = age.getText();
  156. if(age.getText().isEmpty()){
  157. JOptionPane.showMessageDialog(null,
  158. "Error you did not enter an age, please try again.",
  159. "Error", JOptionPane.ERROR_MESSAGE);
  160. }
  161.  
  162. /* Integer.parseInt(data4);
  163. int a = Integer.parseInt(data4);
  164.  
  165. if ((a >= 18)&&(a <= 100)) {
  166. */
  167. try {
  168. file.write("Age : " + data4);
  169. } catch (IOException e1) {
  170. e1.printStackTrace();
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. //HEIGHT TEXTFIELD WORK
  179.  
  180.  
  181. try {
  182. height.getText();
  183.  
  184. }
  185. catch (Exception e2) {
  186. System.out.println("There is an error in Height TextField");
  187. }
  188. final String data5 = height.getText();
  189.  
  190. if(height.getText().isEmpty()){
  191. JOptionPane.showMessageDialog(null,
  192. "Error you did not enter a height, please try again.",
  193. "Error", JOptionPane.ERROR_MESSAGE);
  194. }
  195. /*
  196. Double.parseDouble(data5);
  197. double b = Double.parseDouble(data5);
  198.  
  199. if((b >= 3 )&&(b <= 10)){ */
  200. try {
  201. file.write("Height : " + data5);
  202. } catch (IOException e1) {
  203.  
  204. e1.printStackTrace();
  205. }
  206.  
  207.  
  208.  
  209.  
  210.  
  211. //try {
  212.  
  213.  
  214. //STORE COMBO BOX DATA OF COUNTRY NAME
  215.  
  216. try{
  217. cb.getSelectedItem();
  218. }
  219. catch(Exception e2){
  220. System.out.println("Error in Country Name");
  221. }
  222.  
  223. final String data2 = (String)cb.getSelectedItem();
  224. file.append(" Country : " + "\n" + data2+"\n");
  225.  
  226.  
  227. //STORE COMBO BOX DATA OF NATIONALITY
  228.  
  229. try{
  230.  
  231. cb.getSelectedItem();
  232. { final String data3 = (String)cb.getSelectedItem();
  233. file.write("Nationality : " + data3);
  234. }
  235.  
  236.  
  237.  
  238.  
  239. }
  240. catch(Exception e2){
  241. JOptionPane.showMessageDialog(null,
  242. "Error you did not enter a nationality, please try again.",
  243. "Error", JOptionPane.ERROR_MESSAGE);
  244.  
  245. }
  246.  
  247.  
  248.  
  249.  
  250.  
  251. //STORE RADIO BUTTON DATA
  252.  
  253. try{
  254. if(rb1.isSelected()==true)
  255. file.append(" Gender : " + "Male ");
  256. else if(rb2.isSelected()==true)
  257. file.append(" Gender : " + "Female ");
  258. else
  259. throw new Exception();
  260. }
  261.  
  262. catch(Exception e2){
  263. JOptionPane.showMessageDialog(null, "Enter Gender", "try again", JOptionPane.ERROR_MESSAGE);
  264. }
  265.  
  266. file.close();
  267.  
  268. }
  269. catch (IOException e1) {
  270. System.err.println("Error occured in writing file");
  271. e1.printStackTrace();
  272. }
  273.  
  274.  
  275. }
  276. });
  277.  
  278.  
  279.  
  280.  
  281.  
  282. f1.add(load);
  283. f1.add(rb1);
  284. f1.add(rb2);
  285. f1.add(save);
  286. f1.setLayout(null);
  287. f1.setVisible(true);
  288. f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  289. f1.setSize(500,680);
  290. f1.setResizable(false);
  291. f1.setLocation(440, 10);
  292.  
  293.  
  294.  
  295.  
  296.  
  297. }
  298. }
Add Comment
Please, Sign In to add comment