Created
June 5, 2017 15:06
-
-
Save Blasanka/4ec80a7e819c1ddb5c34157455ec4ec8 to your computer and use it in GitHub Desktop.
Simple Example for JLabel mouse clicked. In this example when mouse clicked shows another JFrame and close the Main JFrame. Note: this is only for study purpose but having multiple JFrames in one application is not good solution. Instead you can use one JFrame and Multiple JPanels with appropriate layouts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
public class Main { | |
JFrame MainFrame; | |
JFrame ChildFrame; | |
JLabel label; | |
public Main(){ | |
MainFrame = new JFrame("Example"); | |
MainFrame.setSize(300, 300); | |
label = new JLabel("Click me"); | |
labelMousePressed(); | |
MainFrame.add(label); | |
MainFrame.setVisible(true); | |
} | |
private void labelMousePressed() { | |
label.addMouseListener(new MouseAdapter(){ | |
public void mousePressed(MouseEvent e){ | |
System.out.println("It works."); | |
MainFrame.dispose(); | |
ChildFrame = new JFrame("Child"); | |
ChildFrame.setSize(300, 300); | |
ChildFrame.setVisible(true); | |
} | |
}); | |
} | |
public static void main(String[] args) { | |
Main m = new Main(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment