Java Applet背景颜色

时间:2012-09-21 11:52:11

标签: java applet background-color

我创建了一个显示登录页面的Applet。 Applet的代码如下:

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中),隐藏了最初显示的所有其他按钮和文本字段。如果我在浏览器中输出,它会显示没有任何背景颜色的原始输出。

你可以帮我添加一些图形(背景和文字颜色,可能是图像),以便applet看起来不错吗? (我必须将其作为大学迷你项目提交)。

0 个答案:

没有答案