public class ExamplePane extends javax.swing.JFrame {
public ExamplePane() {
// this is constructor
initComponents();
}
private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {
jPanel1.setVisible(true);
jPanel2.setVisible(false);
jPanel2.invalidate();
}
private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {
jPanel2.setVisible(true);
jPanel1.setVisible(false);
jPanel1.invalidate();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ExamplePane().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
// End of variables declaration
}
当你试图在点击事件上逐个显示jpanel时,面板就会重叠。所以请帮助我。
答案 0 :(得分:2)
您必须使用CardLayout
,只有两个JPanel
,仅覆盖mouseClicked()
中的MouseAdapter
(来自MouseEvents)
您对已经可见的Swing GUI进行了更改,需要通知已使用的LayoutManage
r有更改,请使用(re)validate()
和repaint()
代替invalidate();
,然后不需要使用setVisible
答案 1 :(得分:0)
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
public class ScrollLabel extends javax.swing.JFrame {
public ScrollLabel() {
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
jPanel1 = new JPanel();
jPanel1.setBackground(Color.red);
jPanel1.add(new JLabel("Panel 1"));
jPanel2 = new JPanel();
jPanel2.setBackground(Color.green);
jPanel2.add(new JLabel("Panel 2"));
jLabel1 = new JLabel("jPanel 1");
jLabel2 = new JLabel("jPanel 2");
jPanel1.setBounds(0, 0, getWidth(),getHeight()-50));
jPanel2.setBounds(0, 0, getWidth(),getHeight()-50));
jLabel1.setBounds(0, getHeight()-40, 100,30);
jLabel2.setBounds(getWidth()/2, getHeight()-40, 100,30);
jLabel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
jLabel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
add(jPanel1);
add(jPanel2);
add(jLabel1);
add(jLabel2);
repaint();
jLabel1.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
jLabel1MouseClicked(e);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
jLabel2.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
jLabel2MouseClicked(e);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}
private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {
jPanel1.setVisible(true);
jPanel2.setVisible(false);
jPanel2.invalidate();
}
private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {
jPanel2.setVisible(true);
jPanel1.setVisible(false);
jPanel1.invalidate();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new ScrollLabel().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
// End of variables declaration
}