2017-11-11 52 views
2

我正在尝试创建一个程序,使得创建一个学校项目的楼层布局图很容易。用户可以输入建筑物的尺寸,然后将其刻画为标签网格,通过单击标签可将其转变为墙壁,窗户或门。因为墙壁,窗户和门具有不同的外观,材质等,我为每个构建了单独的类,所有这些都扩展了buildingObject。为了让它首先工作,我只关注于在窗体上获取buildingObjects。java-动态JLabel没有显示

package building.pkg3d.printer; 

import java.awt.Color; 
import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class buildingObject { 
    private int x; 
    private int y; 
    private int rotation; 
    private javax.swing.JLabel jLabel = new javax.swing.JLabel("wohnvipowa"); 
    private static final int WIDTH = 10; 
    private static final int HEIGHT = 100; 

public buildingObject(int x, int y, int rotation, JPanel p) { 
    this.x = x; 
    this.y = y; 
    this.rotation = rotation; 
    initLabel(p); 
} 

public void initLabel(JPanel p) 
{ 
    jLabel.setLocation(x, y); 
    if (rotation == 1) 
     jLabel.setPreferredSize(new Dimension(WIDTH, HEIGHT)); 
    else 
     jLabel.setPreferredSize(new Dimension(HEIGHT, WIDTH)); 
    jLabel.setBackground(Color.BLACK); 
    jLabel.setOpaque(true); 
    p.add(jLabel); 
    p.validate(); 
    System.out.println("jLabel added to panel"); 
    System.out.println("jLabel at: " + jLabel.getX() + ", " + jLabel.getY()); 
    System.out.println(""); 
    jLabel.setVisible(true); 
} 

public int getX() { 
    return x; 
} 

public void setX(int x) { 
    this.x = x; 
} 

public int getY() { 
    return y; 
} 

public void setY(int y) { 
    this.y = y; 
} 

public int getRotation() { 
    return rotation; 
} 

public JLabel getjLabel() { 
    return jLabel; 
} 

@Override 
public String toString() { 
    return "buildingObject{" + "x=" + x + ", y=" + y + ", rotation=" + rotation + ", jLabel=" + jLabel + '}'; 
} 
} 

我快速测试一个空的形式

package building.pkg3d.printer; 

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import javax.swing.JPanel; 

public class labelTester extends javax.swing.JFrame { 

private buildingObject[][] matrix; 

/** 
* Creates new form labelTester 
*/ 
public labelTester() { 
    initComponents(); 
    initLayout(1500,1500); 
    System.out.println(this.getContentPane().getComponentAt(0, 0).toString()); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 514, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 426, Short.MAX_VALUE) 
    ); 

    pack(); 
}// </editor-fold>       

/** 
* @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(labelTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(labelTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(labelTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(labelTester.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 labelTester(); 
     } 
    }); 
} 

private void initLayout(int w, int l) 
{ 
    int numRows = l/100; 
    int numCol = w/100; 

    matrix = new buildingObject[numRows][numCol]; 
    JPanel p = new JPanel(); 

    for(int i = 0; i < matrix.length; i++) 
    { 
     for(int j = 0; j < matrix[i].length; j++) 
     { 
      matrix[i][j] = new buildingObject(i * 100, j * 100, 1, p);     
     } 
    } 

    this.add(p, BorderLayout.NORTH); 
    this.pack(); 
    this.setVisible(true); 
} 

// Variables declaration - do not modify      
// End of variables declaration     
} 

上,但每当我运行程序,存在标签,但他们不露面。我需要能够将标签放置在特定的x和y值,所以我不相信我可以使用布局。我可以做些什么来让我的标签显示?还是有更好的方法来做到这一点,我不知道?

回答

2

我需要能够将标签放置在特定的x和y值,所以我不相信我可以使用布局。

正确。

但是,您的代码存在问题,因为您不了解使用布局管理器的基础知识,并且您正在使用IDE生成代码。

initComponents()方法你的IDE生成的代码:

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
getContentPane().setLayout(layout); 

因此,这意味着你使用的是GroupLayout

在你initLayout()方法使用的是:

this.add(p, BorderLayout.NORTH); 

所以我们假定你是布局管理器是一个BorderLayout,它不是。如果要将面板添加到框架,则需要使用GroupLayout所需的所有约束。由于您没有使用正确的约束,面板从不显示,您将看不到添加到面板的组件。

最后,默认情况下,JPanel使用FlowLayout,因此当您将BuildObjects添加到面板时,它们将根据布局管理器的规则进行定位。

所以你真正需要做的是重新开始。我建议不要使用IDE来生成GUI代码,而是手动创建GUI,以便在需要时控制布局管理器。

然后,对于包含BuildObjects的面板,您将需要使用空布局。然后,您将负责设置您添加到面板的组件的sizelocation

现在通常使用空布局并不是一个好主意,因为Swing被设计为与布局管理器一起使用,因此pack()方法可以确定面板的首选大小,并且滚动将正常工作。

因此,要使生活更轻松,您可以使用Drag Layout。它允许随机定位组件,但它将实现布局管理器的其他功能。