首先看到@ AndrewThompson的明智忠告:
Java的圖形用戶界面可能需要在多個平臺上工作,使用不同PLAFs不同 屏幕分辨率&。因此,他們不是 有利於組件的準確放置。要組織可靠的GUI組件 ,請改爲使用佈局管理器或它們的組合,以及用於空白區域的佈局填充&邊框。
有一些有用的主題,明白它的意思在這裏:
你會看到使用諸如setLocation()
,setBounds()
或setSize()
的方法是非常不鼓勵的。不過,在應用允許定製表單之前,我已經看到了這種方法。但是,您可以存儲GridBagLayout的約束,而不是特定的(x,y)座標和固定的(寬度,高度)。比方說,你有這樣的表:你必須
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
}
由於數據庫調用耗時的任務:
我帶班從DB包數據第一次啓動考慮使用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();
}
});
}
}
,你會看到這樣的事情:
那你試試這麼遠嗎?至少是否解決了數據庫連接部分?任何代碼顯示? – dic19
我也在嘗試請不要關閉此問題。我使用正常的結果集爲例如:'jtextfield a = new jtextfiled();''和'a.setSize(rs.getInt(height),rs.getInt(width));' –
請投票重新打開,因爲這個問題已被編輯關閉後。 – dic19