简体   繁体   中英

Return Value from one method to another

How do I get my inputted values from my main method to the create GUI method. I have create a frame and I have got the user to input a value in my main method. im now wanting the values that the user inputted to be displayed on the frame

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

/**
* This program displays an empty frame.
*/
public class SimpleFrame extends JFrame
{

 /**
 * The main launcher method
 */
 public static void main(String[] args)
 {
 SimpleFrame frame = new SimpleFrame();



    final int FRAME_WIDTH = 240;
    final int FRAME_HEIGHT = 360;
    final int FRAME_X = 150;
    final int FRAME_Y = 245;



    frame.setLocation(FRAME_X, FRAME_Y);
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setTitle("New Frame Title");

    frame.createGUI();
    frame.setVisible(true);

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer");

    int a = scan.nextInt();


    System.out.println(a);



}

/**
 * This method sets up the graphical user interface.
 */
private void createGUI()
{
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    window.setLayout(new FlowLayout());

    int a;

    // Nothing in the window yet!

Theres various ways you could do this, but by the looks of your code, you will only need to display one value (as you do not have a loop). You could put this value into a JLabel and call a function to update the text of the JLabel.

public void showValue(int a){
    label.setText(Integer.toString(a));
}

If this is too simple, look at Event Listeners as suggested in the comments

Example for updating label:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JLabel label = new JLabel();
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer");
    label.setText(String.valueOf(scan.nextInt()));
}

You can achieve something like this from the below given example , As soon as user Inputs the Value , the text Get Displayed in GUI : label

 Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer");
    while(youWantToTakeTheInput){  //Break this on some condtion of your choice
               int a = scan.nextInt();
               System.out.println(a);

              // Now call the below method with the reference of label 

               displayInputIntoMyGUI(label,a);
   }
   public void displayInputIntoMyGUI(label,a){
       label.setText(a); // This will replace the previous text
      // If you want to Append the Text then this is the way ,
       label.setText(label.getText() + "text u want to append");

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM