试试这个代码示例:
import java.awt.*;
import javax.swing.*;
public class InsetTesting extends JFrame
{
private void createAndDisplayGUI()
{
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationByPlatform(true);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBorder(BorderFactory.createLineBorder(
Color.DARK_GRAY.darker(), 5, true));
contentPane.setBackground(Color.WHITE);
add(contentPane, BorderLayout.CENTER);
pack();
setVisible(true);
}
public Insets getInsets()
{
return (new Insets(30, 20, 10, 20));
}
public Dimension getPreferredSize()
{
return (new Dimension(200, 400));
}
public static void main(String\u005B\u005D args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new InsetTesting().createAndDisplayGUI();
}
});
}
}
这是相同的输出: