0
我只是尝试通过MySql生成按钮。 我得到了一个MySql数据库,我存储按钮相关数据(名称等) 现在我试图为每个条目生成一个自己的按钮。 Debug不会在控制台中打印,但按钮不会显示出来。任何想法或提示。想获得建议。我得到了一个类的swing和一个处理所有mysql请求的地方。(Java/MySQl)生成daynamic按钮
import javax.swing.*;
import java.awt.event.*;
public class swing {
static JFrame frame = new JFrame("MainMenu");
static JPanel panel = new JPanel();
public static void main(String[] args) {
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.add(panel);
JButton button = new JButton("Dursuchen");
panel.add(button);
button.addActionListener(new search());
}
public static void addButton(String name){
JPanel rowPanel = new JPanel();
System.out.println("addButton " + name);
JButton button = new JButton(name);
rowPanel.add(button);
//button.addActionListener(new search());
}
static class Action implements ActionListener{
public void actionPerformed(ActionEvent e){
JFrame frame2 = new JFrame("Clicked");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label = new JLabel();
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
static class search implements ActionListener{
public void actionPerformed(ActionEvent e){
Search.getAllFromKat(0);
}
}
}
MySql我调用addButton()的地方; 这部分是在另一个类中调用mysqlaccess.java
public static void showAllFromKat(int ID){
con = getInstance();
if(con != null){
Statement query;
try{
query = con.createStatement();
String sql =
"SELECT * FROM learn_themen_db";
ResultSet result = query.executeQuery(sql);
while(result.next()){
int KatID = result.getInt("Kat_ID");
String DisplayName = result.getString("Display_Name");
if(KatID == ID){
System.out.println(DisplayName);
swing.addButton(DisplayName);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
'在重新添加按钮后重新绘制()'? –
将rowPanel添加到JFrame中的位置在哪里? – josemr
rowPanel未添加我删除它。这只是一个测试。 Repaint()如何工作? – StonedLover