2012-07-03 82 views
0

我想要一個帶有LineBorder的JTextArea,並在文本和LineBorder之間留下一點填充。邊距與LineBorders

這是可能的標準類或我需要一個自定義的「雙線」邊框(一個顏色和一個邊距)?

一些示例代碼如下...

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Insets; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextArea; 
import javax.swing.SwingConstants; 
import javax.swing.SwingUtilities; 
import javax.swing.border.Border; 
import javax.swing.border.LineBorder; 

public class TextAreaLineBorder extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 

    private class MyPanel extends JPanel 
    { 

     private static final long serialVersionUID = 1L; 

     public MyPanel() 
     { 
      setLayout(new FlowLayout()); 

      JTextArea ta = new JTextArea("Some text"); 
      ta.setSize(200, 50); 

      boolean useDefaultBorder = false; 

      if (useDefaultBorder) 
      { 
       // Setting the margin works fine, with the default border 
       ta.setMargin(new Insets(12, 12, 12, 12)); 

       Border b = ta.getBorder(); 
       Insets defaultInsets = b.getBorderInsets(ta); 
       System.out.println("Default Insets: " 
         + defaultInsets); 

      } 
      else 
      { 
       // Try using a non-default LineBorder 
       LineBorder lb = (LineBorder) BorderFactory.createLineBorder(Color.YELLOW, 2); 
       ta.setBorder(lb); 

       // TODO: What should be done so that the LineBorder has Insets? 
       ta.setMargin(new Insets(12, 12, 12, 12)); 

       Insets lineBorderInsets = lb.getBorderInsets(ta); 
       System.out.println("LineBorder Insets: " + lineBorderInsets); 
      } 
      add(ta); 
     } 
    } 

    public TextAreaLineBorder() 
    { 
     setResizable(true); 

     setName(getClass().getSimpleName()); 
     setTitle("My Frame"); 
     setSize(300, 300); 

     JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP); 

     // Add the panel 
     tabbedPane.addTab("Button panel", new MyPanel()); 

     add(tabbedPane, BorderLayout.CENTER); 

     getContentPane().add(tabbedPane); 
    } 

    private static void createAndShowGUI() 
    { 
     // Create and set up the window. 
     TextAreaLineBorder frame = new TextAreaLineBorder(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       try 
       { 
        createAndShowGUI(); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
        System.exit(0); 
       } 
      } 
     }); 
    } 

} 

回答

0

我創建了一個類,它幫我解決這個問題。同樣的解決方案可能會用於所有JTextComponents。

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Date; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.border.Border; 
import javax.swing.border.CompoundBorder; 
import javax.swing.border.EmptyBorder; 
import javax.swing.border.LineBorder; 

public class JTextAreaWithPaddedBorder extends JTextArea 
{ 
    // ********************************************************************** 
    // To modify the sample program using GWT Designer, uncomment this code 
    // ********************************************************************** 
    // public JTextAreaWithPaddedBorder() 
    // { 
    // } 
    // 
    // /** 
    // * Bogus Constructor 
    // * 
    // * @wbp.parser.constructor (Use this method in the GWT Designer) 
    // */ 
    // 
    // public JTextAreaWithPaddedBorder(int ignore) 
    // { 
    // createAndShowGUI(); 
    // } 
    // ********************************************************************** 
    // To modify the sample program using GWT Designer, uncomment this code 
    // ********************************************************************** 

    public static void createAndShowGUI() 
    { 
     // Create and set up the frame 
     JFrame frmTextareawithpaddedborder = new JFrame(); 
     frmTextareawithpaddedborder.setTitle("TextAreaWithPaddedBorder"); 
     frmTextareawithpaddedborder.setName("frmTextareawithpaddedborder"); 
     frmTextareawithpaddedborder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frmTextareawithpaddedborder.setBounds(100, 100, 514, 495); 
     frmTextareawithpaddedborder.setResizable(false); 

     JPanel contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     frmTextareawithpaddedborder.setContentPane(contentPane); 

     JScrollPane scrollPane = new JScrollPane(); 
     scrollPane.setBounds(5, 5, 490, 395); 
     scrollPane.setName("scrollPane"); 
     contentPane.add(scrollPane); 

     final JTextAreaWithPaddedBorder textArea = new JTextAreaWithPaddedBorder(); 

     textArea.setBorder(new LineBorder(new Color(0, 0, 0), 2)); 
     textArea.setName("textArea"); 
     textArea.setLineWrap(true); 
     textArea.setWrapStyleWord(true); 
     String text = "The iPhone is a line of smartphones designed and marketed by Apple Inc. The first iPhone was unveiled by Steve Jobs, then CEO of Apple, on January 9, 2007,[1] and released on June 29, 2007. The 5th generation iPhone, the iPhone 4S, was announced on October 4, 2011, and released 10 days later. An iPhone can function as a video camera (video recording was not a standard feature until the iPhone 3GS was released), a camera phone, a portable media player, and an Internet client with email and web browsing capabilities, can send texts and receive visual voicemail, and has both Wi-Fi and cellular data (2G and 3G) connectivity. The user interface is built around the device's multi-touch screen, including a virtual keyboard rather than a physical one."; 
     textArea.setText(text); 
     textArea.append("\n\nThis is the text within the TextArea. As the border of the TextArea is changed, the text should display properly."); 
     contentPane.setLayout(null); 
     textArea.setBackground(Color.WHITE); 
     scrollPane.setViewportView(textArea); 

     btnChangeMargins = new JButton("Change Margins"); 
     btnChangeMargins.setBounds(13, 430, 152, 23); 
     btnChangeMargins.setName("btnChangeMargins"); 
     btnChangeMargins.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       long t = (new Date()).getTime() % 100; 
       int top = (int) (t * 18) % 15; 
       int bottom = (int) (t * 34) % 15; 
       int left = (int) (t * 52) % 15; 
       int right = (int) (t * 52) % 15; 
       Insets insets = new Insets(top, left, bottom, right); 
       textArea.setMargin(insets); 
      } 
     }); 
     contentPane.add(btnChangeMargins); 

     btnChangeBGColor = new JButton("Change BG Color"); 
     btnChangeBGColor.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       long t = (new Date()).getTime() % 100; 
       int r = 100 + (int) (t * 18) % 100; 
       int g = 100 + (int) (t * 34) % 100; 
       int b = 100 + (int) (t * 52) % 100; 
       Color c = new Color(r, g, b); 
       textArea.setBackground(c); 
      } 
     }); 

     btnChangeBGColor.setBounds(178, 430, 152, 23); 
     btnChangeBGColor.setName("btnChangeBGColor"); 
     contentPane.add(btnChangeBGColor); 

     btnChangeBorder = new JButton("Change Border"); 
     btnChangeBorder.setBounds(343, 430, 152, 23); 
     btnChangeBorder.setName("btnChangeBorder"); 
     btnChangeBorder.addActionListener(new ActionListener() 
     { 
      int last = 0; 

      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       Border nextBorder; 
       switch (last++ % 8) 
       { 
        case 0: 
         nextBorder = BorderFactory.createLoweredBevelBorder(); 
         break; 

        case 1: 
         nextBorder = BorderFactory.createEmptyBorder(); 
         break; 
        case 2: 
         nextBorder = BorderFactory.createEtchedBorder(); 
         break; 
        case 3: 
         nextBorder = BorderFactory.createLineBorder(Color.black, 2); 
         break; 
        case 4: 
         nextBorder = BorderFactory.createRaisedBevelBorder(); 
         break; 
        case 5: 
         nextBorder = BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red, 3), BorderFactory 
           .createLoweredBevelBorder()); 
         break; 
        case 6: 
         nextBorder = BorderFactory.createMatteBorder(2, 15, 1, 15, new Color(255, 0, 0)); 
         break; 
        case 7: 
         nextBorder = BorderFactory.createTitledBorder("Titled Border"); 
         break; 

        default: 
         nextBorder = BorderFactory.createTitledBorder("Titled Border"); 

       } 

       textArea.setBorder(nextBorder); 
      } 
     }); 
     contentPane.add(btnChangeBorder); 
     frmTextareawithpaddedborder.setVisible(true); 
    } 

    private static final long serialVersionUID = 1L; 
    private static JButton  btnChangeMargins; 
    private static JButton  btnChangeBGColor; 
    private static JButton  btnChangeBorder; 

    @Override 
    public void setBorder(Border border) 
    { 
     int paddingWidth = 0; 

     Border currentBorder = getBorder(); 
     if (currentBorder != null) 
     { 
      // The padding width will be the minimum width specified in the insets 
      Insets insets = getMargin(); 
      paddingWidth = Math.min(insets.bottom, insets.top); 
      paddingWidth = Math.min(paddingWidth, insets.left); 
      paddingWidth = Math.min(paddingWidth, insets.right); 
     } 
     /* 
     * Use a LineBorder for the padding. 
     * 
     * The color must be the same as the background color of the TextComponent 
     */ 
     super.setBorder(new CompoundBorder(border, BorderFactory.createLineBorder(getBackground(), paddingWidth))); 
    } 

    @Override 
    public void setBackground(Color c) 
    { 
     super.setBackground(c); 
     Border b = getBorder(); 
     if (b != null) 
     { 
      setBorder(((CompoundBorder) b).getOutsideBorder()); 
     } 
    } 

    @Override 
    public void setMargin(Insets m) 
    { 
     super.setMargin(m); 
     Border b = getBorder(); 
     if (b != null) 
     { 
      setBorder(((CompoundBorder) b).getOutsideBorder()); 
     } 
    } 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       try 
       { 
        createAndShowGUI(); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

} 
1

您可以將JTextArea添加到JScrollPane中並將邊框放在該邊上。

JTextArea JTA = new JTextArea(); 
JScrollPane JSP = new JScrollPane(JTA); 

JSP.setBorder(BorderFactory.createLineBorder(Color.blue));