所以我嘗試過使用getContentPane()。setBackground(Color.white), ,並嘗試將表和scrollpane設置爲white。設置JPanel的背景顏色
@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);
它是你試圖改變的平衡幀嗎? – Aaron
@Aaron是的,它是 – Zack
爲了更好的幫助,儘快發佈[mcve]。不知道爲什麼你的帖子有這麼多upvotes – MasterBlaster