gpt4 book ai didi

java - 设置JPanel的背景色

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:52 29 4
gpt4 key购买 nike

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

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

最佳答案

您可以尝试设置滚动面板的背景viewport通过使用 scrollPane.getViewport().setBackground(Color.XXX)

关于java - 设置JPanel的背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41388952/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com