Java 单击项目选项卡时,如何刷新JTabbedPane选项卡?
我目前正在开发一个使用Java 单击项目选项卡时,如何刷新JTabbedPane选项卡?,java,swing,user-interface,jpanel,jtabbedpane,Java,Swing,User Interface,Jpanel,Jtabbedpane,我目前正在开发一个使用JTabbedPane的Java应用程序 我想刷新JTabbedPane中的选项卡,但当我单击“选项卡”项目时,该选项卡不会刷新 有什么问题吗 public class MainClass extends JFrame { private JTabbedPane tabbedPane; private JPanel panel1; private JPanel panel2; private JPanel
JTabbedPane
的Java应用程序
我想刷新JTabbedPane
中的选项卡,但当我单击“选项卡”项目时,该选项卡不会刷新
有什么问题吗
public class MainClass
extends JFrame
{
private JTabbedPane tabbedPane;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
public JPanel panel4;
private JPanel panel5;
public MainClass()
{
setTitle( "Demandes d'autorisation d'absence" );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize( 800 ,400 );
setBackground( Color.gray );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the tab pages
createPage1();
createPage2();
createPage3();
createPage4();
createPage5();
// Create a tabbed pane
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Mise à jour", panel1 );
tabbedPane.addTab( "Demandes en cours", panel2 );
tabbedPane.addTab( "Demandes autorisées", panel3 );
tabbedPane.addTab( "Demandes autorisées mise à jour", panel4 );
tabbedPane.addTab( "A propos", panel5 );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
public void createPage1()
{
panel1 = new JPanel();
panel1.setLayout( null );
JLabel label1 = new JLabel( "Mise à jour des autorisations :" );
label1.setBounds( 10, 15, 300, 20 );
panel1.add( label1 );
JButton b = new JButton("Actualiser");
b.setBounds( 10, 55, 150, 20 );
b.addActionListener(new miseajour());
panel1.add( b );
}
public void createPage2()
{
panel2 = new JPanel();
panel2.setLayout( null );
ArrayList columnNames = new ArrayList();
ArrayList data = new ArrayList();
// Connect to an MySQL Database, run query, get result set
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/Autorisations";
String user = "root";
String password = "root";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM Autorisations where etat='en cours'");
{
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.add( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
ArrayList row = new ArrayList(columns);
for (int i = 1; i <= columns; i++)
{
row.add( rs.getObject(i) );
}
data.add( row );
}
}}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
// Create Vectors and copy over elements from ArrayLists to them
// Vector is deprecated but I am using them in this example to keep
// things simple - the best practice would be to create a custom defined
// class which inherits from the AbstractTableModel class
Vector columnNamesVector = new Vector();
Vector dataVector = new Vector();
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
// Create table with database data
JTable table = new JTable(dataVector, columnNamesVector)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane sp = new JScrollPane( table );
sp.setBounds(10,25, 800, 1000);
panel2.add(sp);
}
public void createPage3()
{
....
}
public void createPage4()
{
....
}
public void createPage5()
{
...
}
// Main method to get things started
public static void main( String args[] )
{
// Create an instance of the test application
MainClass mainFrame = new MainClass();
mainFrame.setVisible( true );
}
public类MainClass
扩展JFrame
{
私有JTabbedPane选项卡窗格;
私人JPanel小组1;
私人JPanel小组2;
私人JPanel小组3;
公共JPanel小组4;
私人JPanel小组5;
公共类()
{
setTitle(“自动化和缺席需求”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(800400);
挫折地面(颜色:灰色);
JPanel-topPanel=新的JPanel();
setLayout(新的BorderLayout());
getContentPane().add(topPanel);
//创建选项卡页面
createPage1();
createPage2();
createPage3();
createPage4();
createPage5();
//创建选项卡式窗格
tabbedPane=新的JTabbedPane();
tabbedPane.addTab(“Miseájour”,panel1);
tabbedPane.addTab(“需求方”,第2组);
tabbedPane.addTab(“自动需求”,第3组);
tabbedPane.addTab(“自动需求”,第4组);
tabbedPane.addTab(“提案人”,第5组);
添加(选项卡窗格,BorderLayout.CENTER);
}
公共无效createPage1()
{
panel1=新的JPanel();
panel1.setLayout(空);
JLabel label1=新的JLabel(“自动回复时间:”);
标签1.立根(10,15,300,20);
面板1.添加(标签1);
JButton b=新JButton(“实施者”);
b、 立根(10,55,150,20);
b、 addActionListener(新的miseajour());
第1组:添加(b);
}
公共无效createPage2()
{
panel2=新的JPanel();
panel2.setLayout(空);
ArrayList columnNames=新的ArrayList();
ArrayList数据=新的ArrayList();
//连接到MySQL数据库,运行查询,获取结果集
连接con=null;
语句st=null;
结果集rs=null;
String url=“jdbc:mysql://localhost:3306/Autorisations";
字符串user=“root”;
字符串password=“root”;
试一试{
con=DriverManager.getConnection(url、用户、密码);
st=con.createStatement();
rs=st.executeQuery(“从自动命令中选择*,其中etat='en cours'”);
{
ResultSetMetaData md=rs.getMetaData();
int columns=md.getColumnCount();
//获取列名
对于(int i=1;i如果您想知道何时选择了选项卡,则可以使用ChangeListener
tabbedPane.addChangeListener(...);
然后,当事件触发时,您可以获取选项卡式窗格的选定索引并进行处理。刷新是什么意思?在第一个选项卡中,我执行一些SQL查询,在第二个选项卡中,我有一个Jtable,我希望在单击选项卡项时更新此表您必须执行并再次填充它。要更快地获得更好的帮助,请发布或者。硬编码一些数据来替换数据库。@Andrew Thompson我发布了代码