Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*; //for listeners
  4.  
  5. public class QuoteGUI implements ActionListener
  6. {
  7. static JFrame mainWindow = new JFrame("Quick Cars Quote Generator"); //create main window
  8.  
  9. JTabbedPane systemTabs = new JTabbedPane(); //make the tabs
  10.  
  11. JPanel loginPanel = new JPanel(); //making the panels that will fill the tab
  12. JPanel quotesPanel = new JPanel();
  13.  
  14. JLabel loginLabel = new JLabel("Quick Cars"); //this is under the first tab, loginPanel
  15.  
  16. JLabel loginTitleLabel = new JLabel("Enter Username and Password to log in:"); //this is the title of the tab, loginpanel
  17.  
  18. JLabel usernameLabel = new JLabel("Username");
  19. JLabel passwordLabel = new JLabel("Password");
  20. JTextField username = new JTextField("Username"); //this is the textfield where the user enters their username
  21. JTextField password = new JTextField("Password"); //this is the textfield where the user enters their password
  22. JButton login = new JButton("Login"); //this is the button that logs the user in
  23.  
  24.  
  25. JLabel quotesLabel = new JLabel("Quick Cars"); //this is under the second tab, quotesPanel
  26. JLabel quotesTitleLabel = new JLabel("Options");
  27. JLabel quotesCustomerLabel = new JLabel("Enter Customer Data");
  28. JLabel quotesCarLabel = new JLabel("Enter Car Data");
  29. JLabel quotesPaymentLabel = new JLabel("Enter Loan Data");
  30.  
  31. JButton logout = new JButton("Logout"); //this will log the user out
  32.  
  33. public void runBasicGUI()
  34. {
  35. //start of generic GUI code
  36. Toolkit theKit = mainWindow.getToolkit();
  37. Dimension wndSize=theKit.getScreenSize();
  38.  
  39. mainWindow.setBounds(10, 10, wndSize.width/2, 1000); //set positions then dimensions
  40. mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //tells the program to end when the X is pressed
  41.  
  42. GridLayout grid = new GridLayout(1,1);
  43. Container content = mainWindow.getContentPane();
  44. content.setLayout(grid); //end of generic GUI code
  45.  
  46. createloginPanel(); //calls the method that creates the panels
  47. createquotesPanel();
  48.  
  49. systemTabs.addTab("Login", loginPanel); //adds the panels to the tabs
  50. systemTabs.addTab("Quotes", quotesPanel);
  51.  
  52. systemTabs.setSelectedIndex(0); //sets the tab to be selected
  53. systemTabs.setEnabledAt(1,false); //disables the tab
  54.  
  55. content.add(systemTabs); //adds the tabs to the GUI
  56. mainWindow.setVisible(true); //makes the window appear
  57. }
  58.  
  59. public void createloginPanel()
  60. {
  61. loginPanel.setLayout(null);
  62. loginLabel.setBounds(250, 10, 500, 40); //sets the bounds of the various components: x pos, y pos, x size, y size
  63. loginTitleLabel.setBounds(250, 100, 500, 30);
  64. usernameLabel.setBounds(150, 150, 100, 30);
  65. username.setBounds(250, 150, 200, 30);
  66. passwordLabel.setBounds(150, 200, 100, 30);
  67. password.setBounds(250, 200, 200, 30);
  68. login.setBounds(250, 250, 100, 25);
  69. login.addActionListener(this); //add a listener to the button
  70.  
  71. loginPanel.add(loginLabel); //adds the components to the panel
  72. loginPanel.add(loginTitleLabel);
  73. loginPanel.add(usernameLabel);
  74. loginPanel.add(username);
  75. loginPanel.add(passwordLabel);
  76. loginPanel.add(password);
  77. loginPanel.add(login);
  78. }
  79.  
  80. public void createquotesPanel()
  81. {
  82. quotesPanel.setLayout(null);
  83. quotesLabel.setBounds(250, 25, 500, 25);
  84. quotesTitleLabel.setBounds(20, 100, 500, 25);
  85. quotesCustomerLabel.setBounds(20, 300, 500, 25);
  86. quotesCarLabel.setBounds(20, 500, 500, 25);
  87. quotesPaymentLabel.setBounds(20, 700, 500, 25);
  88.  
  89. logout.setBounds(450, 10, 100, 25);
  90. logout.addActionListener(this); //adds a listener to the button
  91.  
  92. quotesPanel.add(quotesLabel);
  93. quotesPanel.add(logout);
  94. quotesPanel.add(quotesTitleLabel);
  95. quotesPanel.add(quotesCustomerLabel);
  96. quotesPanel.add(quotesCarLabel);
  97. quotesPanel.add(quotesPaymentLabel);
  98. }
  99.  
  100. public void actionPerformed(ActionEvent e)
  101. {
  102. if(e.getSource() == login)
  103. {
  104. String un = username.getText();
  105. String pw = password.getText();
  106.  
  107. if (un.equals("quickcars") && pw.equals("12345"))
  108. {
  109. systemTabs.setEnabledAt(1,true);
  110. systemTabs.setSelectedIndex(1);
  111. systemTabs.setEnabledAt(0, false);
  112. }
  113. }
  114.  
  115. if (e.getSource() == logout)
  116. {
  117. systemTabs.setEnabledAt(0, true);
  118. systemTabs.setEnabledAt(1, false);
  119. systemTabs.setSelectedIndex(0);
  120. }
  121. }
  122.  
  123. public static void main(String[] args)
  124. {
  125. QuoteGUI qg = new QuoteGUI();
  126. qg.runBasicGUI();
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement