晚上好,
我正在尝试完成一个涉及JFrame的简单学校GUI项目,但我坚持正确地返回除数,然后用feedback.setText()部分显示它们。说明如下:
编写一个创建GUI的java代码,以接受来自用户的整数作为输入,并创建输入值的所有除数。您的框架应包含以下组件:
JLabel:输入一个整数来查找所有除数
JTextField:读取用户的输入
JButton:开始这个过程
JLabel:显示输出< ---- Main Issue
import tweepy
import time
import os
import json
import simplejson
search_term = 'word1'
search_term2= 'word2'
search_term3='word3'
lat = "xxxx"
lon = "xxxx"
radius = "xxxx"
location = "%s,%s,%s" % (lat, lon, radius)
API_key = "xxxx"
API_secret = "xxxx"
Access_token = "xxxx"
Access_token_secret = "xxxx"
auth = tweepy.OAuthHandler(API_key, API_secret)
auth.set_access_token(Access_token, Access_token_secret)
api = tweepy.API(auth)
c=tweepy.Cursor(api.search,
q="{}+OR+{}".format(search_term, search_term2, search_term3),
rpp=1000,
geocode=location,
include_entities=True)
data = {}
i = 1
for tweet in c.items():
data['text'] = tweet.text
print(i, ":", data)
i += 1
time.sleep(1)
wfile = open(os.getcwd()+"/workk2.txt", mode='w')
data = {}
i = 0
for tweet in c.items():
data['text'] = tweet.text
wfile.write(data['text']+'\n')
i += 1
wfile.close()
答案 0 :(得分:1)
您有一些问题
divisor
,也没有将价值分配给任何内容,因此divisors
始终是null
divisors
中隐藏了divisor
实例字段,因此您分配给它的任何内容都不会反映在其他地方divisors
,因此您只会获得最后一个值首先,我要稍微更改divisor
来证明我的意思......
private String divisor(int num) {
StringJoiner sj = new StringJoiner(", ");
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
sj.add(Integer.toString(i));
}
}
return sj.toString();
}
首先,我删除了divisors
的声明并将其替换为StringJoiner
,它更简单并且效果更好,但突出了阴影问题。
接下来,我更新了actionPerformed
方法以实际调用divisor
方法并将结果分配给divisors
@Override
public void actionPerformed(ActionEvent e) {
try {
num = Integer.parseInt(answer.getText());
if (e.getSource().equals(find)) {
divisors = divisor(num);
feedback.setText("The divisors are: " + divisors);
}
} catch (NumberFormatException ex) {
feedback.setText("Invalid input. Enter an integer!");
answer.setText("");
}
}