gpt4 book ai didi

java - 自动选择 JTable 中的每隔一行

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

我正在尝试创建一个表格,其中每隔一行都被选中(主要是为了外观)。

我也想知道是否有办法将选择颜色更改为黑色,而不是默认的颜色,它似乎是蓝色。

这是我现在使用的:

public class Statistics {

private JFrame frame;
private JPanel contentPane;

public static void main(String[] arguments) {
new Statistics().construct();
}

public void construct() {
frame = new JFrame("Statistics");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(false);
frame.add(getComponents());
frame.pack();
frame.setVisible(true);
}

public JPanel getComponents() {
contentPane = new JPanel(new FlowLayout());
String[] columnNames = { "Statistic name", "Statistic value" };
Object[][] data = { { "Score", "0"}, {"Correct percentage", "100%" } };
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
contentPane.add(scrollPane);
return contentPane;
}
}

最佳答案

我认为 JTable 最简单和舒适的方法是寻找 prepareRenderer,然后在那里覆盖

if (isSelected) {
setBackground(myTable.getBackground); // Color.white
} else {
setBackground(whatever.Color);
}

对不起,我无法抗拒,你的代码是

import java.awt.*;
import javax.swing.*;

public class Statistics {

private JFrame frame;
private JPanel contentPane;
private String[] columnNames = {"Statistic name", "Statistic value"};
private Object[][] data = {{"Score", "0"}, {"Correct percentage", "100%"}};
private JTable table = new JTable(data, columnNames);
private JScrollPane scrollPane = new JScrollPane(table);

public Statistics() {
frame = new JFrame("Statistics");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(getComponents());
frame.setLocation(150, 150);
frame.pack();
frame.setVisible(true);
}

private JPanel getComponents() {
contentPane = new JPanel(new BorderLayout(10, 10));
table.getColumnModel().getColumn(0).setPreferredWidth(150);
table.getColumnModel().getColumn(1).setPreferredWidth(100);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}

public static void main(String[] arguments) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
Statistics statistics = new Statistics();
}
});
}
}

关于java - 自动选择 JTable 中的每隔一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8016457/

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