我创建了一个类来帮助我解决这个问题。相同的解决方案可能适用于所有 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();
}
}
});
}
}