简体   繁体   中英

Setting Background Color of JPanel

So I've tried using getContentPane().setBackground(Color.white), and tried setting the table and scrollpane to white as well.

@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);
    }



}
}

This is the only frame I can't get to change color, it's created here in another class -

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);  
                    }
                });

I've gotten another panel to successfully change color by doing this

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

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

Try:

balanceFrame.getContentPane().setBackgroundColor(Color.RED);

OR

balanceFrame.setBackgroundColor(Color.RED);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM