【发布时间】:2019-02-01 08:02:06
【问题描述】:
我创建了一个 JScrollPane 并向其中添加了一个 JTable。我希望能够使用垂直滚动条滚动到表格底部。 scollpane 与 table init 一起显示,但是单击顶部或底部箭头不会将视图分别推进到顶部或底部。垂直滚动轨道中也没有滚动条显示。
我尝试将视口设置为不同的大小,还使用不同的构造函数来创建 jscrollpane,即 jscrollpane(table_3, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);但没有一个有效。
public class test {
JFrame f;
int myMonth, myDay, myYear;
private JTable tblAppts;
private String[] colTimes = { "Time", "Test 1", "Test 2", "Test 3" };
private Object[][] myTimes = { { "Time", "Test 1", "Test 2", "Test 3" }, {
"00:30", "", "", "" },
{ "01:00", "", "", "" }, { "01:30", "", "", "" }, { "02:00", "",
"", "" }, { "02:30", "", "", "" },
{ "03:00", "", "", "" }, { "03:30", "", "", "" }, { "04:00", "",
"", "" }, { "04:30", "", "", "" },
{ "05:00", "", "", "" }, { "05:30", "", "", "" }, { "06:00", "",
"", "" }, { "06:30", "", "", "" },
{ "07:00", "", "", "" }, { "07:30", "", "", "" }, { "08:00", "",
"", "" }, { "08:30", "", "", "" },
{ "09:00", "", "", "" }, { "09:30", "", "", "" }, { "10:00", "",
"", "" }, { "10:30", "", "", "" },
{ "11:00", "", "", "" }, { "11:30", "", "", "" }, { "12:00", "",
"", "" }, { "12:30", "", "", "" },
{ "13:00", "", "", "" }, { "13:30", "", "", "" }, { "14:00", "",
"", "" }, { "14:30", "", "", "" },
{ "15:00", "", "", "" }, { "15:30", "", "", "" }, { "16:00", "",
"", "" }, { "16:30", "", "", "" },
{ "17:00", "", "", "" }, { "17:30", "", "", "" }, { "18:00", "",
"", "" }, { "18:30", "", "", "" },
{ "19:00", "", "", "" }, { "19:30", "", "", "" }, { "20:00", "",
"", "" }, { "20:30", "", "", "" },
{ "21:00", "", "", "" }, { "21:30", "", "", "" }, { "22:00", "",
"", "" }, { "22:30", "", "", "" },
{ "23:00", "", "", "" }, { "23:30", "", "", "" } };
private JTable tblTime;
private JTable table_1;
private JTable table_2;
private JTable table_3;
// set the table widths method
public static void setJTableColumnsWidth(JTable table, int
tablePreferredWidth, double... percentages) {
double total = 0;
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
total += percentages[i];
}
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
TableColumn column = table.getColumnModel().getColumn(i);
column.setPreferredWidth((int) (tablePreferredWidth
(percentages[i] / total)));
}
}
test() {
f = new JFrame("WSDP");// creating instance of JFrame#
// open the frame in a maximized state ie max vert and horizontal
f.setSize(1000, 1000);
f.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
// menus
JPanel panel = new JPanel();
panel.setBounds(0, 0, 982, 27);
// create border
Border blackline = BorderFactory.createLineBorder(Color.black);
panel.setBorder(blackline);
f.getContentPane().add(panel);
// icon buttons panel
JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 26, 109, 593);
panel_1.setBorder(blackline);
f.getContentPane().add(panel_1);
JScrollPane scrollPane = new JScrollPane();
// scrollPane.setBounds(107, 78, 475, 875);
scrollPane.setBounds(107, 78, 475, 500);
// scrollPane.setPreferredSize(new Dimension(450, 110));
scrollPane.setBorder(blackline);
scrollPane.setViewportBorder(blackline);
scrollPane.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
table_3 = new JTable(myTimes, colTimes);
scrollPane.setRowHeaderView(table_3);
table_3.setFillsViewportHeight(true);
scrollPane.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
table_3.setRowHeight(20);
table_3.setFont(new Font("Courier", Font.BOLD, 14));
setJTableColumnsWidth(table_3, 480, 10, 30, 30, 30);
f.getContentPane().add(scrollPane);
f.setVisible(true);
}
public static void main(String[] args) {
test t = new test();
}
}
【问题讨论】:
-
用完整的程序代码编辑
-
问题 #1
f.getContentPane().setLayout(null);- 这通常是您的问题的根本原因。JScrollPane依赖于组件preferredSize来确定何时显示(以及显示多少)滚动条。由于JPanel的默认preferredSize是0x0,因此它不会显示任何内容 -
好的,在这个混乱进一步发展之前,您需要仔细阅读How to Use Tables 和How to Use Scroll Panes,因为您对 API 的工作原理有一些基本的误解。我还建议您通读Laying Out Components Within a Container,因为它会解决您即将面临的许多其他问题
-
f.getContentPane().setLayout(null);Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。 -
非常感谢您的帮助,终于成功了。我是一名新的 Java 程序员,我很欣赏有关阅读表格和滚动窗格使用的建议。
标签: java swing jtable jscrollpane