I want to do screen transition with java swing.

Asked 2 years ago, Updated 2 years ago, 146 views

I'd like to do a screen transition by clicking on label 1 in the code below, click on panel 1 to panel 2, click on label 2 in panel 2, click on label 3 in panel 3, click on label 3 in panel 3, and use the show method to move to any panel.
What should I do to do the above?
If you run it now, clicking on the label on panel1 will transition to panel3.
I looked at the API document, but I don't know because I don't know enough.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Layout 2 extensions JFrame implements MouseListener {
    JLabel label1;
    JLabel label2;
    JLabel label3;
    CardLayout layout;
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;

    public static void main(String[]args) {
        Layout2frame = new Layout2("Title");
        frame.setVisible(true);
    }
    Layout 2 (String title) {
        setTitle("Title");
        setBounds (100, 100, 250, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label1 = new JLabel("click");
        panel1 = new JPanel();
        panel1.add(label1);
        label1.addMouseListener(this);

        panel2 = new JPanel();
        label2 = new JLabel ("second page");
        label2.addMouseListener(this);
        panel2.add(label2);

        panel3 = new JPanel();
        label3 = new JLabel ("Third");
        label3.addMouseListener(this);
        panel3.add(label3);


        layout = new CardLayout();

        Container contentPane=getContentPane();
        contentPane.setLayout(layout);
        contentPane.add(panel1, "panel");
        contentPane.add(panel2, "panel2");
        contentPane.add(panel3, "panel3");
    }

    @ Override
    public void mouseClicked (MouseEvente) {

        layout.show(getContentPane(), "panel2";
        layout.show(getContentPane(), "panel3";
    }

    @ Override
    public void mousePressed (MouseEvent) {
        // TODO AUTOMATICALLY GENERATED METHOD STUB

    }

    @ Override
    public void mouseReleased (MouseEvent) {
        // TODO AUTOMATICALLY GENERATED METHOD STUB

    }

    @ Override
    public void mouseEntered (MouseEvent) {
        // TODO AUTOMATICALLY GENERATED METHOD STUB

    }

    @ Override
    public void mouseExited (MouseEvent) {
        // TODO AUTOMATICALLY GENERATED METHOD STUB

    }

}

java swing

2022-09-30 17:34

1 Answers

Using MouseAdapter and Unknown Class is simple.

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Layout 2 extensions JFrame {
    JLabel label1;
    JLabel label2;
    JLabel label3;
    CardLayout layout;
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;

    public static void main(String[]args) {
        Layout2frame = new Layout2("Title");
        frame.setVisible(true);
    }

    Layout 2 (String title) {
        setTitle("Title");
        setBounds (100, 100, 250, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label1 = new JLabel("click");
        panel1 = new JPanel();
        panel1.add(label1);
        label1.addMouseListener(newMouseAdapter(){
            @ Override
            public void mouseClicked (MouseEvente) {
                layout.show(getContentPane(), "panel2";
            }
        });

        panel2 = new JPanel();
        label2 = new JLabel ("second page");
        label2.addMouseListener(newMouseAdapter(){
            @ Override
            public void mouseClicked (MouseEvente) {
                layout.show(getContentPane(), "panel3";
            }
        });
        panel2.add(label2);

        panel3 = new JPanel();
        label3 = new JLabel ("Third");
        label3.addMouseListener(newMouseAdapter(){
            @ Override
            public void mouseClicked (MouseEvente) {
                layout.show(getContentPane(), "panel");
            }
        });
        panel3.add(label3);

        layout = new CardLayout();

        Container contentPane=getContentPane();
        contentPane.setLayout(layout);
        contentPane.add(panel1, "panel");
        contentPane.add(panel2, "panel2");
        contentPane.add(panel3, "panel3");
    }

}

If listeners need to identify components that are still shared and clicked, they can be determined by conditions such as e.getSource()==label1.


2022-09-30 17:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.