如何在 JPanel 上添加选项卡
我有一个 JPanel,上面有一张桌子。在同一个 JPanel 上,我想添加另一个表,但作为一个完全独立的表。因此,我希望顶部某处有两个选项卡按钮,以便在两个表之间进行交换。 (第一个选项卡保留在默认表格上,单击第二个选项卡将转到另一个表格)。
到目前为止我的代码如何进行:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.awt.Font;
import javax.swing.plaf.FontUIResource;
import java.util.Calendar;
import java.awt.Color;
import javax.swing.table.*;
class table extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
private static JFrame openFrame;
public table ()
{
//.....{some code work deleted to simplify}....
String columnNames[] = {"Time","Volume","Rate","CPP"};
String dataValues[][]= new String [counter][4];
for (int i= 0; i<counter; i++){
dataValues[i][0] =dataValues_timemap[i];}
for (int j = 0; j<counter; j++){
dataValues[j][1] = dataValues_vol[j]; }
for (int k = 0; k<counter; k++){
dataValues[k][2] = dataValues_rate[k]; }
for (int l = 0; l<countery; l++){
dataValues[l][3] = dataValues_cpp[l]; }
setTitle("Table Summary");
setSize(280,300);
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocation(1300,280);
table = new JTable( dataValues, columnNames );
scrollPane = new JScrollPane(table);
topPanel.add( scrollPane, BorderLayout.CENTER );
}
}
public static void main( String args[] )
{
// Create an instance of the test application
SimpleTableExample mainFrame = new SimpleTableExample();
mainFrame.setVisible( true );
}
}
I have a JPanel with a table on it. On that same JPanel, I want to add another table but as a totally separate table. So I want two tabs button somewhere on the top to interchange between the two tables. (first tab to stay on default table, clicking on second tab will go to the other table).
How do I go about in my code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.awt.Font;
import javax.swing.plaf.FontUIResource;
import java.util.Calendar;
import java.awt.Color;
import javax.swing.table.*;
class table extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
private static JFrame openFrame;
public table ()
{
//.....{some code work deleted to simplify}....
String columnNames[] = {"Time","Volume","Rate","CPP"};
String dataValues[][]= new String [counter][4];
for (int i= 0; i<counter; i++){
dataValues[i][0] =dataValues_timemap[i];}
for (int j = 0; j<counter; j++){
dataValues[j][1] = dataValues_vol[j]; }
for (int k = 0; k<counter; k++){
dataValues[k][2] = dataValues_rate[k]; }
for (int l = 0; l<countery; l++){
dataValues[l][3] = dataValues_cpp[l]; }
setTitle("Table Summary");
setSize(280,300);
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocation(1300,280);
table = new JTable( dataValues, columnNames );
scrollPane = new JScrollPane(table);
topPanel.add( scrollPane, BorderLayout.CENTER );
}
}
public static void main( String args[] )
{
// Create an instance of the test application
SimpleTableExample mainFrame = new SimpleTableExample();
mainFrame.setVisible( true );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
JTabbedPane
。它正好解决了这个问题。JTabbedPane
可以添加为JPanel
的子级。Use a
JTabbedPane
. It solves exactly this problem.The
JTabbedPane
can be added as a child of yourJPanel
.