2013-08-05 69 views
-1

我的这段代码显示没有输出,也没有错误.. (它只显示小程序启动)。请帮助谢谢 可以在eclipse..whole代码运行它是included..Thank你非常多的帮助爪哇Swing密码

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

你错过了问题要问。请提出可由我们回答的问题。 –

+0

将类JPasswordA设置为抽象,请尝试以下代码行: - public abstract class JPasswordA extends JApplet implements ActionListener {...} –

回答

2

当混合小应用程序作为应用程序运行,则不会调用该方法main。因此,您需要添加JAppletinit方法中的所有组件。还要将组件添加到小程序本身,而不是单独的JFrame(网页中的自动弹出对话框从不受欢迎!)。

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); 
     } 

    } 

} 
0

frame.setVisible(true)应该frame.add(输入)的主要方法(小程序的init(),如果它是一个applet)

0

出于某种原因,版主删除我的回答,并没有解释原因后。

这一次:

如果启动它,因为它工作正常的日食Java应用程序。

你忘了重写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 

    } 

}