2016-07-22 78 views
-2

我想創建特定的行來將面板彼此分開。在paint中創建的下面的圖片應該有助於說明我想說的內容。你怎麼能這樣做? enter image description here如何在JPanel而不是邊界線周圍創建特定的線?

+0

你爲什麼不使用邊界線?似乎邊界線符合您的要求。 – hflzh

+0

當您使用邊框線時,它似乎勾勒出面板空間的整個邊界。這就是爲什麼我不想使用它。 @ zhliu03 – Chuck

+0

用於「員工」,「商店」,「部門」的組件是什麼?他們是JButton嗎?你有沒有試過JButton.setBorder()? – hflzh

回答

0

我不確定這是不是一個好主意,不使用邊框組件,但你可以使用Label

例如,這是一個情況下,在jPanel1 3塊板通過的JLabel

enter image description here


例分離,

public class Ask extends javax.swing.JFrame { 

    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 void initComponents() { 

    jPanel1 = new javax.swing.JPanel(); 
    jLabel1 = new javax.swing.JLabel(); 
    jPanel2 = new javax.swing.JPanel(); 
    jLabel2 = new javax.swing.JLabel(); 
    jPanel3 = new javax.swing.JPanel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 0, Short.MAX_VALUE) 
    ); 
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 93, Short.MAX_VALUE) 
    ); 

    jLabel1.setText("jLabel1"); 
    jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3)); 

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
    jPanel2.setLayout(jPanel2Layout); 
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 121, Short.MAX_VALUE) 
    ); 
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 0, Short.MAX_VALUE) 
    ); 

    jLabel2.setText("jLabel2"); 
    jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3)); 

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); 
    jPanel3.setLayout(jPanel3Layout); 
    jPanel3Layout.setHorizontalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 271, Short.MAX_VALUE) 
    ); 
    jPanel3Layout.setVerticalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 0, Short.MAX_VALUE) 
    ); 

    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) 
      .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addGroup(layout.createSequentialGroup() 
        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(0, 0, 0) 
        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(0, 0, 0) 
        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(0, 0, 0) 
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 3, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
          .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE) 
          .addGroup(layout.createSequentialGroup() 
            .addGap(0, 0, 0) 
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) 
    ); 
    pack(); 
} 

    public static void main(String[] args) { 
     Ask a = new Ask(); 
     a.setVisible(true); 
     a.initComponents(); 
    } 
} 
+0

當我使用邊框組件時,它通常圍繞面板空間的周邊包裹線條。我想控制線條的出現位置,還是可以使用其中一個邊框組件? @TomN – Chuck

+0

此外,我手工編碼的一切,不使用Windows系統的圖形用戶界面系統,我怎樣才能產生你在編碼方面做了什麼。 @TomN – Chuck

+0

好的,我更新了答案。有一個示例代碼。 – TomN

相關問題