设置JPanel的背景颜色

时间:2016-12-30 00:11:51

标签: java swing

所以我尝试使用getContentPane()。setBackground(Color.white), 并尝试将表格和滚动窗格设置为白色。

@SuppressWarnings("serial")
public class BalanceFrame extends JFrame {

private JTable table;
public BalanceFrame(String title, String value) throws Exception {
    super(title);
    SpringLayout springLayout = new SpringLayout();
    getContentPane().setLayout(springLayout);

    JScrollPane scrollPane = new JScrollPane();
    springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.NORTH, getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.SOUTH, getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, getContentPane());
    getContentPane().add(scrollPane);

    ResultSet res = Operations.writeBalanceResult(value);
    JTable table = new JTable(Operations.writeResult(res)); 
    table.setAutoResizeMode( table.AUTO_RESIZE_OFF);
    scrollPane.setViewportView(table);




    for (int column = 0; column < table.getColumnCount(); column++) {
        TableColumn tableColumn = table.getColumnModel().getColumn(column);
        int preferredWidth = tableColumn.getMinWidth();
        int maxWidth = tableColumn.getMaxWidth();

        for (int row = 0; row < table.getRowCount(); row++) {
            TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
            Component c = table.prepareRenderer(cellRenderer, row, column);
            int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
            preferredWidth = Math.max(preferredWidth, width);

            if (preferredWidth >= maxWidth) {
                preferredWidth = maxWidth;
                break;
            }
        }

        TableCellRenderer headerRenderer = tableColumn.getHeaderRenderer();

        if (headerRenderer == null) {
            headerRenderer = table.getTableHeader().getDefaultRenderer();
        }

        Object headerValue = tableColumn.getHeaderValue();
        Component headerComp = headerRenderer.getTableCellRendererComponent(table, headerValue, false, false, 0, column);
        preferredWidth = Math.max(preferredWidth, headerComp.getPreferredSize().width);

        if (preferredWidth >= maxWidth) {
            preferredWidth = maxWidth;
            break;
        }

        tableColumn.setPreferredWidth(preferredWidth + 6);
    }



}
}

这是我无法改变颜色的唯一框架,它是在另一个类中创建的 -

menuItemBalanceSheet.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            balanceFrame = new BalanceFrame("BalanceSheet", value);

                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        balanceFrame.setSize(1200, 600);
                        balanceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        balanceFrame.setVisible(true);  
                    }
                });

我已经通过这样做了另一个成功改变颜色的面板

companyPanel = new CompanyPanel();
companyPanel.setBackground(Color.white);

2 个答案:

答案 0 :(得分:2)

您可以尝试使用scrollPane.getViewport().setBackground(Color.XXX)设置滚动窗格viewport的背景。

答案 1 :(得分:0)

尝试:

balanceFrame.setBackgroundColor(Color.RED);

OR

public void initializeProgressUpdate(){
if (shouldContinue){
    postDelayed(new Runnable() {
        @Override
        public void run() {

            //Do some stuff
            initializeProgressUpdate();
        }
    }, 1000);
}

}