gpt4 book ai didi

java - 如何在底部显示 jframe 中所有选项卡通用的按钮

转载 作者:行者123 更新时间:2023-12-02 04:38:15 24 4
gpt4 key购买 nike

我需要 jframe 中的 jbutton,它对所有选项卡都是通用的。

我尝试将相同的 jbutton 添加到 3 个选项卡,但该按钮仅在最后添加的选项卡中可见。

这是我完整的独立代码。如果您运行此代码,您将完全了解我的问题。

    final JTabbedPane tabs = new JTabbedPane();
JButton jbutton ;
JTable table; // Table 1 in 1st tab
JTable databaseTable; // Table 2 in 2nd tab
JTable serverDetailsTable; // Table 3 in 3rd tab
JFrame frame = new JFrame();
table = new JTable();
final boolean[] canEdit = new boolean [] {
false, true, true
};
String [] columnNames = {
"col1", "col2", "col3"};
Object[][] mappedDeploymentData=
{
{" ", "", ""},
{" ", "", ""}
};
DefaultTableModel tableModel = new DefaultTableModel(mappedDeploymentData, columnNames)
{
public boolean isCellEditable(int row, int column) {
return canEdit[column];
}
};
table.setModel(tableModel);

table.setModel(tableModel);
JScrollPane scrollPane = new JScrollPane(table);

jbutton = new JButton(" Start "); //Button i want for all tabs in a frame
jbutton.setBackground(Color.BLACK);
jbutton.setForeground(Color.YELLOW);
jbutton.setEnabled(true);

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(scrollPane);
panel.add(jbutton,BorderLayout.SOUTH);
table.getTableHeader().setReorderingAllowed(false);
tabs.add("Tab1", panel);

databaseTable = new JTable();
final boolean[] canEdit1 = new boolean [] {
false, false, true,true
};
String [] databaseColumns = {
"col1", "col2","col3"};
Object[][] mappedDatabaseData= {
{" ", "", ""},
{" ", "", ""}
};

DefaultTableModel mappedDatabaseTablemodel = new DefaultTableModel(mappedDatabaseData, databaseColumns)
{
public boolean isCellEditable(int row, int column) {
return canEdit1[column];
}
};

databaseTable.setModel(mappedDatabaseTablemodel);
JScrollPane databaseScrollPane = new JScrollPane(databaseTable);
JPanel databasePanel = new JPanel();
databasePanel.setLayout(new BorderLayout());
databasePanel.add(databaseScrollPane);
databaseTable.getTableHeader().setReorderingAllowed(false);
tabs.add("Tab2", databasePanel);


serverDetailsTable = new JTable();
final boolean[] canEdit2 = new boolean [] {
false, true
};
String [] server = {
" col1", " col2"};
Object[][] serverDeatils=
{
{"",""},
{"",""}
};

DefaultTableModel serverDatabaseTablemodel = new DefaultTableModel(serverDeatils, server)
{
public boolean isCellEditable(int row, int column) {
return canEdit2[column];
}
};
serverDetailsTable.setModel(serverDatabaseTablemodel);
JScrollPane serverScrollPane = new JScrollPane(serverDetailsTable);
serverScrollPane.setVisible(true);
JPanel serverPanel = new JPanel();
serverPanel.setLayout(new BorderLayout());
serverPanel.add(serverScrollPane);
serverDetailsTable.setTableHeader(null);
tabs.add("Tab3", serverPanel);



frame.setContentPane(tabs);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle("Frame");
frame.pack();
frame.setSize(900, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

如果您运行上述独立程序,您可以在第一个选项卡中看到该按钮,我需要在所有选项卡中显示相同的按钮。

enter image description here

最佳答案

您仅将按钮添加到第一个选项卡。

考虑创建一个主面板,它将作为框架的内容 Pane ,然后向其中添加选项卡 Pane 和按钮:

JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(tabs, BorderLayout.CENTER);
mainPanel.add(jbutton, BorderLayout.SOUTH);

frame.setContentPane(mainPanel);

同时删除以下行,因为您不想将按钮添加到某个特定选项卡:

panel.add(jbutton, BorderLayout.SOUTH); 

关于java - 如何在底部显示 jframe 中所有选项卡通用的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538150/

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