我是java的新手。我的大多数问题都没有得到答复,我不知道我的问题有什么问题。我希望这个问题得到一些答案。
所以,现在我正在创建一个框架,这个框架包含tabbedPane,tabbedPane包含两个JPanels。所有这些都是在一个函数createAndShowGUI()
还有另一个功能是使用for循环创建按钮,我想将这个新按钮添加到JPanel panel
。这是我的代码。我想将createButton()
方法创建的按钮添加到createAndShowGUI()
方法
public class Try extends JFrame {
static Connection conn;
//JPanel panel;
static JButton buttons;
static int totalRows;
static int recordPerPage = 1000000;
static int totalPages = 0;
private static JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
protected static void createAndShowGUI() throws SQLException {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setBounds(30, 50, 1300, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
frame.setContentPane(contentPane);
UIManager.put("TabbedPane.selected", Color.lightGray);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setBorder(new EmptyBorder(10, 10, 10, 10));
frame.add(tabbedPane);
JPanel panel = new JPanel();
tabbedPane.addTab("TABLE", null, panel, null);
tabbedPane.setFont( new Font( "Dialog", Font.BOLD|Font.ITALIC, 16 ) );
panel.setBackground(Color.white);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("GRAPH", null, panel_1, null);
panel_1.setBackground(Color.white);
JTable table = new JTable();
panel.add(table);
table.setFillsViewportHeight(true);
createDBConnection();
}
private static void createDBConnection() throws SQLException {
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection("jdbc:h2:file:G:/hs_temp/h2_db/test", "sa", "sa");
} catch (SQLException e) {
e.printStackTrace();
}
createPaginationButtons(conn);
}
private static void createPaginationButtons(Connection conn) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM cdr");
while (rs.next()) {
JOptionPane.showMessageDialog(null, rs.getInt(1));
totalRows = rs.getInt(1);
}
int v = totalRows % recordPerPage == 0 ? 0 : 1;
totalPages = totalRows / recordPerPage + v;
createButton(totalPages);
}
private static void createButton(int totalPages) {
JOptionPane.showMessageDialog(null, totalPages);
for(int i = 0; i < totalPages;) {
buttons = new JButton(""+(i+1) );
//JOptionPane.showMessageDialog(null, buttons);
panel.add(buttons);
i++;
}
}
}
任何想法,怎么做?