Advertisement
enkacang

LAB TASK 1 - IPT

Oct 19th, 2022
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. /*
  2. Name : MUHAMMAD HILMI BIN KAMARUL'AZMI
  3. Registration Number : 01DDT20F1122
  4. Class : KHAS5
  5.  */
  6.  
  7. import java.awt.*;
  8. import javax.swing.*;
  9.  
  10. public class LT1_01DDT20F1122 extends JFrame {
  11.     private int _xSizePanel = 300;
  12.     private int _ySizePanel = 30;
  13.     private int _xIncrement = 30;
  14.  
  15.     private String _days = "1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31";
  16.     private String _months = "January-February-March-April-May-June-July-August-September-October-November-December";
  17.     private String _years = "2022-2023-2024";
  18.  
  19.     public LT1_01DDT20F1122(){
  20.          // 1 - Title
  21.         JPanel panelLabelTitle = this.addLabel("HOTEL PAK JAGO", 0);
  22.         JLabel lbl = (JLabel) panelLabelTitle.getComponent(0);
  23.         lbl.setFont(new Font("Serif", 1, 30));
  24.         lbl.setForeground(Color.BLUE);
  25.         this.add(panelLabelTitle);
  26.        
  27.         // 2 - Form Name
  28.         JPanel panelFormName = this.addPanelForm("Name :",0);
  29.         this.add(panelFormName);
  30.        
  31.         // 3- Form Mobile
  32.         JPanel panelFormMobile = this.addPanelForm("Mobile Number :",0);
  33.         this.add(panelFormMobile);
  34.        
  35.         // 4 - Form Email
  36.         JPanel panelFormEmail = this.addPanelForm("Email :",0);
  37.         this.add(panelFormEmail);
  38.        
  39.         // 5 - Date Combo Box
  40.         JPanel panelComboBoxDate = this.addPanelComboBox("Booking Date", String.format("%s/%s/%s ", _days, _months, _years), 0);
  41.         this.add(panelComboBoxDate);
  42.         JPanel panelComboBoxNumber = this.addPanelComboBox("Person", String.format("1-2-3-4-5-6-7-8-9 Max"), 0);
  43.         this.add(panelComboBoxNumber);
  44.        
  45.         // 6 - Images
  46.         ImageIcon imageIcon = new ImageIcon("C:\\Users\\User\\Downloads\\room.png");
  47.         Image image = imageIcon.getImage();
  48.         Image newimg = image.getScaledInstance(450, 120,  java.awt.Image.SCALE_SMOOTH);
  49.         imageIcon = new ImageIcon(newimg);  
  50.         JLabel picLabel = new JLabel(imageIcon);
  51.         this.add(picLabel);
  52.        
  53.         // 7 - Radio Button
  54.         String roomList = "Single Room-RM103-33 Room left!/Double Room-RM250-13 Room left!/Family Room-RM650-12 Room left!/Deluxe Room-RM450-4 Room left!";
  55.         JPanel panelRadioButton = this.addPanelRadioButton("",roomList, 0);
  56.         panelRadioButton.setLayout(new BoxLayout(panelRadioButton,BoxLayout.X_AXIS));
  57.         panelRadioButton.setBorder(BorderFactory.createTitledBorder("Room List"));
  58.         this.add(panelRadioButton);
  59.  
  60.  
  61.         //============================= USING BoxLayout inside this.JFrame FlowLayout =============================
  62.         JPanel boxPanel = new JPanel();
  63.         boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.Y_AXIS));
  64.            
  65.         //8 - TextArea Comment 
  66.         JPanel panelForm3 = this.addTextArea("Comment :", 0);
  67.         this.add(panelForm3);
  68.        
  69.         // 9 - Check for agreement
  70.         JPanel panelCheckBoxAgreement = this.addPanelCheckButton("","Accept Terms and Conditions",0);
  71.         boxPanel.add(panelCheckBoxAgreement);
  72.        
  73.         //10 - button
  74.         JPanel panelAddButton = this.addPanelButton("SUBMIT",0);
  75.         JButton jbtn = (JButton)panelAddButton.getComponent(0);
  76.         boxPanel.add(panelAddButton);
  77.        
  78.         this.add(boxPanel);
  79.  
  80.         //============================= INITIALIZATION =============================
  81.         this.endInitialization();
  82.     }
  83.    
  84.     private void endInitialization() {
  85.         this.setTitle("BOOKING HOTEL PAK JAGO THE GREAT");
  86.         this.setLayout(new FlowLayout());
  87.         this.setDefaultCloseOperation(3);
  88.         this.setSize(500, 650);
  89.         this.getContentPane().setBackground(new Color(255, 252, 187));
  90.         this.setResizable(false);
  91.         this.setVisible(true);
  92.     }
  93.    
  94.     //============================= REUSEABLE METHOD =============================
  95.     private JPanel addLabel(String name, int xPosition) {
  96.         JPanel panel = new JPanel();
  97.         JLabel label = new JLabel(name);
  98.         panel.add(label);
  99.         this.setPanel(panel, xPosition);
  100.         return panel;
  101.     }
  102.    
  103.     private JPanel addTextArea(String name, int xPosition) {
  104.         JPanel panel = new JPanel();
  105.         JLabel label = new JLabel(name);
  106.         panel.add(label);
  107.         JTextArea area = new JTextArea("", 5, 31);
  108.         panel.add(area);
  109.         panel.setBackground(new Color(255, 252, 187));
  110.         return panel;
  111.     }
  112.    
  113.     private JPanel addPanelForm(String name, int xPosition) {
  114.         JPanel panel = new JPanel();
  115.         JLabel label = new JLabel(name);
  116.         panel.add(label);
  117.         JTextField textField = new JTextField(20);
  118.         panel.add(textField);
  119.         this.setPanel(panel, xPosition);
  120.         return panel;
  121.     }
  122.    
  123.     private JPanel addPanelRadioButton(String name, String buttonList, int xPosition) {
  124.         String[] split = buttonList.split("/");
  125.         JPanel panel = new JPanel();
  126.         JLabel label = new JLabel(name);
  127.         panel.add(label);
  128.         ButtonGroup bg = new ButtonGroup();
  129.         for (String item: split) {
  130.             String[] split2 = item.split("-");
  131.             JRadioButton r1 = new JRadioButton("<html>" + split2[0] + "<br/>" + split2[1] + "<br/>" + split2[2] + "</html>");
  132.             bg.add(r1);
  133.             r1.setBackground(new Color(255, 252, 187));
  134.             panel.add(r1);
  135.         }
  136.         this.setPanel(panel, xPosition);
  137.         return panel;
  138.     }
  139.    
  140.     private JPanel addPanelComboBox(String name, String comboBoxList, int xPosition)
  141.     {
  142.         String[] split = comboBoxList.split("/");
  143.         JPanel panel = new JPanel();
  144.         JLabel label = new JLabel(name);
  145.         panel.add(label);
  146.            
  147.         for (String item: split) {
  148.             String[] item2 = item.split("-");
  149.             JComboBox cb=new JComboBox(item2);
  150.             panel.add(cb);
  151.         }
  152.            
  153.         panel.setLayout(new FlowLayout());
  154.         return panel;
  155.     }
  156.    
  157.     private JPanel addPanelCheckButton(String name, String buttonList, int xPosition) {
  158.        
  159.         String[] split = buttonList.split("/");
  160.         JPanel panel = new JPanel();
  161.         JLabel label = new JLabel(name);
  162.         panel.add(label);
  163.        
  164.         for (String item: split) {
  165.             JCheckBox checkBox = new JCheckBox(item);
  166.             checkBox.setBackground(new Color(255, 252, 187));
  167.             panel.add(checkBox);
  168.         }
  169.        
  170.         panel.setBackground(new Color(255, 252, 187));
  171.         panel.setBounds(0,xPosition * _xIncrement, _xSizePanel, 60);
  172.         panel.setLayout(new FlowLayout());
  173.         return panel;
  174.     }
  175.    
  176.     private JPanel addPanelButton(String buttonList, int xPosition) {
  177.         JPanel panel = new JPanel();
  178.         String[] split = buttonList.split("-");
  179.         for (String item: split) {
  180.             JButton button = new JButton(item);
  181.             panel.add(button);
  182.         }
  183.         this.setPanel(panel, xPosition);
  184.         return panel;
  185.     }
  186.    
  187.     private void setPanel(JPanel panel, int xPosition) {
  188.         panel.setBackground(new Color(255, 252, 187));
  189.         panel.setBounds(0,
  190.             xPosition * _xIncrement, _xSizePanel, _ySizePanel);
  191.         panel.setLayout(new FlowLayout());
  192.     }
  193.    
  194.     //============================= MAIN METHOD =============================
  195.     public static void main(String args[]) {
  196.         new LT1_01DDT20F1122();
  197.     }
  198.    
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement