gpt4 book ai didi

java - 缩进列名

转载 作者:行者123 更新时间:2023-11-30 07:13:44 26 4
gpt4 key购买 nike

enter image description here

我正在制作一个 GUI,我试图在其中缩进列名。我还粘贴了我的源代码。我正在使用 GridBagConstraints。这是我创建 GridBagConstraints 的代码,但似乎什么也没发生

 private final static Dimension VOLUME_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension NAME_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension IMAGE_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension DATE_FIELD_DIMENSION = new Dimension(150, 25);
private final static Dimension STATUS_FIELD_DIMENSION = new Dimension(100, 25);
private final static Dimension IMAGE_PANEL_DIMENSION = new Dimension(1000,250);
private static void ImagePanel(ArrayList<ResourceListObject> arr, JPanel imagelist)
{


for(int i=0 ; i < arr.size();i++)
{
try {
// column 1: name
JTextField t1 = createTextField(arr.get(i).getName(), NAME_FIELD_DIMENSION);

// column 2: Created At
JTextField t2 = createTextField(arr.get(i).getCreatedDate(), DATE_FIELD_DIMENSION);


// column 3: Status
JTextField t3 = createTextField(arr.get(i).getStatus(), STATUS_FIELD_DIMENSION);

// column 4: Image Id
JTextField t4 = createTextField(arr.get(i).getID(), IMAGE_FIELD_DIMENSION);

//column 5:Volume Id
JTextField t5 = createTextField(arr.get(i).getVolume_id(), VOLUME_FIELD_DIMENSION);
// construct the row (panel), and then add it to the list panel
JPanel rowPanel = new JPanel();
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.X_AXIS));
rowPanel.add(t1);
rowPanel.add(t2);
rowPanel.add(t3);
rowPanel.add(t4);
rowPanel.add(t5);
imagelist.add(rowPanel);
} catch (Exception e) {
}

}


}

private static JTextField createTextField(String text, Dimension d) {
JTextField tf = new JTextField(text);
tf.setPreferredSize(d);
tf.setBackground(Color.WHITE);
tf.setEditable(false);
return tf;
}

private static JPanel getColumnHeadings() {

GridBagConstraints regularFieldConstraints = new GridBagConstraints();
regularFieldConstraints.fill = GridBagConstraints.BOTH;
String nameColumn = "Image Name";
String secondColumn = "Created At";
String thirdColumn = "Status";
String fourthColumn = "Image Id";
String fifthColumn = "Volume Id";

JLabel name = new JLabel(nameColumn);
JLabel created_at = new JLabel(secondColumn);
JLabel status = new JLabel(thirdColumn);
JLabel id = new JLabel(fourthColumn);
JLabel volume_id = new JLabel(fifthColumn);

name.setPreferredSize(NAME_FIELD_DIMENSION);
created_at.setPreferredSize(DATE_FIELD_DIMENSION);
status.setPreferredSize(STATUS_FIELD_DIMENSION);
id.setPreferredSize(IMAGE_FIELD_DIMENSION);
volume_id.setPreferredSize(VOLUME_FIELD_DIMENSION);

JPanel headingsPanel = new JPanel(new GridBagLayout());
headingsPanel.add(name, regularFieldConstraints);
headingsPanel.add(created_at, regularFieldConstraints);
headingsPanel.add(status, regularFieldConstraints);
headingsPanel.add(id, regularFieldConstraints);
headingsPanel.add(volume_id, regularFieldConstraints);

return headingsPanel;
}

最佳答案

sun.swing.table.DefaultTableCellHeaderRenderer 类不是已发布 API 的一部分。如果您不愿意依赖它,请考虑以下替代选项:

image

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class Demo {

/** @see https://stackoverflow.com/a/6644956/230513 */
private static class HeaderRenderer implements TableCellRenderer {

TableCellRenderer renderer;

public HeaderRenderer(JTable table) {
renderer = table.getTableHeader().getDefaultRenderer();
}

@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
// assumes JLabel
JLabel label = (JLabel) renderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
label.setHorizontalAlignment(JLabel.LEADING);
return label;
}
}

private void initGUI() {
Object[] columnNames = new Object[]{
"Image Name", "Created At", "Status", "Image Id", "Volume Id"};
DefaultTableModel dtm = new DefaultTableModel(columnNames, 0);
dtm.addRow(new Object[]{"Some picture's name", "2013-10-05",
"Status unknown", "0123456789", "9876543210"});

JTable table = new JTable(dtm);
table.getTableHeader().setDefaultRenderer(new HeaderRenderer(table));

JScrollPane scrollPane = new JScrollPane(table);

JPanel content = new JPanel(new BorderLayout());
content.setPreferredSize(new Dimension(600, 250));
content.add(scrollPane);

JFrame frame = new JFrame("Demo");
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().initGUI();
}
});
}
}

关于java - 缩进列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19195145/

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