Hye 我正在开发一个 Java J2SE(测试,仅用于知识)应用程序。在这里,我正在做的是向主面板添加 5 个子面板。然后为它们分配不同的颜色,然后我必须为 JLabel 分配彩色盒子的图像。只需使用 Random 类,我就必须在子面板中随机设置可见的带有图像的 JLabels。
问题! 问题是每当我添加带有图像的 JLabel 时,子面板都会自动调整大小。我该如何克服呢?我搜索了很多,但未能了解如何解决这个问题!
我一直在尝试什么!我使用了不同的布局管理器,但没有成功!
我的代码
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
public class PanelMovement extends javax.swing.JFrame {
public PanelMovement() {
initComponents();
Thread t=new Thread(new RunThread());
t.start();
a.setVisible(false);
b.setVisible(false);
c.setVisible(false);
d.setVisible(false);
e.setVisible(false);
}
private void initComponents() {
mainpanel = new javax.swing.JPanel();
green = new javax.swing.JPanel();
a = new javax.swing.JLabel();
blue = new javax.swing.JPanel();
b = new javax.swing.JLabel();
black = new javax.swing.JPanel();
c = new javax.swing.JLabel();
white = new javax.swing.JPanel();
d = new javax.swing.JLabel();
yellow = new javax.swing.JPanel();
e = new javax.swing.JLabel();
tt = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
mainpanel.setBackground(new java.awt.Color(255, 255, 255));
green.setBackground(new java.awt.Color(51, 255, 0));
green.setMinimumSize(new java.awt.Dimension(97, 122));
a.setIcon(new javax.swing.ImageIcon(getClass().getResource("/2.png"))); // NOI18N
a.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
aMouseClicked(evt);
}
});
javax.swing.GroupLayout greenLayout = new javax.swing.GroupLayout(green);
green.setLayout(greenLayout);
greenLayout.setHorizontalGroup(
greenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(greenLayout.createSequentialGroup()
.addContainerGap()
.addComponent(a)
.addContainerGap(31, Short.MAX_VALUE))
);
greenLayout.setVerticalGroup(
greenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(greenLayout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(a)
.addContainerGap(61, Short.MAX_VALUE))
);
blue.setBackground(new java.awt.Color(0, 0, 204));
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/5.jpg"))); // NOI18N
b.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
bMouseClicked(evt);
}
});
javax.swing.GroupLayout blueLayout = new javax.swing.GroupLayout(blue);
blue.setLayout(blueLayout);
blueLayout.setHorizontalGroup(
blueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(blueLayout.createSequentialGroup()
.addContainerGap()
.addComponent(b)
.addContainerGap(31, Short.MAX_VALUE))
);
blueLayout.setVerticalGroup(
blueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(blueLayout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(b)
.addContainerGap(61, Short.MAX_VALUE))
);
black.setBackground(new java.awt.Color(0, 0, 0));
c.setForeground(new java.awt.Color(255, 255, 255));
c.setIcon(new javax.swing.ImageIcon(getClass().getResource("/3.jpg"))); // NOI18N
c.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
cMouseClicked(evt);
}
});
javax.swing.GroupLayout blackLayout = new javax.swing.GroupLayout(black);
black.setLayout(blackLayout);
blackLayout.setHorizontalGroup(
blackLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(blackLayout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(c)
.addContainerGap(11, Short.MAX_VALUE))
);
blackLayout.setVerticalGroup(
blackLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, blackLayout.createSequentialGroup()
.addContainerGap(46, Short.MAX_VALUE)
.addComponent(c)
.addGap(40, 40, 40))
);
white.setBackground(new java.awt.Color(0, 255, 255));
d.setIcon(new javax.swing.ImageIcon(getClass().getResource("/1.png"))); // NOI18N
d.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
dMouseClicked(evt);
}
});
javax.swing.GroupLayout whiteLayout = new javax.swing.GroupLayout(white);
white.setLayout(whiteLayout);
whiteLayout.setHorizontalGroup(
whiteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(whiteLayout.createSequentialGroup()
.addContainerGap()
.addComponent(d)
.addContainerGap(31, Short.MAX_VALUE))
);
whiteLayout.setVerticalGroup(
whiteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, whiteLayout.createSequentialGroup()
.addContainerGap(61, Short.MAX_VALUE)
.addComponent(d)
.addGap(25, 25, 25))
);
yellow.setBackground(new java.awt.Color(255, 255, 0));
e.setIcon(new javax.swing.ImageIcon(getClass().getResource("/4.jpg"))); // NOI18N
e.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
eMouseClicked(evt);
}
});
javax.swing.GroupLayout yellowLayout = new javax.swing.GroupLayout(yellow);
yellow.setLayout(yellowLayout);
yellowLayout.setHorizontalGroup(
yellowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, yellowLayout.createSequentialGroup()
.addContainerGap(22, Short.MAX_VALUE)
.addComponent(e)
.addGap(19, 19, 19))
);
yellowLayout.setVerticalGroup(
yellowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, yellowLayout.createSequentialGroup()
.addContainerGap(46, Short.MAX_VALUE)
.addComponent(e)
.addGap(40, 40, 40))
);
jLabel1.setText("Score=");
javax.swing.GroupLayout mainpanelLayout = new javax.swing.GroupLayout(mainpanel);
mainpanel.setLayout(mainpanelLayout);
mainpanelLayout.setHorizontalGroup(
mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainpanelLayout.createSequentialGroup()
.addGap(199, 199, 199)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(tt, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainpanelLayout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(green, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(67, 67, 67)
.addComponent(blue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50)
.addComponent(black, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
.addComponent(white, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(yellow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
mainpanelLayout.setVerticalGroup(
mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainpanelLayout.createSequentialGroup()
.addGap(131, 131, 131)
.addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(yellow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(black, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(blue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(green, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(white, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 115, Short.MAX_VALUE)
.addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(44, 44, 44))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(mainpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(mainpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
int count=0;
private void aMouseClicked(java.awt.event.MouseEvent evt) {
count+=1;
tt.setText(Integer.toString(count));
}
private void bMouseClicked(java.awt.event.MouseEvent evt) {
count+=1;
tt.setText(Integer.toString(count));
}
private void cMouseClicked(java.awt.event.MouseEvent evt) {
count+=1;
tt.setText(Integer.toString(count));
}
private void dMouseClicked(java.awt.event.MouseEvent evt) {
count+=1;
tt.setText(Integer.toString(count));
}
private void eMouseClicked(java.awt.event.MouseEvent evt) {
count+=1;
tt.setText(Integer.toString(count));
}
private class RunThread implements Runnable{
Random randomGenerator = new Random();
public void run(){
boolean flag=true;
while(flag){
int RandomBox = randomGenerator.nextInt(5)+1;
try{
Thread.currentThread().sleep(1000);
}catch(Exception e){
}
if(RandomBox==1){
a.setVisible(true);
try {
Thread.currentThread().sleep(300);
a.setVisible(false);
} catch (InterruptedException ex) {
Logger.getLogger(PanelMovement.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if(RandomBox==2){
b.setVisible(true);
try {
Thread.currentThread().sleep(300);
b.setVisible(false);
} catch (InterruptedException ex) {
Logger.getLogger(PanelMovement.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if(RandomBox==3){
c.setVisible(true);
try {
Thread.currentThread().sleep(300);
c.setVisible(false);
} catch (InterruptedException ex) {
Logger.getLogger(PanelMovement.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if(RandomBox==4){
d.setVisible(true);
try {
Thread.currentThread().sleep(300);
d.setVisible(false);
} catch (InterruptedException ex) {
Logger.getLogger(PanelMovement.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if(RandomBox==5){
e.setVisible(true);
try {
Thread.currentThread().sleep(300);
e.setVisible(false);
} catch (InterruptedException ex) {
Logger.getLogger(PanelMovement.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PanelMovement.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PanelMovement.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PanelMovement.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PanelMovement.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PanelMovement().setVisible(true);
new PanelMovement().pack();
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel a;
private javax.swing.JLabel b;
private javax.swing.JPanel black;
private javax.swing.JPanel blue;
private javax.swing.JLabel c;
private javax.swing.JLabel d;
private javax.swing.JLabel e;
private javax.swing.JPanel green;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel mainpanel;
private javax.swing.JTextField tt;
private javax.swing.JPanel white;
private javax.swing.JPanel yellow;
// End of variables declaration
}
屏幕截图
添加 jlabel 后添加JLabel 之前 (真正的问题)