0
我在JavaFX項目內的錨窗格內有一個JPanel。我遇到的問題是,在面板內加載的內容不侷限於面板,並在文字到達面板邊緣時切斷文本,而不是移動到下一行。內容不限於JPanel
這很好,但如果我測試Jframe內的面板。
使用Scene Builder for JavaFX創建了錨定面板並將其放置在用戶界面中。 下面是我製作測距字體大小標籤的代碼(這是創建一個Word Cloud)並將它們放入JPanel中,然後返回此面板。
public JPanel nnsePairWordCloud() throws SQLException, InstantiationException, IllegalAccessException {
Database databaseConnection = new Database();
databaseConnection.getConnection();
ResultSet nnsePairs = databaseConnection.getNNSEPairInfoFromDatabase();
ResultSet nnseScores = databaseConnection.getNNSESimilarityResultsFromDatabase();
//adding words from the database to the array that will make up the word cloud
while(nnsePairs.next()) {
String wordPair = nnsePairs.getString(1);
WORDS.add(wordPair);
}
//adding each pairs weighting from the database - this will determine the size of the word
while(nnseScores.next()) {
Double wordResult = nnseScores.getDouble(1);
WEIGHTINGS.add(wordResult);
}
System.out.println("cloud data got");
JPanel panel = new JPanel();
panel.setSize(380, 275);
Cloud cloud = new Cloud();
// cloud.setMinWeight(-1);
//cloud.setMaxWeight(1);
cloud.setMaxTagsToDisplay(300);
//Random random = new Random();
for (int i =0; i<WORDS.size(); i++) {
cloud.addTag(new Tag(WORDS.get(i), WEIGHTINGS.get(i)));
}
for (Tag tag : cloud.tags()) {
final JLabel label = new JLabel(tag.getName());
label.setOpaque(false);
label.setFont(label.getFont().deriveFont((float) tag.getWeight() * 10));
panel.add(label);
}
//frame.add(panel);
//frame.setSize(800, 600);
//frame.setVisible(true);
return panel;
}
這裏是面板返回並設置的代碼 - 首先創建一個SwingNode,然後加載內容。我必須這樣做才能在JavaFX UI中加載內容。
final SwingNode nodeForWordCloud = new SwingNode();
createAndSetSwingContentForWordCloud(nodeForWordCloud);
wordCloudBox.getChildren().add(nodeForWordCloud);
private void createAndSetSwingContentForWordCloud(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
swingNode.setContent(new WordCloud().initUI());
} catch (SQLException ex) {
Logger.getLogger(WordNetPairPageController.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(WordNetPairPageController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(WordNetPairPageController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
沒有更多的上下文和代碼,你的問題等同於「猜測我沒有展示給你的代碼有什麼問題」,而這個我們不可能回答。請改善你的問題。 [幫助]可以幫助以及[問]。 –
Thanks @HovercraftFullOfEels我已經編輯它並添加了代碼來嘗試和幫助我所要求的。感謝您的建議 :) – user3469624