1

ログインページを表示するアプレットを作成しました。アプレットのコードは次のとおりです。

import java.net.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * Class login - write a description of the class here
 * 
 * @author (your name) 
 * @version (a version number)
 */

public class login extends JApplet

    implements ActionListener
{
    // instance variables - replace the example below with your own

    private JTextField input;
    private JTextField pass;
    private JPasswordField pwd;
    private final String LOGIN = "LOGIN";
    private final String PASSWORD = "PASSWORD";
    private final String CLEAR = "CLEAR";
    private final String Fgt_pwd="Forgot Password";
    private final String REGISTER="REGISTER";
     /**
     * Called by the browser or applet viewer to inform this JApplet that it
     * has been loaded into the system. It is always called before the first 
     * time that the start method is called.
     */
    public void init()
    {
        // provide any initialisation necessary for your JApplet
    }


    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if(LOGIN.equals(command))
        {
            input.setText("Login Successful");
           try{
               URL url = new URL(getDocumentBase(), "feedback.html");
               getAppletContext().showDocument(url);
            }catch(MalformedURLException ex){
                showStatus("URL not found");
            }

        }    
        else if(CLEAR.equals(command))
            input.setText("");
        else if(Fgt_pwd.equals(command))
            input.setText("Redirecting to Forgot Password Page");
        else if(REGISTER.equals(command))
            input.setText("Redirecting to Registration Page");
    }

    /**
     * Called by the browser or applet viewer to inform this JApplet that it 
     * should start its execution. It is called after the init method and 
     * each time the JApplet is revisited in a Web page. 
     */
    public void start()
    {
        // provide any code requred to run each time 
        // web page is visited
         // this is a workaround for a security conflict with some browsers
        // including some versions of Netscape & Internet Explorer which do 
        // not allow access to the AWT system event queue which JApplets do 
        // on startup to check access. May not be necessary with your browser.
        Container contentPane = getContentPane();
        JScrollPane sPane = new JScrollPane();
        JPanel pContPanel = new JPanel();

        pContPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints(6, 5, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,10,10,10), 20, 20);


        Font myFont = new Font("Ariel",Font.BOLD,22);

        JLabel mtitle = new JLabel("WELCOME TO D.A.S.H.");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        pContPanel.add(mtitle, gbc);
        mtitle.setFont(myFont);

        //Adding the title
        JLabel title = new JLabel("SIGN IN TO YOUR ACCOUNT");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        pContPanel.add(title, gbc);

        //Create upper left part for Login ID
        JPanel panel1 = new JPanel();
        JLabel prompt = new JLabel("Enter Login ID");
        panel1.add(prompt);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        pContPanel.add(panel1, gbc); 
       // prompt.setAlignmentX(Component.LEFT_ALIGNMENT);


        //adding the textfield for username
        input = new JTextField(15);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        pContPanel.add(input, gbc); 


        //adding the label for password
        JPanel passPanel=new JPanel();
        JLabel passLabel = new JLabel("Enter Password");
        passPanel.add(passLabel);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        pContPanel.add(passPanel, gbc);


        //adding the password field
        pwd=new JPasswordField(15);
        pwd.setEchoChar('*');
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        pContPanel.add(pwd, gbc);

        JLabel rtitle = new JLabel("NEW USER? CLICK HERE TO REGISTER.");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 3;
        pContPanel.add(rtitle, gbc); 


        //Now create button for login
        JButton bLogin = new JButton(LOGIN);
        bLogin.addActionListener(this);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        pContPanel.add(bLogin, gbc);


        //now create button for clear
        JButton bPassword = new JButton(CLEAR);
        bPassword.addActionListener(this);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        pContPanel.add(bPassword, gbc);


        //now create button for forgot password
        JButton bForgotPwd = new JButton(Fgt_pwd);
        bForgotPwd.addActionListener(this);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 2;
        gbc.gridy = 4;
        pContPanel.add(bForgotPwd, gbc);

        JButton bRegister = new JButton(REGISTER);
        bRegister.addActionListener(this);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.gridwidth = 1;
        pContPanel.add(bRegister, gbc);


       //adding the panel to the scrollpane 
       sPane.setViewportView(pContPanel);
       //adding scrollpane to the ContentPane
       contentPane.add(sPane, BorderLayout.CENTER);

    }

今のところ何も入っていないペイントメソッドもあります。コードの出力は完璧です。上記のコードに paint メソッドを追加して背景色を追加しようとすると、次のようになります。

public void paint(Graphics g)
    {

        // simple text displayed on applet
        setBackground(Color.blue);
    }

私が見ている出力は、最初に表示されていた他のすべてのボタンとテキストフィールドを隠している(appletviewerの)背景が青色のウィンドウです。ブラウザで出力すると、背景色のない元の出力が表示されます。

アプレットの見栄えを良くするために、グラフィック (背景とテキストの色、場合によっては画像) を追加する方法を教えてください。(大学のミニプロジェクトとして提出する必要があります)。

4

0 に答える 0