Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从textfield java小程序获取url_Java_Applet - Fatal编程技术网

从textfield java小程序获取url

从textfield java小程序获取url,java,applet,Java,Applet,我正在制作一个基本的java小程序幻灯片,我正在尝试设置一个文本字段,用户可以在其中粘贴一个图像URL,该图像显示在幻灯片上(也保存在本地) 我被困在如何获取文本字段(URL)中插入的值,并在显示和保存图像的代码段中使用它 下面是我正在处理的小程序代码 package javaapplication7; import java.awt.*; import java.applet.*; import java.awt.event.*; //===========================

我正在制作一个基本的java小程序幻灯片,我正在尝试设置一个文本字段,用户可以在其中粘贴一个图像URL,该图像显示在幻灯片上(也保存在本地)

我被困在如何获取文本字段(URL)中插入的值,并在显示和保存图像的代码段中使用它

下面是我正在处理的小程序代码

package javaapplication7;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

//==================================================================
// This applet presents a slide show of images.
//==================================================================

public class SlideShow extends Applet {

    private AudioClip sound1;

    private SlideShowGUI gui;

//-----------------------------------------------------------
// Creates the GUI.
//-----------------------------------------------------------

    public void init () {
       gui = new SlideShowGUI (this);

            //-----------------------------------------------------------
            // Making the audioclip playing in loop
            //-----------------------------------------------------------
            sound1 = getAudioClip(getCodeBase(), "sound1.wav");
            sound1.play();
            sound1.loop();
} // method init

} // class SlideShow


//==================================================================
// The GUI is made up of two primary panels, one for the images
// and one for the slide show controls.
//==================================================================

class SlideShowGUI {

    private ImagePanel imagePanel;

    private ControlPanel controlPanel;

//-----------------------------------------------------------
// Sets up the two primary panels.
//-----------------------------------------------------------

public SlideShowGUI (SlideShow app) {

    app.setLayout (new BorderLayout());
    app.setBackground (Color.white);
    imagePanel = new ImagePanel(app);
    app.add (imagePanel, "Center");
    controlPanel = new ControlPanel(imagePanel);
    app.add (controlPanel, "South");
    app.setSize (700, 500);
    } // constructor SlideShowGUI
} // class SlideShowGUI

//==================================================================
// The center panel contains the slide images.
//==================================================================

class ImagePanel extends Panel {

private CardLayout ourCardLayout;
private SlideShow applet;
private Slide s1, s2, s3;
//-----------------------------------------------------------
// Creates the slides and adds them to the card layout.
//-----------------------------------------------------------

public ImagePanel (SlideShow applet) {
    this.applet = applet;
    ourCardLayout = new CardLayout();
    setLayout (ourCardLayout);
    s1 = new Slide (applet, "pic1.jpg");
    s2 = new Slide (applet, "pic2.jpg");
    s3 = new Slide (applet, "pic3.jpg");
    add (s1, "Pic1");
    add (s2, "Pic2");
    add (s3, "Pic3");
    ourCardLayout.first (this);
    setSize (700, 500);
} // constructor ImagePanel

//-----------------------------------------------------------
// Moves to the previous slide.
//-----------------------------------------------------------

public void goBack() {

    ourCardLayout.previous (this);
} // method goBack


//-----------------------------------------------------------
// Moves to the next slide.
//-----------------------------------------------------------
public void goForward() {

    ourCardLayout.next (this);
} // method goForward
} // class ImagePanel

//==================================================================
// Respresents one slide in the slide show.
//==================================================================
class Slide extends Canvas {
    SlideShow applet;
    Image image;
    TextField inputBox;
//-----------------------------------------------------------
// Gets the image.
//-----------------------------------------------------------

public Slide (SlideShow applet, String filename) {
     image = applet.getImage (applet.getDocumentBase(), filename);
     setSize (700, 500);
} // constructor Slide

//-----------------------------------------------------------
// Draws the image.
//-----------------------------------------------------------               

public void paint (Graphics page) {
    page.drawImage (image, 0, 0, 700, 500, this);

    String url = inputBox.getText();


    } // method paint
} // class Slide

//==================================================================
// Creates the control panel of buttons.
//==================================================================
class ControlPanel extends Panel implements ActionListener{
    private static ImagePanel imagePanel;
    private static Button backButton;
    private static Button forwardButton;
    private static TextField inputBox;
    private static Button loadButton;
//-----------------------------------------------------------
// Sets up all buttons and creates the player thread.
//-----------------------------------------------------------
public ControlPanel(ImagePanel imagePanel) {

    this.imagePanel = imagePanel;
    setBackground (Color.red);
    backButton = new Button ("Previous image");
    backButton.addActionListener (this);
    forwardButton = new Button ("Next image");
    forwardButton.addActionListener (this);
    add (backButton);
    add (forwardButton);

    inputBox = new TextField("insert URL",40);
    add (inputBox);
    loadButton = new Button("Load");
    loadButton.addActionListener(this); 
    add (loadButton);
} // constructor ControlPanel

//-----------------------------------------------------------
// The control panel also serves as the listener for all
// buttons. This method is invoked when any button is
// pressed.
//-----------------------------------------------------------
public void actionPerformed (ActionEvent event) {

    Object obj = event.getSource();
    if (obj == backButton)
    imagePanel.goBack();
    else if (obj == forwardButton)
    imagePanel.goForward();        

} // method actionPerformed


} // class ControlPanel
这是用来检索和保存图像的代码

try {

        URL url = new URL("http://www.iki.rssi.ru/IPL/show14.gif");
        image = ImageIO.read(url); 
        ImageIO.write(image, "gif",new File("C:/xampp/htdocs/img/show14.gif"));


    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Done");
} 


public void paint(Graphics g) {  

  g.drawImage(picture, 20 ,30, this);  

}
因此,在底部的幻灯片窗口中,有一个用于粘贴URL的文本字段和一个用于执行操作的“加载”按钮

用于操作的屏幕截图文本字段和按钮


有什么提示吗?

试试inputBox.getText()1)为什么要编写小程序?如果是老师指定的,请参考。2) 为什么要使用AWT?有关放弃AWT使用组件而支持Swing的许多好理由,请参阅。另一方面,为了从另一个站点加载资源(如图像),小程序需要具有
所有权限!谢谢