我遇到了这个问题,即使我添加了它,我的 Label2 也没有出现在 Jframe 上。我假设我设置为图像的其他标签与它重叠?如果这是问题,我将如何解决它?
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Generator extends JFrame {
private static final long serialVersionUID = 1L;
/**
* Launch the application.
*/
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
final Generator frame = new Generator();
frame.setVisible(true);
} catch (final Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Generator() {
/*
* @Images/Text
*/
final ImageIcon icon = new ImageIcon("data/hover_a.png");
final JLabel label = new JLabel(icon);
final ImageIcon icon1 = new ImageIcon("data/background.png");
final JLabel label1 = new JLabel(icon1);
final ImageIcon img = new ImageIcon("data/icon.png");
final JLabel label2 = new JLabel("Test Text");
/*
* @Image/Text Bounds
*/
label.setBounds(24, 266, 240, 40);
label1.setBounds(0, 0, 289, 330);
label2.setBounds(0, 0, 80, 15);
/*
* @Image Hovers
*/
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(final MouseEvent evt) {
label.setIcon(new ImageIcon("data/hover_b.png"));
}
@Override
public void mouseExited(final MouseEvent evt) {
label.setIcon(new ImageIcon("data/hover_a.png"));
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(295, 358));
setTitle("test");
getContentPane().setLayout(null);
setResizable(false);
setVisible(true);
setIconImage(img.getImage());
getContentPane().add(label);
getContentPane().add(label1);
getContentPane().add(label2);
pack();
}
}