[英]Setting Background Color of JPanel
所以我尝试使用getContentPane()。setBackground(Color.white),并尝试将表和scrollpane设置为白色。
@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);
您可以尝试使用scrollPane.getViewport().setBackground(Color.XXX)
设置滚动窗格视口的背景。
尝试:
balanceFrame.getContentPane().setBackgroundColor(Color.RED);
要么
balanceFrame.setBackgroundColor(Color.RED);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.