有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在另一个方法中使用一个方法的返回语句?

我想通过将返回语句传递到showWindow来调用load_picture中的返回语句tempimage,但我不确定如何调用。这是我的一段代码。编辑: 我想我想说的是,我不确定如何处理硬编码的“picture1.gif”。我知道我需要调用一个方法来加载图像,但我不太确定应该用什么来代替它。 :

package project3; 

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

public class Project3 {
    //initializing global
static Project3 theobject = new Project3();
final static int MIN_NUMBER=1; 
final static int MAX_NUMBER=8; 
static int image_number=1;
static Image theimage;
// This routine will load an image into memory, non-static requires an object
// It expects the name of the image file name and a JFrame  passed to it
// It will assume an Internet conection is available
// It can only be called AFTER the program object has been created
// It will return a type Image variable, call it like this:   theimage = object.load_picture("picture1.gif", frame); 
// (hard code 'picture1.gif' only when testing - USE a method or variable for 'real' call)
// This code requires you to do an 'import java.awt.*' and an 'import java.net.*'
// Note: this method is using parameter and return type for input/output

// This routine will load an image into memory, non-static requires an object
// It expects the name of the image file name and a JFrame  passed to it
// It will assume an Internet conection is available
// It can only be called AFTER the program object has been created
// It will return a type Image variable, call it like this:   theimage = object.load_picture("picture1.gif", frame); 
// (hard code 'picture1.gif' only when testing - USE a method or variable for 'real' call)
// This code requires you to do an 'import java.awt.*' and an 'import java.net.*'
// Note: this method is using parameter and return type for input/output

public Image load_picture(String imagefile, JFrame theframe)
{

Image tempimage;
// Create a MediaTracker to inform us when the image has
// been completely loaded.
MediaTracker tracker;
tracker = new MediaTracker(theframe);

// getImage() returns immediately. The image is not
// actually loaded until it is first used. We use a
// MediaTracker to make sure the image is loaded
// before we try to display it.

String startURL;
if (imagefile.startsWith("http"))
   startURL = "";
else
   startURL = "http://www.canyons.edu/departments/comp_sci/ferguson/cs111/images/";

URL myURL=null;
try
{
myURL = new URL(startURL + imagefile);
}
catch(MalformedURLException e) {
  System.out.println("Error caught " + e.toString());
}

//tempimage = getImage(myURL);   // JApplet version
tempimage = Toolkit.getDefaultToolkit().getImage(myURL); // stand alone program version


// Add the image to the MediaTracker so that we can wait for it

tracker.addImage(tempimage, 0);
try { tracker.waitForID(0); }
catch ( InterruptedException err) { System.err.println(err); }

return tempimage;
}

// This class/method uses a  global variable that MUST be set before calling/using
// note: You can not call the paint routine directly, it is called when frame/window is shown
// look up the repaint() routine in the book
// Review Listings 8.5 and 8.6
//
public static class MyPanel extends JPanel {
 public void paintComponent (Graphics g) {
     JPanel panel= new JPanel();
    int xpos,ypos;
    super.paintComponent(g);
    // set the xpos and ypos before you display the image
    xpos = 300; // you pick the position
    ypos = 200; // you pick the position
    if (theimage != null) {
        g.drawImage(theimage,xpos,ypos,this);
        // note: theimage global variable must be set BEFORE paint is called
    }
 }
}
public static void showWindow( String filename ) {
    // create, size and show a GUI window frame, you may need to click on taskbar to see window
    //display the filename in the title of the window frame, otherwise the window will be blank (for now)

JFrame frame1= new JFrame();
theimage = theobject.load_picture("picture1.gif", frame1);
//"picture1.gif" is hardcoded, I want to call this using a method
frame1.setTitle(filename);
frame1.setSize(440,302);
frame1.setLocation(400,302);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}

感谢您的帮助。谢谢


共 (2) 个答案

  1. # 1 楼答案

    load_picture方法返回的值可以直接发送到showWindow方法,也可以将其分配给变量:

    String filename = "your/filename";
    JFrame theFrame = new JFrame();
    Project3 project = new Project3();
    MyPanel.showWindow(project.load_picture(filename, theFrame);
    
  2. # 2 楼答案

    在showWindow方法中,只需调用load_picture方法,如下所示:

    Image tempImage = load_picture(filename, frame1);
    

    从这里,您可以对tempImage对象执行任何您喜欢的操作