几天前,我瞥了一眼。
在需要使用 JTable 的那一刻,我感到很头疼,最近我看到了 JSplitPane 的实用程序,我想到了一个想法,但真诚的,我不知道从哪里开始获得好的结果.
我想要的结果,也许会对其他人有所帮助,是一个使用 JScrollPane + JPanel + JSplitPane + GridBagLayout + 可能还有其他组件创建的表格,我们希望可以轻松放置其他组件,例如 JButton、图像或我们想要的其他组件,因为我想成为JPanel的物理单元。
在我看来,这是一个可观的列表示例,也可以在此论坛上找到:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.MatteBorder;
public class DynamicPanelList {
public static void main(String[] args) {
new DynamicPanelList();
}
public DynamicPanelList() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ListPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ListPane extends JPanel {
private JPanel mainList;
public ListPane() {
setLayout(new BorderLayout());
this.mainList = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.weighty = 1;
this.mainList.add(new JPanel(), gbc);
this.add(new JScrollPane(mainList));
JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();
panel.add(new JLabel("Hello"));
panel.setBorder(new MatteBorder(0, 0, 1, 0, Color.GRAY));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
ListPane.this.mainList.add(panel, gbc, 0);
ListPane.this.validate();
ListPane.this.repaint();
}
});
add(add, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
在这段代码中,我想添加一些功能来添加标题(不是很困难),但困难的部分是添加可以调整大小的列。这是我想我需要使用 JSplitPane 的部分。如果我的想法是错误的,请发表评论,如果您有想法,即使是很小的想法,也请与我们分享。
接下来的日子,我会尝试在这里添加更多代码来更详细地解释我的想法。
谢谢,我很抱歉我的“洞穴”英语技能。