为什么我不能将EmptyBorder添加到自定义JComponent?

时间:2015-04-26 08:24:00

标签: java swing

我的代码无法将自定义JComponent对象的边框设置为new EmptyBorder(50, 50, 50, 50)。这就是我的显示器上的样子(你可以看到,它显然适用于JButton但不适用于自定义JComponent):

enter image description here

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;

public class Sandbox
{
    public static void main(String[] args)
    {
        // Instantiate components needed.
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.add(panel);
        WideComponent wc = new WideComponent();
        JButton button = new JButton("Test");

        // Add borders
        EmptyBorder empty1 = new EmptyBorder(50, 50, 50, 50);
        wc.setBorder(empty1);
        EmptyBorder empty2 = new EmptyBorder(50, 50, 50, 50);
        button.setBorder(empty2);

        // Add stuff to panel
        panel.add(wc);
        panel.add(button);

        // Add panel to frame, show frame
        frame.add(panel);
        frame.setVisible(true);
        frame.pack();
    }
}

class WideComponent extends JComponent
{
    final int WIDTH = 500;
    final int HEIGHT = 150; 

    // drawing method
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
    }

    // JComponent sizing methods
    public Dimension getPreferredSize()
    {
        return new Dimension(WIDTH, HEIGHT);
    }
    public Dimension getMaximumSize() { return getPreferredSize(); }
    public Dimension getMinimumSize() { return getPreferredSize(); }    
}

1 个答案:

答案 0 :(得分:2)

你的边框工作正常 - 但只是空边框被涂成蓝色,因为你正在绘制整个组件,边框是该组件的一部分。例如:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class Sandbox {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      JPanel panel = new JPanel();
      frame.add(panel);
      WideComponent wc = new WideComponent();
      JButton button = new JButton("Test");

      wc.setBorder(createBorder());
      button.setBorder(createBorder());

      panel.add(wc);
      panel.add(button);

      frame.add(panel);
      frame.setVisible(true);
      frame.pack();
   }

   private static Border createBorder() {
      Border innerBorder = BorderFactory.createLineBorder(Color.red);
      int eb = 50;
      Border outerBorder = BorderFactory.createEmptyBorder(eb, eb, eb, eb);
      Border comboBorder = BorderFactory.createCompoundBorder(outerBorder,
            innerBorder);
      return comboBorder;
   }
}

class WideComponent extends JComponent {
   final int WIDTH = 500;
   final int HEIGHT = 150;

   public WideComponent() {
      setOpaque(true);
   }

   // drawing method
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
       g.setColor(Color.BLUE);
       g.fillRect(0, 0, WIDTH, HEIGHT);
   }

   // JComponent sizing methods
   public Dimension getPreferredSize() {
      return new Dimension(WIDTH, HEIGHT);
   }

   public Dimension getMaximumSize() {
      return getPreferredSize();
   }

   public Dimension getMinimumSize() {
      return getPreferredSize();
   }
}

如果要在WideComponent周围添加空白边框,请将其嵌套在JPanel中,并将边框赋予嵌套JPanel。例如,可以通过添加以下代码来更改上面的代码:

  JPanel nestingJPanel = new JPanel();
  nestingJPanel.add(new WideComponent());
  nestingJPanel.setBorder(createBorder());

  panel.add(wc);
  panel.add(button);
  panel.add(nestingJPanel);