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

A Comprehensive Evaluation of the Internal Consulting Process: Steps and Considerations

Introduction Internal consulting has emerged as a critical function within organizations, offering in-house expertise to solve complex business problems and drive change. It closely mirrors external consulting in methodology but is differentiated by the consultant's intimate knowledge of the organization and a vested interest in its long-term success. This article aims to evaluate the key steps involved in the internal consulting process, offering insights into each phase's significance and challenges. Steps in the Internal Consulting Process The internal consulting process can generally be segmented into five distinct stages: Initial Assessment, Data Collection and Analysis, Solution Development, Implementation, and Evaluation. Below is an evaluation of each step: Step 1: Initial Assessment Objective: To understand the problem or opportunity area and define the scope of the project. Significance: A well-defined scope ensures that the consulting project stays focused and manage...

The Evolving Landscape of Consulting Practice: Changes and Implications

Introduction Consulting is a field that thrives on its ability to adapt to market demands and emerging trends. As businesses evolve due to technological advancements, shifts in consumer behavior, and fluctuations in global markets, consulting practices must keep pace. This article explores some of the significant changes currently transforming the consulting industry and discusses their implications for both consultants and clients. Technological Disruption Data Analytics and Artificial Intelligence Consulting firms are increasingly integrating data analytics and artificial intelligence into their service offerings. These technologies allow consultants to offer data-driven insights that can significantly enhance strategic decision-making. This evolution means consultants now need skills in data interpretation and analysis, alongside their traditional expertise in business strategy. Virtual Consulting Platforms The advent of digital platforms enables consulting services to be offered re...

The Skillset of Internal Consultants: A Comparative Analysis

Introduction In the organizational landscape, the role of internal consultants has gained prominence due to the increasing complexity of business problems and the need for specialized in-house expertise. While many skills required for internal consulting overlap with those of external consultants, there are distinct abilities that set them apart. This article aims to compare and contrast these skill sets to provide a clearer understanding of what makes an effective internal consultant. Skills Common to Both Internal and External Consultants Problem-Solving Both types of consultants need to excel at identifying issues and creating viable solutions. Critical thinking and analytical skills are paramount for dissecting complex situations and recommending actionable strategies. Communication Excellent communication skills are a must for any consultant. Whether it’s making a presentation to stakeholders, writing a report, or simply discussing ideas with a team, effective communication is key...