如何在屏幕上打印扫描仪的结果

时间:2022-01-20 21:52:51

I have a scanner that can read a txt file that I am trying to print on the screen. I am trying to do this by printing the scanner (scan) results of what the user typed in on the on the screen using Graphics g.

我有一个扫描仪,可以读取我试图在屏幕上打印的txt文件。我试图通过使用Graphics g打印用户在屏幕上输入的内容的扫描仪(扫描)结果来做到这一点。

Any Ides on how to do this a different way Thank You :)

任何关于如何以不同方式做到这一点的Ides谢谢:)

try {
    Scanner scan = new Scanner(new File("/Users/AlexSpallone/Desktop/name.txt"));
      msg = " Your Name is" + scan;
     } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

3 个解决方案

#1


2  

You should call the nextLine method on your Scanner object:

您应该在Scanner对象上调用nextLine方法:

try {
Scanner scan = new Scanner(new File("/Users/AlexSpallone/Desktop/name.txt"));
  msg = " Your Name is" + scan.nextLine();
 } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

#2


1  

Take a look at How to use text areas

看看如何使用文本区域

String msg = null;
try {
    Scanner scan = new Scanner(new File("/Users/AlexSpallone/Desktop/name.txt"));
    msg = " Your Name is" + scan.nextLine();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
JTextArea ta = new JTextArea(msg);
add(new JScrollPane(ta));

Equally, if it's just a small String, you could just use a JLabel. See How to use labels for more details

同样,如果它只是一个小字符串,你可以使用JLabel。有关详细信息,请参见如何使用标签

I'd also take a look at Creating a UI with Swing

我还要看看使用Swing创建UI

#3


0  

Use this code:

使用此代码:

public class MyPanel extends JPanel {
    public void paintComponenet(Graphics g) {
        super.paintComponent(g);
        try {
            Scanner scan = new Scanner(new File("/Users/AlexSpallone/Desktop/name.txt"));
            msg = " Your Name is" + scan.nextLine();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        g.drawString(20, 20, msg);
    }
}

Then add an object of this class to a JFrame.

然后将此类的对象添加到JFrame。

#1


2  

You should call the nextLine method on your Scanner object:

您应该在Scanner对象上调用nextLine方法:

try {
Scanner scan = new Scanner(new File("/Users/AlexSpallone/Desktop/name.txt"));
  msg = " Your Name is" + scan.nextLine();
 } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

#2


1  

Take a look at How to use text areas

看看如何使用文本区域

String msg = null;
try {
    Scanner scan = new Scanner(new File("/Users/AlexSpallone/Desktop/name.txt"));
    msg = " Your Name is" + scan.nextLine();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
JTextArea ta = new JTextArea(msg);
add(new JScrollPane(ta));

Equally, if it's just a small String, you could just use a JLabel. See How to use labels for more details

同样,如果它只是一个小字符串,你可以使用JLabel。有关详细信息,请参见如何使用标签

I'd also take a look at Creating a UI with Swing

我还要看看使用Swing创建UI

#3


0  

Use this code:

使用此代码:

public class MyPanel extends JPanel {
    public void paintComponenet(Graphics g) {
        super.paintComponent(g);
        try {
            Scanner scan = new Scanner(new File("/Users/AlexSpallone/Desktop/name.txt"));
            msg = " Your Name is" + scan.nextLine();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        g.drawString(20, 20, msg);
    }
}

Then add an object of this class to a JFrame.

然后将此类的对象添加到JFrame。