我想知道Java或JFrame是否可以在程序中发生某些事情时提醒用户.例如,在Skype中收到新消息时,Skype图标背景将变为黄色.

我可以用Java做到这一点吗?
我想知道我怎么可以将文件加载lol.txt从src文件夹到我的close方法.到目前为止的代码:
public void close() throws IOException {
boolean loadFromClasspath = true;
String fileName = "..."; // provide an absolute path here to be sure that file is found
BufferedReader reader = null;
try {
if (loadFromClasspath) {
// loading from classpath
// see the link above for more options
InputStream in = getClass().getClassLoader().getResourceAsStream("lol.txt");
reader = new BufferedReader(new InputStreamReader(in));
} else {
// load from file system
reader = new BufferedReader(new FileReader(new File(fileName)));
}
String line = …Run Code Online (Sandbox Code Playgroud) 我的代码如下所示:
private ArrayList<Action> Actions;
private String Action;
Actions.get(Actions.length-1).getString();
Run Code Online (Sandbox Code Playgroud)
我在最后一行上不断收到错误消息(长度无法解析或不是字段)。目前这就是我的方法的样子,由于此错误,我无法真正继续:/
我正在尝试创建一种方法,可以选择 lol.txt(有 113 行)中的随机行并将其作为消息框发送出去。它应该如何工作:
在我的情况下,第 2 步不起作用,所以我希望有人能对此提出建议。这是代码:
public void close(){
try{
Random random = new Random();
int randomInt = random.nextInt(112);
FileReader fr = new FileReader("lol.txt");
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
Scanner scan = null;
for (int i = 0; i < randomInt + 1; i++) {
scan = new Scanner(line);
line = scan.nextLine();
}
JOptionPane.showMessageDialog(null,line);
}catch (IOException e){
JOptionPane.showMessageDialog(null,e.getMessage()+" for lol.txt","File Error",JOptionPane.ERROR_MESSAGE);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想将带有数组列表的解决方案发送给我,那很好,但我真的希望它是我最初计划的方式。
我正在尝试创建一个程序,但它没有显示任何JFrame奇怪的内容。代码似乎很合适,但 java 似乎很困惑什么的。到目前为止,这是我的代码:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Img extends JFrame{
/**
*
*/
private static final long serialVersionUID = -6362332275268668673L;
static JFrame panel = new JFrame();
private JButton next= new JButton("Next");
public Img(String a, String b){
ShowPng1(a,b);
}
public void ShowPng1(String a, String b) {
ImageIcon theImage = new ImageIcon("Icon_Entry_21.png");
panel.setSize(300, 300);
panel.setResizable(false);
JLabel label = new JLabel(a);
JLabel label2 = null; …Run Code Online (Sandbox Code Playgroud) 这里有一个while循环我有这个问题:
while((input = InputHandler.getInt()) != 1 && input != 2){
if(input == InputHandler.CODE_ERROR)
System.out.print("Input must be a number");
}
Run Code Online (Sandbox Code Playgroud)
这个while循环只接受一次输入而不再请求它,所以它循环使用整个时间输入的输入.我在这里做错了什么,因为对我来说这个wile循环工作真的很奇怪?
InputHandler类:
public class InputHandler {
public static Scanner in = new Scanner(System.in);
public static int CODE_ERROR = -6;
public static int getInt(){
try{
return in.nextInt();
} catch(InputMismatchException e){
return CODE_ERROR;
}
}
}
Run Code Online (Sandbox Code Playgroud)