小编Vit*_*nko的帖子

如何在任务栏中进行JFrame警报?

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

我可以用Java做到这一点吗?

java alert taskbar

7
推荐指数
1
解决办法
1372
查看次数

将文件从src文件夹加载到阅读器中

我想知道我怎么可以将文件加载lol.txtsrc文件夹到我的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)

java directory load file src

4
推荐指数
1
解决办法
4万
查看次数

长度无法解析或不是字段(ArrayList 获取函数)

我的代码如下所示:

private ArrayList<Action> Actions;
private String Action;

Actions.get(Actions.length-1).getString();
Run Code Online (Sandbox Code Playgroud)

我在最后一行上不断收到错误消息(长度无法解析或不是字段)。目前这就是我的方法的样子,由于此错误,我无法真正继续:/

string field arraylist

2
推荐指数
1
解决办法
4911
查看次数

从文件中随机选择行

我正在尝试创建一种方法,可以选择 lol.txt(有 113 行)中的随机行并将其作为消息框发送出去。它应该如何工作:

  1. 生成从 0 到 112 的随机数
  2. for 循环应该遍历随机行数
  3. 将随机生成的行输出为消息框

在我的情况下,第 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)

如果您想将带有数组列表的解决方案发送给我,那很好,但我真的希望它是我最初计划的方式。

java random file line

1
推荐指数
1
解决办法
8886
查看次数

JFrame打开为空

我正在尝试创建一个程序,但它没有显示任何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)

java swing jframe

1
推荐指数
1
解决办法
2217
查看次数

无尽的java循环,同时采取输入

这里有一个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)

java loops infinite-loop while-loop

1
推荐指数
1
解决办法
990
查看次数

标签 统计

java ×5

file ×2

alert ×1

arraylist ×1

directory ×1

field ×1

infinite-loop ×1

jframe ×1

line ×1

load ×1

loops ×1

random ×1

src ×1

string ×1

swing ×1

taskbar ×1

while-loop ×1