Hye我正在开发Java J2SE(测试,仅用于知识)应用程序。这里我要做的是我要在主面板上添加5个子面板。然后为它们分配不同的颜色,之后我必须为JLabel分配彩色框的图像。只需使用Random类,我就必须在子面板中随机设置JLabel与图像的可见性。
问题! 问题是每当我添加一个带有图像的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(真正的问题)后
答案 0 :(得分:0)
像.setResizable(false)
这样的东西会起作用吗?还是我误解了这个问题?
答案 1 :(得分:0)
您可以使用null布局管理器或AbsoluteLayout。
答案 2 :(得分:0)
好吧,我正在拥抱解决自己的问题。直到最后一个选项,我都尝试了一切我找到了一个解决方案,而不是使用子面板,为什么不将JLabel添加到主面板并使用线程随机设置可见的。这真棒!我的新代码是:
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Panel extends javax.swing.JFrame {
public Panel() {
initComponents();
a.setVisible(false);
b.setVisible(false);
c.setVisible(false);
d.setVisible(false);
e.setVisible(false);
Thread t=new Thread(new RunThread());
t.start();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
b = new javax.swing.JLabel();
c = new javax.swing.JLabel();
e = new javax.swing.JLabel();
a = new javax.swing.JLabel();
d = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/2.png"))); // NOI18N
c.setIcon(new javax.swing.ImageIcon(getClass().getResource("/1.png"))); // NOI18N
e.setIcon(new javax.swing.ImageIcon(getClass().getResource("/4.jpg"))); // NOI18N
a.setIcon(new javax.swing.ImageIcon(getClass().getResource("/4.jpg"))); // NOI18N
d.setIcon(new javax.swing.ImageIcon(getClass().getResource("/3.jpg"))); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(b))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(294, 294, 294)
.addComponent(c))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(92, 92, 92)
.addComponent(a)))
.addContainerGap(283, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(e)
.addGap(34, 34, 34))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(d)
.addGap(104, 104, 104))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(b)
.addGap(39, 39, 39)
.addComponent(d)
.addGap(32, 32, 32)
.addComponent(c)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addComponent(a)
.addGap(5, 5, 5)
.addComponent(e)
.addGap(121, 121, 121))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}
private class RunThread implements Runnable{
Random randomGenerator = new Random();
public void run(){
int i;
try{
while(!Thread.currentThread().isInterrupted()){
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(1000);
a.setVisible(false);
} catch (InterruptedException ex) {
}
}
else if(RandomBox==2){
b.setVisible(true);
try {
Thread.currentThread().sleep(1000);
b.setVisible(false);
} catch (InterruptedException ex) {
}
}
else if(RandomBox==3){
c.setVisible(true);
try {
Thread.currentThread().sleep(1000);
c.setVisible(false);
} catch (InterruptedException ex) {
}
}
else if(RandomBox==4){
d.setVisible(true);
try {
Thread.currentThread().sleep(1000);
d.setVisible(false);
} catch (InterruptedException ex) {
}
}
else if(RandomBox==5){
e.setVisible(true);
try {
Thread.currentThread().sleep(1000);
e.setVisible(false);
} catch (InterruptedException ex) {
}
}
}
}
}catch(Exception e){
}
}
}
/**
* @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(Panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Panel.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 Panel().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel a;
private javax.swing.JLabel b;
private javax.swing.JLabel c;
private javax.swing.JLabel d;
private javax.swing.JLabel e;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
它会限制组件自动调整大小...... Bingoo