Specify and attach coordinates when labeling the swing panel

Asked 1 years ago, Updated 1 years ago, 67 views

I'd like to set the label on the panel by coordinate designation in java swing, but I don't know.
I would appreciate it if you could let me know the code as well.

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;

import javax.swing.*;
import javax.swing.border.LineBorder;

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

    public static void main(String[]args) {
        Layout 5 frame = new Layout 5("Example");
        frame.setVisible(true);
    }

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

        label1 = new JLabel("1");
        panel1 = new JPanel();
        label1.setBorder (new LineBorder (Color.blue, 2, true));
        panel1.setPreferredSize(new Dimension(200,100));
        label1.addMouseListener(newMouseAdapter(){
            @ Override
            public void mouseClicked (MouseEvente) {
                layout.show(getContentPane(), "panel2";
            }
        });
        // Coordinate designation
        Dimensioned=label1.getPreferredSize();
        label1.setBounds(50,100,d.width,d.height);
        panel1.add(label1);

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

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

        label4 = new JLabel("4");
        label4.addMouseListener(newMouseAdapter(){
            @ Override
            public void mouseClicked (MouseEvente) {
                layout.show(getContentPane(), "panel3";
            }
        });
        panel1.add(label4);
        label4.setBorder (new LineBorder (Color.blue, 2, true));

        layout = new CardLayout();

        Container contentPane=getContentPane();
        contentPane.setLayout(layout);

        JScrollPane scrollpane1 = new JScrollPane(panel1);
        scrollpane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLBAR_ALWAYS);

        JScrollPane scrollpane2 = new JScrollPane(panel2);
        scrollpane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLBAR_ALWAYS);

        JScrollPane scrollpane3 = new JScrollPane(panel3);
        scrollpane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLBAR_ALWAYS);
        panel3.setPreferredSize (new Dimension (1000, 1000);

        contentPane.add(scrollpane1, "panel1");
        contentPane.add(scrollpane2, "panel2");
        contentPane.add(scrollpane3, "panel3");
    }

}

java swing

2022-09-30 18:08

1 Answers

You can place null in absolute coordinates by specifying setLayout method in constructor in JPanel.

I don't know which stage of Java I'm tripping at.
I appreciate that I could make the following decision when I answer that you will ask me a question including the code.

  • Do you know JPanel's Layout Manager
  • JPanel's setLayout(null) and JLabel's setBound are ready, but they are not displayed because they do not setSize (English similar question).
  • Do you want flexible layout using JLayeredPane?
  • Are you not interested in Java and would you like to throw a whole challenge?

It was a rather awkward answer, but even if it doesn't work, I think it would be easier to get a more accurate answer if you included the code or error being created in the questionnaire.
Please consider the questionnaire when you ask similar questions in the future.

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class NullLayoutFrame extensions JFrame {
    publicNullLayoutFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(null); // constructor without layout manager specified
        // If you want the layout manager to be null in setLayout, remove the above and enable the following two lines.
        // JPanel panel = new JPanel();
        // panel.setLayout(null);

        JLabel label = new JLabel("Absolute bound.");
        Dimensioned=label.getPreferredSize();
        label.setBounds(50,100,d.width,d.height); // Set label by coordinate specification

        panel.add(label);

        getContentPane().add(panel, BorderLayout.CENTER);
    }

    public static void main(String[]args) {
        SwingUtilities.invokeLater(newRunnable(){
            public void run() {
                NullLayoutFrame = new NullLayoutFrame();
                frame.setBounds(10,10,300,200);
                frame.setVisible(true);
            }
        });
    }  
}


2022-09-30 18:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.