Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. package test1;
  2.  
  3. import java.awt.Container;
  4. import java.awt.FlowLayout;
  5. import javax.swing.*;
  6.  
  7. public class Test1{
  8.  
  9. //Should components be final? Should they be defined outside of the class?
  10. private final JTextArea outputArea = new JTextArea();
  11. private final JTextField errorReportField = new JTextField();
  12.  
  13. private final JPanel inputPanel = new JPanel();
  14.  
  15. private final JLabel nameLabel = new JLabel("Item Name");
  16. private final JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
  17. private final JLabel priceLabel = new JLabel("Price per unit (or L) in pence");
  18.  
  19. private final JTextField nameField = new JTextField(10);
  20. private final JTextField numberField = new JTextField(10);
  21. private final JTextField priceField = new JTextField(10);
  22.  
  23. private final JButton addVolumeButton = new JButton("Add by Volume");
  24. private final JButton addNumberButton = new JButton("Add by number of units");
  25.  
  26. public Test1() {
  27. JFrame frame = new JFrame("Fuel station shop");
  28. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.  
  30. outputArea.setEditable(false);
  31. outputArea.setRows(30);
  32. JScrollPane scrollPanel = new JScrollPane(outputArea);
  33. scrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  34. errorReportField.setEditable(false);
  35.  
  36. //Better way of adding multiple components to panel?
  37. inputPanel.setLayout(new FlowLayout());
  38. inputPanel.add(nameLabel);
  39. inputPanel.add(nameField);
  40. inputPanel.add(numberLabel);
  41. inputPanel.add(numberField);
  42. inputPanel.add(priceLabel);
  43. inputPanel.add(priceField);
  44. inputPanel.add(addVolumeButton);
  45. inputPanel.add(addNumberButton);
  46.  
  47. Container contentPane = frame.getContentPane();
  48. //Why is it adding components from bottom to top?
  49. contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
  50. contentPane.add(scrollPanel);
  51. contentPane.add(errorReportField);
  52. contentPane.add(inputPanel);
  53.  
  54. frame.pack();
  55. frame.setVisible(true);
  56. }
  57.  
  58. public static void main(String[] args) {
  59. Test1 test = new Test1();
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement