gpt4 book ai didi

java - 使用数据库创建 jcomponents

转载 作者:行者123 更新时间:2023-11-29 06:40:17 25 4
gpt4 key购买 nike

我想使用数据库动态创建 jcomponents。当我打开任何 jframe 或 jpanel 组件时,如 jlabel、jtextfields、jcombobox 等应该通过读取数据库行来创建。我对如何从数据库值(即 String 中的 jcomponent 对象)提供引用感到困惑。这是我的数据库表

enter image description here

    try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","pass");
stat = con.createStatement();
ResultSet rs = stat.executeQuery("select * from design");
while(rs.next()){
jTextField1 = new JTextField();
jTextField1.setSize(rs.getInt("height"),rs.getInt("width"));
jTextField1.setLocation(rs.getInt("x"), rs.getInt("y"));
}
rs.close();
stat.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}

这是演示表。我知道这行不通,因为我无法与对象和数据库通信。我想在 jframe 上打印 jcomponents。我将编写 for 循环以多次打印它们。请帮助我。

最佳答案

首先请看@AndrewThompson 的明智建议:

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.

有一些有助于理解其含义的主题:

您会看到极力反对使用 setLocation()setBounds()setSize() 等方法。但是,我在应用允许自定义表单之前已经看到了这种方法。但是您可以存储 GridBagLayout 的约束,而不是特定的 (x,y) 坐标和固定的 (width,height) .假设您有一个这样的表:

enter image description here

我首先使用一个类来包装来自数据库的数据:

public class Data {
private String componentType, text;
private int column, row, width, height, weightX, weightY;

public Data(String componentType, int column, int row, int width, int height
,int weightX, int weightY, String text) {

this.componentType = componentType;
this.column = column;
this.row = row;
this.width = width;
this.height = height;
this.weightX = weightX;
this.weightY = weightY;
this.text = text;
}

// getters and setters here
}

由于数据库调用是一项耗时的任务,您必须考虑使用 SwingWorker在后台线程中执行数据库调用(耗时任务)并在 Event Dispatch Thread 中创建/更新您的 GUI .

说到这里你可能会有这样的事情:

public class Demo {

private JPanel content;
private JFrame frame;

private void createAndShowGUI() {
content = new JPanel(new GridBagLayout());

SwingWorker<Void, Data> worker = new SwingWorker<Void, Data>() {
@Override
protected Void doInBackground() {
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","password");
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery("select * from TableName");
while(rs.next()){
String componentType = rs.getString("component");
int column = rs.getInt("x");
int row = rs.getInt("y");
int width = rs.getInt("width");
int height = rs.getInt("height");
int weightx = rs.getInt("weightx");
int weighty = rs.getInt("weighty");
String text = rs.getString("text");
Data data = new Data(componentType, column, row, width, height
,weightx, weighty, text);
publish(data);
}
rs.close();
stat.close();
con.close();
} catch(Exception e) {
System.out.println(e);
}

return null;
}

@Override
protected void process(List<Data> chunks) {
for(Data data : chunks) {

JComponent component = null;
if(data.getComponentType().equalsIgnoreCase("JTextField")) {
component = new JTextField(data.getText());
}

if(data.getComponentType().equalsIgnoreCase("JComboBox")) {
component = new JComboBox();
}

if(data.getComponentType().equalsIgnoreCase("JLabel")) {
component = new JLabel(data.getText());
}

if(component != null) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = data.getColumn();
constraints.gridy = data.getRow();
constraints.gridwidth = data.getWidth();
constraints.gridheight = data.getHeight();
constraints.weightx = data.getWeightX();
constraints.weighty = data.getWeightY();

constraints.anchor = GridBagConstraints.WEST;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(8,8,8,8);
content.add(component, constraints);
}

}
}

@Override
protected void done() {
frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
};

worker.execute();
}


public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}

你会看到这样的东西:

enter image description here

关于java - 使用数据库创建 jcomponents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com