2013-04-07 112 views
2

我將createLineborder設置爲我的JTextPane。但與TextPane的左端和頂端相比,JTextpane的右端和底端的邊界線略有不同。我在網上搜索,我發現它是默認行爲lineborder。那麼,任何人都可以告訴我,是否有任何邊界在所有四邊都有相同的線條尺寸?JTextpane的所有四面都具有相同尺寸的邊框

嗨我不能把我的代碼,因爲它是非常巨大的。所以只需在這裏輸入示例代碼。 JPanel面板; JTextPane窗格;

public BorderedTextPane() { 
    // TODO Auto-generated constructor stub 
    pane = new JTextPane(); 
    panel = new JPanel(null); 
    JPanel innerPanel = new JPanel(null); 
    innerPanel.setBounds(50,50,300,400); 
    pane.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    pane.setSize(new Dimension(innerPanel.getWidth(),innerPanel.getHeight())); 
    innerPanel.add(pane); 
    panel.add(innerPanel); 
    add(panel); 
    setVisible(true); 
    setSize(new Dimension(500,500)); 
    setLocationRelativeTo(null); 
} 

這可能如果您在放大的textpane或當您將它保存在一個文件中清楚地看到。下圖可以更好地解釋它。比較左邊和右邊的線。 enter image description here

+0

問題是JTextPane的放置在JScrollPane的 – mKorbel 2013-04-07 06:33:27

+0

顯示你的代碼.... – 2013-04-07 06:51:47

+0

爲了詳細說明mkorbel的評論一LineBorder如果你看到一條較粗的線,那麼它是來自父組件,改變你的LineBorder的顏色來看看它的區別 – camickr 2013-04-07 17:20:07

回答

1

JComponent#setBorder() API:

雖然技術上可以設定,從JComponent繼承的任何對象,外觀上邊框和感覺實現很多標準Swing組件不與用戶很好地工作設置邊界。一般來說,當您想要在除JPanelJLabel以外的標準Swing組件上設置邊框時,我們建議您將組件放在JPanel中,並將邊框設置爲JPanel

2

我會說,它更多的是用null佈局...

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.CompoundBorder; 
import javax.swing.border.EmptyBorder; 

public class TestBorder { 

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

    public TestBorder() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      setBorder(new EmptyBorder(10, 10, 10, 10)); 
      JTextPane pane = new JTextPane(); 
      JPanel panel = new JPanel(new BorderLayout()); 
      JPanel innerPanel = new JPanel(new BorderLayout()); 

      pane.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      innerPanel.add(pane); 
      panel.add(innerPanel); 
      add(panel); 
     } 
    } 
} 
+0

感謝MadProgrammer的代碼,我會研究它。 – user001 2013-04-07 08:01:25

0

諮詢API setBorder方法(新EmptyBorder());

除了使用變量的JTextPane varible.setBorder(BorderFactory.createLineBorder(Color.xxx);

相關問題