Skip to main content

Swing Lab 1



  1. Create a new java project in Intellij. File → New → Project. You should see the following dialog. Click next.
  1. Select Create project from template and click next.
  1.  In the Project name field, type SwingIntro and click finish.
  1. Extend the Main class with JFrame class. Ensure that you import javax.swing.*;
public class Main extends JFrame {
  1. Modify the main method as shown below:
public static void main(String[] args) {
   java.awt.EventQueue.invokeLater(new Runnable() {
       @Override
       public void run() {
           new Main();
       }
   });
}
  1. In the Main class constructor add the following coding
                  setVisible(true);
  1. Run the program. You should be able to see the following window.
  1. We have not added any component yet, but you can maximise, minimise. resize and close the window.
  2. Close the window. Notice that even after closing the window, the application is still running. Let’s fix this by adding the following coding in the Main class constructor and run the application again.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  1. Let’s add a title to our frame and set the width and height of the frame using the following code to the Main class constructor and run the program.
setTitle("My first swing app");
setSize(500,300);
  1. We have a frame, now let's add a canvas. We will use a Panel object as canvas and set the background color as blue. Then add the panel to frame. You will have to create an attribute called panel of type JPanel. Run the program.
panel  = new JPanel();
panel.setBackground(Color.BLUE);
add(panel);
  1. Now that we have a panel, let’s add some component to the panel. Let's start by creating a button and adding it to the panel. Add the following code and run the program.
btn1 = new JButton();
btn1.setText("Click Me");
panel.add(btn1);
  1. Notice that nothing happens when you click on the button. We need a listener to listen and respond to the events related to the button. To add a mouse listener add the following code to the constructor.
btn1.addMouseListener(new java.awt.event.MouseAdapter() {
   @Override
   public void mouseClicked(java.awt.event.MouseEvent ae){
       btn1MouseClicked(ae);
   }
});
  1. Above codes adds a new instance of MouseAdapter as the listener. mouseClicked method is overridden to call the method that would run when the mouse is clicked.
  2. Add a method called btn1MouseClicked to Main class. This method will print “Hello World!” to console. Run the application. The application should now print “Hello World!” to the console, every time you click “click me” button.
  3. Now let’s add more components the panel. Add a JLabel to the panel. Set the text of the label to “test”. Run the program.
  4. Modify the btn1.MouseClicked() to set the text of the label to “Hello World!” whenever the button is clicked.
  5. Code a java swing project to create a simplified calculator the can be used for addition. The calculator will have a label, textfield and a button as shown below:




5.0



Add



Comments

Popular posts from this blog

CUMIPMT and CUMPRINC function

CUMIPMT Cumulative interest payment function allows you to calculate the interest paid for a loan or from an investment from period A to period B. When getting a loan, CUMIPMT function can be used to calculate the total amount of interest paid in the first five months or from period 12 to period 20. A period can be a month, a week or two week. Loan Amount : 350,000.00 APR: 4.5% Down payment: 0.00 Years: 25 Payment per year: 12 From the above data, we can calculate the following: No of Period: 25 × 12 = 300 Periodic Rate: 4.5/12 = 0.375% Here is how you will substitute these values into the function. = CUMIPMT (periodic rate, No of period, vehicle price, start period, end period,  ) = CUMIPMT (0.375, 300, 350000, 1, 5, 0) In an excel worksheet, we use cell address instead of actual values as shown below: Here is the formula view of the worksheet: CUMPRINC Another related function is CUMPRINC. CUMPRINC function is used to calculate cumul

Excel PMT Function

PMT function is very useful for calculating monthly payment required to payback a loan or mortgage at a fixed rate. This function require a minimum of three inputs, periodic rate, number of periods, present value or the loan amount. Here is a simple example. Home Loan: 350,000.00 Interest rate: 4.5% Number of years to repay the loan: 25 Note: To calculate monthly payment, we need to find the monthly rate and number of months as shown above. Then it is simply a matter of substituting the values into the payment function, as shown in the formula view below.

BCG's Brand Advocacy Index

The Boston Consulting Group's (BCG) Brand Advocacy Index (BAI) is a metric developed to help companies measure the degree of customer advocacy for their brands. BAI focuses on the likelihood of customers to recommend a brand to others, which is a powerful indicator of brand strength and customer loyalty. Unlike other customer satisfaction or loyalty metrics, BAI emphasizes the importance of customer referrals and word-of-mouth marketing. BAI is calculated based on a survey where customers are asked about their willingness to recommend a brand to their friends, family, or colleagues. The responses are then used to compute a score, which ranges from -100 to 100. A higher BAI score indicates that a brand has more advocates who are likely to recommend the brand to others, while a lower score suggests that the brand has fewer advocates or even a higher number of detractors. BCG's research has shown that companies with higher BAI scores tend to experience higher growth rates and bett