我有這個代碼就在這裏,這是一個按鈕,一個標籤,當按鈕被一個點擊標籤incrememnts:獲取值時的JButton在JLabel的增加值並投入數據庫
int helpfulNumber = content.getHelpful();
JButton helpfulBT = new JButton("Helpful");
reviewBoxPanel.add(helpfulBT);
JLabel helpfulLB = new JLabel("Helpful: " + helpfulNumber);
reviewBoxPanel.add(helpfulLB);
helpfulBT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
int helpfulNumber = content.getHelpful();
int newHelp = helpfulNumber + 1;
helpfulLB.setText("Helpful:" + newHelp);
}
});
helpfulLB.setText("Helpful: " + newHelp); // this doesn't work
在下面的代碼中,點擊提交時,我需要獲取標籤的值,但是新值爲helpfulNumber
。因爲它是現在,似乎只獲取標籤的舊值
final JButton submitBT = new JButton("Submit");
southPanel.add(submitBT);
submitBT.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == submitBT)
{
MovieReview some = new MovieReview();
some.setUser(userTF.getText());
some.setMovie(titleTF.getText());
some.setFeatured(featuredCB.isSelected());
some.setRating(Integer.parseInt(ratingTF.getText()));
some.setHelpful(helpfulNumber);
some.setUnhelpful(notHelpfulNumber);
some.setComments(reviewTA.getText());
some.setId(content.getId());
if(owner.updateReview(isUpdate, some))
{
// success
try {
MovieReviewDSC.edit(some);
//tableModel.fireTableRowsInserted(firstRow, lastRow);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(ReviewEditor.this, "Database succesfully updated!");
}
else
{
// fail
}
}
}
});
這樣的想法是,當這個框架是開放的,也有已經在那裏從content
值可能不爲0。目前,如果我點擊按鈕增加並點擊提交,號碼保持其原始值。
我試過在helpfulBT
actionListener之外再次使用helpfulLB.setText("Helpful: " + newHelp);
,但不能識別newHelp
變量。任何幫助將不勝感激,謝謝你的時間。
解由於@ tiago7和@Boosha
helpfulNumber = content.getHelpful();
JButton helpfulBT = new JButton("Helpful");
reviewBoxPanel.add(helpfulBT);
JLabel helpfulLB = new JLabel("Helpful: " + helpfulNumber);
reviewBoxPanel.add(helpfulLB);
helpfulBT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
helpfulNumber += 1;
helpfulLB.setText("Helpful:" + helpfulNumber);
}
});
而宣告在幀int helpfulNumber
。謝謝你們,我感謝幫助
如果我這樣做,我得到的錯誤:「在一個封閉的範圍定義的局部變量newHelp必須是最終或有效決賽」,如果我改變newHelp爲'最終詮釋newHelp'我得到「最終的局部變量newHelp不能分配,因爲它是在封閉類型中定義的「 – user3690863 2014-09-21 14:40:07
聲明它爲類屬性 – mobilepotato7 2014-09-21 14:42:42
完美,現在我想出了邏輯並使其工作。 – user3690863 2014-09-21 14:53:36