从textfield java applet获取url

时间:2016-02-05 04:51:28

标签: java applet

我正在编写一个基本的Java Applet幻灯片,我正在尝试设置一个文本字段,用户可以在其中粘贴图像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的文本字段和用于执行操作的“加载”按钮。

屏幕截图文字字段和操作按钮

screenshot textfield and button for action

任何提示?

0 个答案:

没有答案