Explain the usage of JFrame with example

Asked By 10 points N/A Posted on -
qa-featured

What is the function of JFrame in Java and how to declare JFrames? Can you give example of it? Thanks

SHARE
Best Answer by Sheldon Ron
Best Answer
Best Answer
Answered By 0 points N/A #103611

Explain the usage of JFrame with example

qa-featured

 

An example is illustrated below about a frame which holds two panels and that panels hold some Swing components. Try this out in your main function.
frame = new JFrame("Display image");
      panel = new ShowImage();
      panel2=new Panel();
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(500, 1000);
      frame.setVisible(true);
      lblmain = new JLabel(new ImageIcon(image) );
      panel.add(lblmain);
      cmdSegment= new javax.swing.JButton();
      cmdSegment.setText("Segment");
      cmdSegment.setVisible(true);
      panel.add(cmdSegment);
      delimiter=new JSlider();
      delimiter.setValue(40);
      panel.add(delimiter);
      lbldel = new JLabel();
      panel.add(lbldel);
      panel.add(panel2);
      txtMsg=new javax.swing.JTextArea();
      txtMsg.setMaximumSize(new java.awt.Dimension(200, 200));
      txtMsg.setMinimumSize(new java.awt.Dimension(200, 200));
      panel.add(txtMsg);
      
      cmdSegment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try{
                    cmdSegmentOnClick(evt);
                }catch(Exception e){
                    txtMsg.setText(txtMsg.getText()+"n"+e.getMessage()+e.getStackTrace());
                }
            }
        });
        delimiter.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                try{
                    delimiterChange(evt);
                }catch(Exception e){
                    txtMsg.setText(txtMsg.getText()+"n"+e.getMessage()+e.getStackTrace());
                }
            }
        });
      txtMsg.setText("Output Messages………………………………………………………………………………………………………………");
      lbldel.setText(Integer.toString(delimiter.getValue()));
      label=new JLabel();
      panel2.add(label);
An explanation,
All you need to do is to create a frame then insert panel to the frame. then you can create objects of command, check, textbox and others to add to the panel. the order on which you add the controls give the display in the frame window.
Thanks. Please provide a feedback.
 
Answered By 563020 points N/A #103612

Explain the usage of JFrame with example

qa-featured

The JFrame is the foremost window used to display the components that needs to be displayed on the screen. It is directly related to Swing which is the chief Java graphical user interface widget toolkit. It is a component of JFC or the Oracle’s Java Foundation Classes. Swing was designed to give a more complicated set of graphical user interface components than the previous AWT or Abstract Window Toolkit. The best way of explaining the use of JFrame is through example.

import javax.swing.*;
// Importing the package that contains all the Swing Components and Classes.

public class FrameExample {

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        //Create and set up the frame.
        //The string passed as an argument will be displayed
        //as the title.

        JFrame frame = new JFrame("[=] Hello World [=]");

        //Display the window.
        frame.setSize(400, 100);
        frame.setVisible(true);
    } 
}

And here’s another one.

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class SwingExample implements Runnable {

    @Override
    public void run() {
        // Create the window
        JFrame f = new JFrame("Hello, World!");
        // Sets the behavior for when the window is closed
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Add a layout manager so that the button is not placed on top of the label
        f.setLayout(new FlowLayout());
        // Add a label and a button
        f.add(new JLabel("Hello, world!"));
        f.add(new JButton("Press me!"));
        // Arrange the components inside the window
        f.pack();
        // By default, the window is not visible. Make it visible.
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingExample se = new SwingExample();
        // Schedules the application to be run at the correct time in the event queue.
        SwingUtilities.invokeLater(se);
    }

}

Login/Register to Answer

Related Questions