在JPanel上按行和列顺序动态创建Button

时间:2018-11-26 06:20:46

标签: java jpanel

Java Dynamically Button Created on JPanel

我希望该动态按钮按行和列排序。当它到达我的显示器边缘时,它将移至下一列。有什么想法吗?这是我的代码。对不起,我的英语不好。

package tes;
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class prog extends JFrame {

static prog myFrame;
JPanel mainPanel;

public void createAndShowGUI(){
    myFrame = new prog();
    myFrame.prepareUI();
    myFrame.pack();
    myFrame.setVisible(true); 
}

private void prepareUI() {
    mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    mainPanel.add(new subPanel());
    getContentPane().add(mainPanel, BorderLayout.CENTER);
    myFrame.pack();
}
private class subPanel extends JPanel {

    public subPanel me;

    public subPanel() {
        super();
        me = this;
    try {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/progkeg", "root", "konoha89");
        Statement stm = con.createStatement();
        String sql = ("SELECT CONCAT(`program`.`kode_prog`,`program`.`nama_prog`) FROM program" );
        ResultSet rs = stm.executeQuery(sql);

        while (rs.next()){ 
        JButton progbtn = new JButton(rs.getString(1));
        add(progbtn);
        }

    }catch(SQLException e){JOptionPane.showMessageDialog(null,e);}  

    }
  }
}

0 个答案:

没有答案