javaのswingでパネルにラベルを座標指定で設定したいのですがわかりません。
コードも込みで教えていただけるとありがたいです。
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Layout5 extends JFrame {
JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;
JPanel panel1;
JPanel panel2;
JPanel panel3;
CardLayout layout;
public static void main(String[] args) {
Layout5 frame = new Layout5("例");
frame.setVisible(true);
}
Layout5(String title) {
setTitle("例");
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(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
layout.show(getContentPane(), "panel2");
}
});
//座標指定
Dimension d = label1.getPreferredSize();
label1.setBounds(50, 100, d.width, d.height);
panel1.add(label1);
panel2 = new JPanel();
label2 = new JLabel("2");
label2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
layout.show(getContentPane(), "panel3");
}
});
panel2.add(label2);
panel3 = new JPanel();
label3 = new JLabel("3");
label3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
layout.show(getContentPane(), "panel1");
}
});
panel3.add(label3);
label4 = new JLabel("4");
label4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
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_SCROLLBAR_ALWAYS);
JScrollPane scrollpane2 = new JScrollPane(panel2);
scrollpane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane scrollpane3 = new JScrollPane(panel3);
scrollpane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel3.setPreferredSize(new Dimension(1000,1000) );
contentPane.add(scrollpane1,"panel1");
contentPane.add(scrollpane2, "panel2");
contentPane.add(scrollpane3, "panel3");
}
}