Home > other >  Java GUI Frame creation compilation error?
Java GUI Frame creation compilation error?

Time:01-21

I'm working on simple Java GUI Frame creation with buttons, it should show the frame with two buttons "OK" and "Clear" respectively. And two lines for writing the First Name and Last Name. I'm using VS Code editor. Following code gives compilation error, any help would be appreciated:

 import java.awt.*;
 import javax.swing.*;
 import javafx.event.ActionEvent;

      public class MainFrame extends JFrame{
           final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
           JTextField tfFirstName, tfLastName;
           JLabel lbWelcome;

      public void initialize(){
    /********** Form Panel ***************/
    JLabel lbFirstName = new JLabel("First Name");
    lbFirstName.setFont(mainFont);

    tfFirstName = new JTextField();
    tfFirstName.setFont(mainFont);

    JLabel lbLastName = new JLabel("Last Name");
    lbLastName.setFont(mainFont);

    tfLastName = new JTextField();
    tfLastName.setFont(mainFont);

    JPanel formPanel = new JPanel();
    formPanel.setLayout(new GridLayout(4, 1, 5, 5));
    formPanel.add(lbFirstName);
    formPanel.add(tfFirstName);
    formPanel.add(lbLastName);
    formPanel.add(tfLastName);

    /********** Welcome Label ***************/
    lbWelcome = new JLabel();
    lbWelcome.setFont(mainFont);

    /********** Button Panel ***************/
    JButton btnOK = new JButton("OK");
    btnOK.setFont(mainFont);
    btnOK.addActionListener(new ActionListener(){
          
        @Override
        public void actionPerformed(ActionEvent e){
            // TODO Auto-generated method stub
            String firstName = tfFirstName.getText();
            String lastName = tfLastName.getText();
            lbWelcome.setText("Hello "   firstName   " "   lastName);
        }
    });

    JButton btnClear = new JButton("Clear");
    btnClear.setFont(mainFont);
    btnClear.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e){
            tfFirstName.setText("");
            tfLastName.setText("");
            lbWelcome.setText("");
        }
    });

    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BorderLayout());
    mainPanel.setBackground(new Color(128, 128, 255));
    mainPanel.add(formPanel, BorderLayout.NORTH);
    mainPanel.add(lbWelcome, BorderLayout.CENTER);
    mainPanel.add(buttonsPanel, BorderLayout.SOUTH);

    add(mainPanel);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.setBackground(new Color(128, 128, 255));
    mainPanel.add(formPanel, BorderLayout.NORTH);
    mainPanel.add(lbWelcome, BorderLayout.CENTER);

    setTitle("Welcome");
    setSize(500, 600);
    setMinimumSize(new Dimension(300, 400));
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setVisible(true);
}

public static void main(String[] args) {
    MainFrame myFrame = new MainFrame();
    myFrame.initialize();
}
}

Compilation error message:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at JavaProjectTest.src.MainFrame.main(MainFrame.java:83)

Output result after fix: enter image description here

CodePudding user response:

There are two main problems:

(1) import javafx.event.ActionEvent; should be changed to import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

(2) The order of JPanel mainPanel = new JPanel(); should be before using mainPanel.setBackground(new Color(128, 128, 255));.

import javax.swing.*;
import java.awt.*;

//REMOVE THIS
//import javafx.event.ActionEvent;

//ADD THIS
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame {
    final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
    JTexThere are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import jThere are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.ava.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.tField tfFirstName, tfLastName;
    JLabel lbWelcome;

    public void initialize(){
        /********** Form Panel ***************/
        JLabel lbFirstName = new JLabel("First Name");
        lbFirstName.setFont(mainFont);

        There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.There are two main problems, (1) `import javafx.event.ActionEvent;` should be changed to `import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;` (2) The order of `JPanel mainPanel = new JPanel();` should be before using `mainPanel.setBackground(new Color(128, 128, 255));`.tfFirstName = new JTextField();
        tfFirstName.setFont(mainFont);

        JLabel lbLastName = new JLabel("Last Name");
        lbLastName.setFont(mainFont);

        tfLastName = new JTextField();
        tfLastName.setFont(mainFont);

        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(4, 1, 5, 5));
        formPanel.add(lbFirstName);
        formPanel.add(tfFirstName);
        formPanel.add(lbLastName);
        formPanel.add(tfLastName);

        /********** Welcome Label ***************/
        lbWelcome = new JLabel();
        lbWelcome.setFont(mainFont);

        /********** Button Panel ***************/
        JButton btnOK = new JButton("OK");
        btnOK.setFont(mainFont);
        btnOK.addActionListener(new ActionListener(){

            @Overrideimport javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame {
    final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
    JTextField tfFirstName, tfLastName;
    JLabel lbWelcome;

    public void initialize(){
        /********** Form Panel ***************/
        JLabel lbFirstName = new JLabel("First Name");
        lbFirstName.setFont(mainFont);

        tfFirstName = new JTextField();
        tfFirstName.setFont(mainFont);

        JLabel lbLastName = new JLabel("Last Name");
        lbLastName.setFont(mainFont);

        tfLastName = new JTextField();
        tfLastName.setFont(mainFont);

        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(4, 1, 5, 5));
        formPanel.add(lbFirstName);
        formPanel.add(tfFirstName);
        formPanel.add(lbLastName);
        formPanel.add(tfLastName);

        /********** Welcome Label ***************/
        lbWelcome = new JLabel();
        lbWelcome.setFont(mainFont);

        /********** Button Panel ***************/
        JButton btnOK = new JButton("OK");
        btnOK.setFont(mainFont);
        btnOK.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e){
                // TODO Auto-generated method stub
                String firstName = tfFirstName.getText();
                String lastName = tfLastName.getText();
                lbWelcome.setText("Hello "   firstName   " "   lastName);
            }
        });

        JButton btnClear = new JButton("Clear");
        btnClear.setFont(mainFont);
        btnClear.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e){
                tfFirstName.setText("");
                tfLastName.setText("");
                lbWelcome.setText("");
            }
        });

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new BorderLayout());

//MOVE SEQUENCE JPanel mainPanel = new JPanel();
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(new Color(128, 128, 255));
        mainPanel.add(formPanel, BorderLayout.NORTH);
        mainPanel.add(lbWelcome, BorderLayout.CENTER);
        mainPanel.setBackground(new Color(128, 128, 255));
        mainPanel.add(formPanel, BorderLayout.NORTH);
        mainPanel.add(lbWelcome, BorderLayout.CENTER);
        mainPanel.add(buttonsPanel, BorderLayout.SOUTH);

        add(mainPanel);



        setTitle("Welcome");
        setSize(500, 600);
        setMinimumSize(new Dimension(300, 400));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        MainFrame myFrame = new MainFrame();
        myFrame.initialize();
    }
}

            public void actionPerformed(ActionEvent e){
                // TODO Auto-generated method stub
                String firstName = tfFirstName.getText();
                String lastName = tfLastName.getText();
                lbWelcome.setText("Hello "   firstName   " "   lastName);
            }
        });

        JButton btnClear = new JButton("Clear");
        btnClear.setFont(mainFont);
        btnClear.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e){
                tfFirstName.setText("");
                tfLastName.setText("");
                lbWelcome.setText("");
            }
        });

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new BorderLayout());

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(new Color(128, 128, 255));
        mainPanel.add(formPanel, BorderLayout.NORTH);
        mainPanel.add(lbWelcome, BorderLayout.CENTER);
        mainPanel.setBackground(new Color(128, 128, 255));
        mainPanel.add(formPanel, BorderLayout.NORTH);
        mainPanel.add(lbWelcome, BorderLayout.CENTER);
        mainPanel.add(buttonsPanel, BorderLayout.SOUTH);

        add(mainPanel);



        setTitle("Welcome");
        setSize(500, 600);
        setMinimumSize(new Dimension(300, 400));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        MainFrame myFrame = new MainFrame();
        myFrame.initialize();
    }
}
  •  Tags:  
  • java
  • Related