【问题标题】:JComponent - cannot set Background/BorderJComponent - 无法设置背景/边框
【发布时间】:2014-07-29 10:35:00
【问题描述】:

我想在我的基本JFrame 中添加一个JComponent。我的问题是我无法为组件设置背景颜色或边框。

JFrame

JPanel drawPanel = new JPanel(new GridBagLayout());
drawPanel.add(new DrawingBoard());
JScrollPane scrollPane = new JScrollPane(drawPanel);
this.add(scrollPane, BorderLayout.CENTER);
this.setVisible(true);

JComponent

private class DrawingBoard extends JComponent {
        ...
        public DrawingBoard() {
        this.setPreferredSize(new Dimension(500, 500));
        this.setBackground(Color.WHITE);     //doesn't work
        this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); //doesn't work
        this.setOpaque(true);
        ...

【问题讨论】:

  • 必须是JComponent 还是JPanel

标签: java swing colors jpanel jcomponent


【解决方案1】:

基本上,因为JComponent 没有UI 委托,它从不绘制背景(它的paintComponent 方法基本上什么都不做)。

您可以提供执行所需操作的 UI 委托,或者直接使用 JPanel,例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;

public class TestComponent {

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

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

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

    public class TestPane extends JPanel {

        public TestPane() {
            setBackground(Color.WHITE);
            setBorder(new CompoundBorder(
                            BorderFactory.createEmptyBorder(0,10,10,10),
                            BorderFactory.createLineBorder(Color.RED)
            ));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

【讨论】:

  • 我有时会建议JPanel 因为它有一个 UI 委托;旧版本的com.apple.laf.AquaPanelUI 可见herehere
【解决方案2】:

我找到了解决问题的简单方法。我只是将 Jcomponent 添加到 Jpanel,然后将 Jpanel 添加到 Jframe。

    JPanel outer = new JPanel(new GridBagLayout()); 
    JPanel drawPanel = new JPanel(new GridBagLayout());
    drawPanel.add(new DrawingBoard(500,500));
    drawPanel.setBackground(Color.WHITE);
    outer.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
    JScrollPane scrollPane = new JScrollPane(drawPanel);
    outer.add(scrollPane);
    this.add(outer, BorderLayout.CENTER);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2018-05-19
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多