简体   繁体   中英

How to open new window by clicking label in java?

I want to open a new window when I click the label. By implementing a key pressed listener. But it is not working. Even nothing is happening.

JLabel lblNewLabel_2 = new JLabel("Create New Account!!!!");
lblNewLabel_2.setForeground(Color.GREEN);
lblNewLabel_2.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        
        Dosomething();
    }
});
private void Dosomething() {
    hide();
    Account account=new Account();
    account.visible();
    
}
protected static void hide() {
    frame.hide();
}

You can use JButton as others say and remove its border to make it look like JLabel but if you really want to use JLabel and detect clicks I think you should write a MouseAdapter for it.

JLabel fooLabel = new JLabel("foo");

fooLabel.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                doSomething();
            }
        }
);

You can also override mouseClicked method but in that case the user must not move the cursor while clicking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM