-1

我的这段代码没有显示输出,也没有错误..(它只是显示小程序已启动)。请帮助谢谢你可以在eclipse中运行它..包含整个代码..非常感谢你的帮助

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


public class JPasswordA extends JApplet implements ActionListener {

    static JLabel passwordLabel = new JLabel("Please enter your password:");
    static JTextField input = new JTextField(20);
    static JButton enter = new JButton("Submit");
    static Font bigFont = new Font("Arial", Font.BOLD, 16);
    final static int WIDTH = 465;
    final static int HEIGHT = 150;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setLayout(new FlowLayout());
        frame.setSize(WIDTH, HEIGHT);
        frame.setLayout(new FlowLayout());
        frame.add(passwordLabel);
        passwordLabel.setFont(bigFont);
        frame.add(input);
        frame.add(enter);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        enter.addActionListener(new Action());
    }

    static class Action implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String inputPassword = input.getText();
            String passWord = "Rosebud";
            if (inputPassword.equals(passWord)) {
                JOptionPane.showMessageDialog(null,
                        "               Your password is              correct,"
                        + "\n                     You may proceed.",
                        "Password Correct",
                        JOptionPane.PLAIN_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null,
                        "Sorry, you have entered an incorrect password,"
                        + "\n       Make sure your CAPS are not locked.",
                        "Password Incorrect",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
4

4 回答 4

2

main当混合小程序作为应用程序运行时,不会调用该方法。因此,您需要在代替init方法中添加所有组件。JApplet还将组件添加到小程序本身,而不是单独的JFrame(来自网页的自动弹出对话框永远不会流行!)。

于 2013-08-05T12:41:18.953 回答
0

frame.setVisible(true)应该在 main 方法中的 frame.add(enter) 之后(Applet init() 如果它是一个小程序)

于 2013-08-05T12:50:09.747 回答
0

由于某种原因,版主删除了我的答案,并没有解释原因。

这里又是:

如果您将它作为 Java 应用程序启动,它在 Eclipse 上运行良好。

您忘记覆盖 init 方法而只使用 main 代替。

在下面找到工作代码:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;

public class JPasswordA extends JApplet implements ActionListener {

    static JLabel passwordLabel = new JLabel("Please enter your password:");
    static JTextField input = new JTextField(20);
    static JButton enter = new JButton("Submit");
    static Font bigFont = new Font("Arial", Font.BOLD, 16);
    final static int WIDTH = 465;
    final static int HEIGHT = 150;

    @Override
    public void init() {
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setLayout(new FlowLayout());
        frame.setBackground(Color.red);

        frame.setSize(WIDTH, HEIGHT);
        frame.setLayout(new FlowLayout());
        frame.add(passwordLabel);
        passwordLabel.setFont(bigFont);
        frame.add(input);
        frame.add(enter);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        enter.addActionListener(new Action());
    }

    static class Action implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            String inputPassword = input.getText();
            String passWord = "Rosebud";
            if (inputPassword.equals(passWord)) {
                JOptionPane
                        .showMessageDialog(
                                null,
                                "               Your password is               correct,\n                     You may proceed.",
                                "Password Correct", JOptionPane.PLAIN_MESSAGE);
            } else {
                JOptionPane
                        .showMessageDialog(
                                null,
                                "Sorry, you have entered an incorrect password,\n       Make sure your CAPS are not locked.",
                                "Password Incorrect", JOptionPane.ERROR_MESSAGE);
            }

        }
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }

}
于 2013-08-05T12:50:45.640 回答
0

要运行小程序,请创建一个 html 文件,例如 -

<html>
    <title>Password Applet</title>  
    <applet code="JPasswordA.class" width="465" height="150"></applet>  
</html>

您没有正确实施 init 。我稍微更改了代码。

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

public class JPasswordA extends JApplet implements ActionListener {

    private final static JLabel passwordLabel = new JLabel("Please enter your password:");
    private final static JTextField input = new JTextField(20);
    private final static JButton enter = new JButton("Submit");
    private final static Font bigFont = new Font("Arial", Font.BOLD, 16);
    private final static int WIDTH = 465;
    private final static int HEIGHT = 150;

    public void init(){

        setVisible(true); 
        setLayout(new FlowLayout());    
        setSize(WIDTH, HEIGHT);
        setLayout(new FlowLayout());
        add(passwordLabel);
        passwordLabel.setFont(bigFont);
        add(input);
        add(enter);
        enter.addActionListener(this);

    }             

    @Override
    public void actionPerformed(ActionEvent e) { 

        String inputPassword = input.getText();
        String passWord = "Rosebud";

        if(inputPassword.equals(passWord)) {
            JOptionPane.showMessageDialog(null, "Your password is correct,\nYou may proceed.", "Password Correct", JOptionPane.PLAIN_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "Sorry, you have entered an incorrect password,\nMake sure your CAPS are not locked.", "Password Incorrect", JOptionPane.ERROR_MESSAGE);
        }

    }

}
于 2013-08-05T12:46:08.083 回答