gpt4 book ai didi

java - JTable 未出现

转载 作者:行者123 更新时间:2023-12-01 14:24:05 25 4
gpt4 key购买 nike

每当我将面板的布局设置为FlowLayout时,JTable就会出现,但是我的imageBackground和按钮都放错了位置。当我将布局设置为 null 时,表格不会出现,但按钮和 imageBackground 位于我想要的位置。我该怎么办?

public class AssetPanel extends JPanel implements ActionListener{
private ArrayList<AssetDetails> assetList;
private Frame frame;
private Database db;

private JTable assetTable;
private JScrollPane scrollPane;

private JButton btnBack;
private JButton btnView;

public AssetPanel (Frame frame){
super();
this.frame = frame;
initialize();
}

public void initialize(){
setName("Assets");
setSize(700, 475);
setLayout(null);
setVisible(true);

db = new Database();

btnView = new JButton("View");
btnView.addActionListener(this);
btnView.setBounds(450, 400, 90, 20);
add(btnView);

btnBack = new JButton("Back");
btnBack.setFont(new Font("Tahoma", Font.BOLD, 12));
btnBack.setBounds(550, 400, 90, 20);
btnBack.addActionListener(this);
add(btnBack);

ImageIcon imageBackground = new ImageIcon(AssetPanel.class.getResource("/resources/assets.png"));
JLabel jlBackground = new JLabel(imageBackground);
jlBackground.setBounds(0,0, 700, 475);
add(jlBackground);
initializeTable();
}

@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == btnBack){
frame.changePanel("Main Menu");
}
}

public void initializeTable(){
Object[][] assetData;
assetList = new ArrayList<>();
String[] columnNames = {"Asset Name", "Date Acquired", "Type", "Classification"};
assetList = db.getAssetTable();

assetData = new Object[assetList.size()][columnNames.length];
for(int i = 0; i < assetList.size(); i++){
assetData[i][0] = assetList.get(i).getAssetName();
assetData[i][1] = assetList.get(i).getDateAcquired();
assetData[i][2] = assetList.get(i).getType();
assetData[i][3] = assetList.get(i).getClassification();
}

assetTable = new JTable(assetData, columnNames);
assetTable.setPreferredScrollableViewportSize(new Dimension(400, 100));
assetTable.setLocation(150, 100);
assetTable.setFillsViewportHeight(true);

scrollPane = new JScrollPane(assetTable);
add(scrollPane);
}
}

最佳答案

不要使用空布局或使用 setBounds() 方法来定位组件和调整组件大小。

however my imageBackground and buttons are misplaced

背景是一个容器组件。也就是说,您将其创建为组件并绘制图像作为背景。然后将其他组件添加到背景组件中。现在图像将出现在背景中,其他组件出现在其顶部。

请参阅Background Panel举一个创建背景组件的例子。

关于java - JTable 未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17302006/

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