gpt4 book ai didi

java - 在JTable中添加3D效果

转载 作者:行者123 更新时间:2023-11-30 03:58:22 25 4
gpt4 key购买 nike

我使用以下代码创建了一个 JTable:

    List<String> visibleColumns = new ArrayList<String>();
visibleColumns.add("Column 1");
visibleColumns.add("Column 2");
visibleColumns.add("Column 3");

DefaultTableModel tableModel = new DefaultTableModel(visibleColumns.toArray(),100);
JTable table = new JTable(tableModel);

JPanel panel = new SwingPaneTable();
panel.setBounds(0, 0, 280, 150);
panel.setLayout(new BorderLayout());
panel.add(table, BorderLayout.CENTER);
panel.add(table.getTableHeader(), BorderLayout.NORTH);

// Set Row Header
JList<String> rowHeader = new JList<String>();
rowHeader.setFixedCellWidth(18);
rowHeader.setFixedCellHeight(18);
rowHeader.setBackground(panel.getBackground());
rowHeader.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
JScrollPane scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scroll.setRowHeaderView(rowHeader);
panel.add(scroll, BorderLayout.CENTER);

表格看起来像这样:

enter image description here

现在,我需要在我的表格中添加此效果:

enter image description here

有人知道我如何在这段代码中执行此操作吗?

最佳答案

您似乎付出了很多似乎不需要的努力(恕我直言)...

JScrollPane 为您提供了向滚动 Pane 的每个角添加角以及提供行和列标题的方法...

参见http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

对于 JTable,它会自动将 JTableHeader 应用到 JScrollPane 的列标题区域。

Corners

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableModel;

public class TableExample {

public static void main(String[] args) {
new TableExample();
}

public TableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

List<String> visibleColumns = new ArrayList<String>();
visibleColumns.add("Column 1");
visibleColumns.add("Column 2");
visibleColumns.add("Column 3");

DefaultTableModel tableModel = new DefaultTableModel(visibleColumns.toArray(), 100);
JTable table = new JTable(tableModel);

JScrollPane scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JPanel left = new JPanel();
left.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
scroll.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, left);

JPanel right = new JPanel();
right.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
scroll.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, right);

JPanel columnHeader = new JPanel() {
@Override
public Dimension getPreferredSize() {
JScrollBar sb = new JScrollBar(JScrollBar.VERTICAL);
return new Dimension(sb.getPreferredSize().width, 10);
}
};
columnHeader.setBorder(new CompoundBorder(
new MatteBorder(1, 1, 0, 0, Color.WHITE),
new MatteBorder(0, 0, 1, 1, Color.GRAY)
));
scroll.setRowHeaderView(columnHeader);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(scroll);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

关于java - 在JTable中添加3D效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22543410/

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