What is the difference between using Jpanel and JFrame (user interface)?

Asked 2 years ago, Updated 2 years ago, 35 views

In the following program, we have assembled the Rate3 class program as the inheritance class for JPanel, but we receive a compilation error.
I think the program is probably related to the installation of a menu bar on the frame, but I don't know more about the cause.
I would appreciate it if you could tell me what to do if it is an inheritance class for Jpanel or if it needs to be an inheritance class for JFrame.

compilation error

Rate3.java:30: Error: Cannot find symbol
    mbar.add(menu1); setJMenuBar(mbar);
                    ^
  Symbol: Method setJMenuBar (JMenuBar)
  Location: Class Rate 3
Rate3.java:103: Error: Symbol not found
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ^
  Symbol: Method setDefaultCloseOperation(int)
  Location: Class Rate 3
Rate3.java:105: Error: Symbol not found
    setTitle("Currency Exchange");
    ^
  Symbol: Method setTitle (String)
  Location: Class Rate 3
3 errors

source code

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

public class rate 3 extensions JPanel {
  intn;
  JMenuBar mbar = new JMenuBar();
  JTextField f0 = new JTextField("");
  JButton b0 = new JButton("Run");
 
  JLabel1 = new JLabel("Input a number and press the button.");
  JMenu menu1 = new JMenu("Please select Currency"); 
  JMenuItem01 = new JMenuItem("USD to JPY");
  JMenuItem item02 = new JMenuItem("CNY to JPY");
  JMenuItem03 = new JMenuItem("Euro to JPY");
  boolean a=false;
  boolean b=false; boolean c=false;
 
  

  publicRate3(){
    // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   // setTitle("Currency Exchange");
    setLayout (null);
    
    
    add(f0);f0.setBounds(100,50,50,30);
    add(b0);b0.setBounds(150,50,50,30);
    mbar.add(menu1); setJMenuBar(mbar);
    menu1.add(item01); menu1.add(item02); menu1.add(item03);
   
    add(l1); l1.setBounds (150, 140, 150, 100);
    //pack(); 
    setSize (400,300);
   
   

    item01.addActionListener(newActionListener(){
      public void actionPerformed (ActionEventevt) {
       
       a = true;
       b = false;
       c=false;


    }
});

    

    item02.addActionListener(newActionListener(){
      public void actionPerformed (ActionEventevt) {
        
        b = true;
        a=false;c=false;
        

      }
    });

    item03.addActionListener(newActionListener(){
      public void actionPerformed (ActionEventevt) {
        
        c=true;
        a=false;b=!c;
        

      }
    });
    
    b0.addActionListener(newActionListener(){
        public void actionPerformed (ActionEventevt) {
        if(a) {Double = Double.parseDouble(f0.getText());
        
            Dollar d1 = new Dollar(n);
           Double2 = d1.Ex();
            l1.setText(n+"USD is "+d2+"yen";}
        else if(b) {Double=Double.parseDouble(f0.getText()));
           
            CNY c1 = new CNY(n);
            Double c2 = c1.Ex2();
            
            l1.setText(n+"RMB is "+c2+"yen";}

        else if(c) {Double=Double.parseDouble(f0.getText()));
              
              CNY c3 = new CNY(n);
              Double c4 = c3.Ex3();
              
              l1.setText(n+"euro is "+c4+"yen";}
        
        else {return;} 
            
    
        }
      });
}


 public static void main(String[]args) arrows Exception {
    new Rate3().setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setTitle("Currency Exchange");
  }


  static class Dollar {
   intd = 100;
  
   Double; Double;

   public Dollar (Double n) {
     This.n = n;

   }

   public Double Ex() {
    return dl = d*n;
   }

  }

  static class CNY {
     Double cn;
     intc = 15;
     intd = 120;
     Double;
     public CNY (Double n) {
       This.n = n;
     }
     
     public Double Ex2() {
      return cn = c*n;
      
     }

     public Double Ex3() {
       return cn = d*n;
     }

  }
}

java

2022-09-30 19:54

1 Answers

Reading the Java API documentation often solves these basic questions.

However, in this case, it is about the Swing component, and it requires a little bit of prerequisite knowledge (so let me add a little bit of explanation).

Extended version of java.awt.Frame to add support for the JFC/Swing component architecture.

JPanel is a generic lightweight container.

The previous "prerequisite knowledge" would look something like this:

  • In addition to the GUI component "Swing", there must be a predecessor called "AWT"
  • General GUI component terminology

However, if you also read java.awt.Frame in the above quotation, you will be able to understand the difference between JFrame and JPanel to some extent.

In other words, JFrame is the same as Frame

Frame is a top-level window with a title and border

Yes. Conversely, JPanel is not a top-level window and does not have a title (or menu).

Incidentally, JPanel also has the corresponding AWT class Panel, so you can also refer to this API document.

Panel is the simplest container class. Panels provide applications with space to paste various components, such as other panels.

Title with compilation error, menu settings, etc. are access to top-level windows only, and JPanel does not have them.

Setting the content pane (ContentPane) of JPanel in JFrame may be the desired behavior.

From the previous JFame API document:

For task-oriented documentation on using JFrame, see How to Make Frames in The Java Tutorial.

JFrame retains JRootPane as its only child. In principle, the content pane provided by the root pane must contain all components except the menu that JFrame displays.

I am familiar with the tutorials in the quote above, but for the time being, if you modify the code in the questionnaire to a minimum, it will look like the following (Note: it is a minimal modification and it is a construction):

public class Rate3 extensions JFrame{

    // Rate 3 class equivalent of a questionnaire
    public class MyJPanel extensions JPanel {
        // ...
    }

    public static void main(String[]args) arrows Exception {
        Rate3 app = new Rate3();
        // Configuring JPanel in the Content Pane
        app.getContentPane().add(app.newMyJPanel());
        app.pack();
        app.setVisible(true);
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        app.setTitle("Current Exchange");
    }

    static class Dollar {
        // ...
    }

    static class CNY {
        // ...
    }
}

The full code is here.


2022-09-30 19:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.