2012-05-04 62 views
0

如何刪除TitledBorder中標題的空格?我的意思是紅色邊框
red border http://s019.radikal.ru/i634/1205/8a/1b40d810a4fe.jpg刪除TitledBorder中標題的空格

一段代碼:

Font f = new Font(Font.DIALOG, Font.PLAIN, 14); 
Map m = f.getAttributes(); 
m.put(TextAttribute.BACKGROUND, Color.yellow); 
Font ff = new Font(m); 
Border mb = BorderFactory.createMatteBorder(20, 0, 0, 0, Color.yellow); 
Border test = BorderFactory.createEmptyBorder(-2,-2,-2,-2); 
Border mb6 = BorderFactory.createTitledBorder(mb, "Title", TitledBorder.CENTER, TitledBorder.TOP, ff, Color.DARK_GRAY); 
mb6 = BorderFactory.createCompoundBorder(test, mb6); 
Border mb2 = BorderFactory.createMatteBorder(1, 0, 0, 0, Color.gray); 
mb2 = BorderFactory.createCompoundBorder(test,mb2); 
Border mb3 = BorderFactory.createCompoundBorder(mb6, mb2); 
Border mb4 = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray); 
Border mb5 = BorderFactory.createCompoundBorder(mb4, mb3); 
modeSetPanel.setBackground(Color.red); 
modeSetPanel.setBorder(mb5); 
+1

該紅色邊框可能已添加到您的JLabel中。但你需要發佈繪製這個窗口的代碼才能幫助我們 –

+0

我添加了一段代碼 – Natalia

+0

那麼,我期待看到的是在你的JLabel上調用setBorder,它接受一個邊框對象。有沒有在你的代碼中的某個地方打電話給setBorder? –

回答

3

如果你不關心邊界,你可以添加一個JPanel設置標籤面板中。 在此JPanel中添加另一個BorderLayout。在BorderLayout的內部添加兩個JPanel:北面的一個背景爲黃色,中間的背景爲紅色。只需添加一個標題爲「標題」的JLabel到黃色的JPanel,並將您的控件添加到紅色的標題。

標籤周圍的紅色邊框消失了。

這裏是沒有任何邊框,並在雨雲外觀&感覺一個例子:

Sample...

2

其實這不是一個答案,但在評論圖像是不可能的......

我想這樣的代碼:

import java.awt.Color; 
import java.awt.Font; 
import java.awt.font.TextAttribute; 
import java.util.Map; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.Border; 
import javax.swing.border.TitledBorder; 

public class RedBorderProblem extends JFrame { 

    public RedBorderProblem() { 
     final JPanel modeSetPanel = new JPanel(); 

     final Font f = new Font(Font.DIALOG, Font.PLAIN, 14); 
     final Map m = f.getAttributes(); 
     m.put(TextAttribute.BACKGROUND, Color.yellow); 
     final Font ff = new Font(m); 
     final Border mb = BorderFactory.createMatteBorder(20, 0, 0, 0, Color.yellow); 
     final Border test = BorderFactory.createEmptyBorder(-2, -2, -2, -2); 
     Border mb6 = BorderFactory.createTitledBorder(mb, "Title", TitledBorder.CENTER, TitledBorder.TOP, ff, 
       Color.DARK_GRAY); 
     mb6 = BorderFactory.createCompoundBorder(test, mb6); 
     Border mb2 = BorderFactory.createMatteBorder(1, 0, 0, 0, Color.gray); 
     mb2 = BorderFactory.createCompoundBorder(test, mb2); 
     final Border mb3 = BorderFactory.createCompoundBorder(mb6, mb2); 
     final Border mb4 = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray); 
     final Border mb5 = BorderFactory.createCompoundBorder(mb4, mb3); 
     modeSetPanel.setBackground(Color.red); 
     modeSetPanel.setBorder(mb5); 

     this.add(modeSetPanel); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.pack(); 
     this.setVisible(true); 
    } 

    public static void main(final String[] args) { 
     new RedBorderProblem(); 
    } 

} 

,其結果是

enter image description here