程序终止而不在eclipse中运行

时间:2015-07-05 15:36:49

标签: java eclipse jframe

当我运行此代码(具有简单按钮和带有动作侦听器的LOGIN按钮)时,它会在没有运行且没有显示屏幕的情况下终止。

我试过System.exit(0);主要功能是克服这个终止问题,但都是徒劳的

   public class HOme extends JFrame{

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
Color cardinal = new Color(194, 35, 38);
int w=155;
int h=50;
public HOme(String title) {

    super(title);

    getContentPane().setSize(width,height);
    getContentPane().setBackground(Color.WHITE);
    getContentPane().setLayout(null);


    final JPanel panel2 = new JPanel();
    panel2.setBounds(364, 33, 664, 344);
    getContentPane().add(panel2);

    JPanel panel3 = new JPanel();
    panel3.setBackground(Color.WHITE);
    panel3.setBounds(81, 382, 947, 243);
    getContentPane().add(panel3);
    panel3.setLayout(null);

    JButton btnHome = new JButton("Home");
    btnHome.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnHome.setForeground(Color.WHITE);
    btnHome.setBackground(cardinal);
    btnHome.setBounds(517, 33, w, h);
    btnHome.setContentAreaFilled(false);
    btnHome.setOpaque(true);
    panel3.add(btnHome);

    JButton btnClients = new JButton("Clients");
    btnClients.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnClients.setForeground(Color.WHITE);
    btnClients.setBounds(690, 33, w, h);
    btnClients.setBackground(cardinal);
    btnClients.setContentAreaFilled(false);
    btnClients.setOpaque(true);
    panel3.add(btnClients);

    JButton btnClose = new JButton("Close");
    btnClose.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnClose.setForeground(Color.WHITE);
    btnClose.setBounds(690, 198, w, h);
    btnClose.setBackground(cardinal);
    btnClose.setContentAreaFilled(false);
    btnClose.setOpaque(true);
    panel3.add(btnClose);

    JButton btnLogin = new JButton("Admin Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Login l=new Login();
            panel2.add(l);
        }
    });
    btnLogin.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    btnLogin.setForeground(Color.WHITE);
    btnLogin.setBounds(517, 116, w, h);
    btnLogin.setBackground(cardinal);
    btnLogin.setContentAreaFilled(false);
    btnLogin.setOpaque(true);
    panel3.add(btnLogin);

    JPanel panel1 = new JPanel();
    panel1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(204,       51, 0), null));
    panel1.setBackground(Color.WHITE);
    panel1.setBounds(81, 33, 263, 344);
    getContentPane().add(panel1);
    panel1.setLayout(null);


    JButton btnStartMonitoring = new JButton("");
    btnStartMonitoring.setIcon(new ImageIcon(path1));
    btnStartMonitoring.setBackground(cardinal);

    btnStartMonitoring.setForeground(Color.WHITE);
    btnStartMonitoring.setFont(new Font("Tahoma", Font.PLAIN, 15));
    btnStartMonitoring.setBounds(10, 274, 239, 59);

    panel1.add(btnStartMonitoring);

    JLabel lblLogo = new JLabel("New label");
    lblLogo.setIcon(new ImageIcon(path2));
    lblLogo.setBounds(0, 11, 263, 253);
    panel1.add(lblLogo);

}


public static void main(String args[]) {

      new HOme("HOme");
        //System.exit(0);
}

}

被修改

我有一个从JPanel扩展的登录类。当我从Home点击登录按钮时。它没有显示“登录”面板 Login.class

   public class Login extends JPanel {
   private JTextField txtPassword;
   private JTextField txtID;
   Color cardinal = new Color(194, 35, 38);
   int w=155;
   int h=50;
   public Login() {
    setBackground(Color.WHITE);
setLayout(null);

JLabel lblLogin = new JLabel("Login   ");
lblLogin.setBackground(Color.ORANGE);
lblLogin.setHorizontalAlignment(SwingConstants.RIGHT);
lblLogin.setFont(new Font("Trajan Pro", Font.BOLD, 36));
lblLogin.setBounds(125, 0, 424, 59);
lblLogin.setBackground(cardinal);
//lblLogin.setContentAreaFilled(false);
lblLogin.setOpaque(true);
lblLogin.setForeground(Color.white);
add(lblLogin);

JLabel lblId = new JLabel("ID");
lblId.setHorizontalAlignment(SwingConstants.RIGHT);
lblId.setFont(new Font("Tekton Pro", Font.PLAIN, 23));
lblId.setBounds(181, 127, 66, 28);
add(lblId);

JLabel lblPassword = new JLabel("Password");
lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
lblPassword.setFont(new Font("Tekton Pro", Font.PLAIN, 23));
lblPassword.setBounds(136, 188, 111, 28);
add(lblPassword);

txtPassword = new JTextField();
lblPassword.setLabelFor(txtPassword);
txtPassword.setBounds(266, 183, 256, 41);
lblPassword.setForeground(cardinal);
add(txtPassword);
txtPassword.setColumns(10);

txtID = new JTextField();
lblId.setLabelFor(txtID);
txtID.setBounds(266, 123, 256, 39);
lblId.setForeground(cardinal);
add(txtID);
txtID.setColumns(10);

JButton btnLogin = new JButton("Login");
btnLogin.setForeground(Color.WHITE);
btnLogin.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnLogin.setBounds(324, 294, w, h);
btnLogin.setBackground(cardinal);
btnLogin.setContentAreaFilled(false);
btnLogin.setOpaque(true);
add(btnLogin);
setVisible(true);

}

3 个答案:

答案 0 :(得分:1)

您没有让JFrame可见。

你可以做任何一个 -

  1. 在构造函数中,通过在末尾添加以下行使其可见 -

    setVisible(true);
    
  2. 或者在main()功能中,您可以这样做 -

    HOme h = new HOme("HOme");
    h.setVisible(true);
    

答案 1 :(得分:0)

setVisible(true);方法的末尾添加HOme之类的内容。

答案 2 :(得分:0)

回答第二部分:

  1. 导入MouseListener,import java.awt.event.MouseListener;

  2. 在某处构建MouseListener,包括按钮的操作

  3. 在您定义btnLogin的位置,添加一行btnLogin.addMouseListener(<name of MouseListener>);

  4. 示例:http://www.java2s.com/Code/Java/Swing-JFC/ButtonActionSample.htm