Advertisement
om_gan

java gui

Feb 5th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. //Import packages
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. //Main class
  7. public class gui1{
  8. //Declare variables
  9. static JFrame frame1;
  10. static Container pane;
  11. static JButton btnConnect, btnDisconnect;
  12. static JLabel lblServer, lblUsername, lblPassword, lblPort;
  13. static JTextField txtServer, txtUsername, txtPassword, txtPort;
  14. static Insets insets;
  15.  
  16. public static void main (String args[]){
  17. //Set Look and Feel
  18. try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
  19. catch (ClassNotFoundException e) {}
  20. catch (InstantiationException e) {}
  21. catch (IllegalAccessException e) {}
  22. catch (UnsupportedLookAndFeelException e) {}
  23.  
  24. //Create the frame
  25. frame1 = new JFrame ("Sample GUI Application");
  26. frame1.setSize (800,200);
  27. pane = frame1.getContentPane();
  28. insets = pane.getInsets();
  29. pane.setLayout (null);
  30.  
  31. //Create controls
  32. btnConnect = new JButton ("Connect");
  33. btnDisconnect = new JButton ("Disconnect");
  34. lblServer = new JLabel ("Remote host:");
  35. lblUsername = new JLabel ("Username:");
  36. lblPassword = new JLabel ("Password:");
  37. lblPort = new JLabel ("Port #:");
  38. txtServer = new JTextField (10);
  39. txtUsername = new JTextField (10);
  40. txtPassword = new JTextField (10);
  41. txtPort = new JTextField (5);
  42.  
  43. //Add all components to panel
  44. pane.add (lblServer);
  45. pane.add (lblUsername);
  46. pane.add (lblPassword);
  47. pane.add (lblPort);
  48. pane.add (txtServer);
  49. pane.add (txtUsername);
  50. pane.add (txtPassword);
  51. pane.add (txtPort);
  52. pane.add (btnConnect);
  53. pane.add (btnDisconnect);
  54.  
  55. //Place all components
  56. lblServer.setBounds (insets.left + 5, insets.top + 5, lblServer.getPreferredSize().width, lblServer.getPreferredSize().height);
  57. txtServer.setBounds (lblServer.getX() + lblServer.getWidth() + 5, insets.top + 5, txtServer.getPreferredSize().width, txtServer.getPreferredSize().height);
  58. lblUsername.setBounds (txtServer.getX() + txtServer.getWidth() + 5, insets.top + 5, lblUsername.getPreferredSize().width, lblUsername.getPreferredSize().height);
  59. txtUsername.setBounds (lblUsername.getX() + lblUsername.getWidth() + 5, insets.top + 5, txtUsername.getPreferredSize().width, txtUsername.getPreferredSize().height);
  60. lblPassword.setBounds (txtUsername.getX() + txtUsername.getWidth() + 5, insets.top + 5, lblPassword.getPreferredSize().width, lblPassword.getPreferredSize().height);
  61. txtPassword.setBounds (lblPassword.getX() + lblPassword.getWidth() + 5, insets.top + 5, txtPassword.getPreferredSize().width, txtPassword.getPreferredSize().height);
  62. lblPort.setBounds (txtPassword.getX() + txtPassword.getWidth() + 5, insets.top + 5, lblPort.getPreferredSize().width, lblPort.getPreferredSize().height);
  63. txtPort.setBounds (lblPort.getX() + lblPort.getWidth() + 5, insets.top + 5, txtPort.getPreferredSize().width, txtPort.getPreferredSize().height);
  64. btnConnect.setBounds (txtPort.getX() + txtPort.getWidth() + 5, insets.top + 5, btnConnect.getPreferredSize().width, btnConnect.getPreferredSize().height);
  65. btnDisconnect.setBounds (insets.left + 15, lblServer.getY() + lblServer.getHeight() + 5, btnDisconnect.getPreferredSize().width, btnDisconnect.getPreferredSize().height);
  66.  
  67. //Set frame visible
  68. frame1.setVisible (true);
  69.  
  70. //Button's action
  71. btnConnect.addActionListener(new btnConnectAction()); //Register action
  72. }
  73.  
  74. public static class btnConnectAction implements ActionListener{
  75. public void actionPerformed (ActionEvent e){
  76. System.out.println("You entered "+txtUsername.getText());
  77. }
  78. }
  79. }
  80. -================-
  81.  
  82. import java.awt.*;
  83. import javax.swing.*;
  84.  
  85. public class RectangleProgram extends JFrame
  86. {
  87. private static final int WIDTH = 400;
  88. private static final int HEIGHT = 300;
  89.  
  90. private JLabel lengthL, widthL, areaL, perimeterL;
  91. private JTextField lengthTF, widthTF, areaTF, perimeterTF;
  92. private JButton calculateB, exitB;
  93.  
  94. public RectangleProgram()
  95. {
  96. //Instantiate the labels:
  97. lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
  98. widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
  99. areaL = new JLabel("Area: ", SwingConstants.RIGHT);
  100. perimeterL = new JLabel("Perimeter: ", SwingConstants.RIGHT);
  101.  
  102. //Text fields next:
  103. lengthTF = new JTextField(10);
  104. widthTF = new JTextField(10);
  105. areaTF = new JTextField(10);
  106. perimeterTF = new JTextField(10);
  107.  
  108. //Buttons too:
  109. calculateB = new JButton("Calculate");
  110. exitB = new JButton("Exit");
  111.  
  112. //Set the window's title.
  113. setTitle("Sample Title: Area of a Rectangle");
  114.  
  115. //Get the content pane (CP).
  116. Container pane = getContentPane();
  117.  
  118. //Set the layout.
  119. pane.setLayout(new GridLayout(5, 2));
  120.  
  121. //Other JFrame stuff.
  122. setSize(WIDTH, HEIGHT);
  123. setVisible(true);
  124. setDefaultCloseOperation(EXIT_ON_CLOSE);
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement