簡體   English   中英

程序終止而不在Eclipse中運行

[英]Program Terminates without running in eclipse

當我運行此代碼(具有簡單的按鈕和帶有操作偵聽器的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擴展的登錄類。 當我從首頁單擊“登錄”按鈕時。 它沒有顯示“登錄”面板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);

}

您沒有使您的JFrame可見。

您可以-

  1. 在您的構造函數中,通過在末尾添加以下行使其可見:

     setVisible(true); 
  2. 或者在您的main()函數中,您可以-

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

添加類似setVisible(true);東西setVisible(true); HOme方法的末尾。

第二部分的答案:

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

  2. 在某處構造一個MouseListener,包括按鈕的動作

  3. 在定義btnLogin ,添加一行btnLogin.addMouseListener(<name of MouseListener>);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM