所以我希望我的导入文本文件“Instructions.txt”被添加到JFrame中,供玩家查看他/她是否选择查看说明。我可以在屏幕的左边显示一个单词,但没有别的。此外,如果我通过拖动其中一个边来调整窗口大小,则会在左侧第一个单词的顶部显示多个单词。有什么想法吗?
//Battleship.java
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;
public class Battleship
{
public static void main (String[] args) throws IOException
{
String name = JOptionPane.showInputDialog("What is your name?");
String answer = JOptionPane.showInputDialog("Welcome to Battleship, "+name+". Would you like to see a set of instructions?");
if(answer.equals ("yes") || answer.equals ("Yes"))
{
TextFrame textframe = new TextFrame();
textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Scanner scan = new Scanner (new File("Instructions.txt"));
while (scan.hasNext())
{
textframe.displayText(scan.next());
}
JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Buttons b = new Buttons();
JPanel panel = new JPanel();
panel.add(b);
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}
else
{
JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Buttons b = new Buttons();
JPanel panel = new JPanel();
panel.add(b);
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}
}
}
//TextFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TextFrame extends JFrame
{
public TextFrame()
{
setTitle("Instructions");
setSize(400,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void displayText(String text)
{
JLabel Text = new JLabel(text);
add(Text);
}
}
答案 0 :(得分:0)
也许你应该在调用一个显示它的每个标记的方法之前阅读整个"Instructions.txt"
文件。声明一个变量,将文件的内容写入其中,然后显示它。如前面的答案所示,JLabel不是显示大量文本的最佳组件。
答案 1 :(得分:0)
此时您正在为文本文件的每一行添加新的JLabel
。所有这些JLabel
都会添加到框架的中心区域,替换之前添加的所有组件。因此,您看到的唯一单词是文件中的最后一个单词。您可以使用以下代码解决此问题:
Scanner scan = new Scanner (new File("Instructions.txt"));
StringBuilder sb = new StringBuilder();
while (scan.hasNext()) {
sb.append(scan.next());
}
textframe.displayText(sb.toString());
要在JLabel
中显示长文本,我还会将文本包装在html标记中:
public void displayText(String text) {
JLabel lblText = new JLabel("<html><body>" + text + "</body></html>");
add(lblText);
}
此外,我建议您使用BufferedReader
阅读您的文件。说实话,我从来没有使用扫描仪来读取文本文件,我不确定,如果这是一个好习惯。可以在此处找到一个示例:Reading and displaying data from a .txt file
答案 2 :(得分:0)
我用JTextArea更新了你的代码。我把它全部放在一个文件中,所以我删除了一些导入。你应该修理这些项目。
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Battleship {
public static void main(String[] args) throws IOException {
String name = JOptionPane.showInputDialog("What is your name?");
String answer = JOptionPane.showInputDialog("Welcome to Battleship, " + name + ". Would you like to see a set of instructions?");
if (answer.equals("yes") || answer.equals("Yes")) {
TextFrame textframe = new TextFrame();
JTextArea jta = new JTextArea();
jta.setEditable(false);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
textframe.add(jta);
textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Scanner scan = new Scanner(new File("Instructions.txt"));
StringBuilder sb = new StringBuilder();
while (scan.hasNextLine()) {
String line = scan.nextLine();
jta.append(line);
}
JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Buttons b = new Buttons();
JPanel panel = new JPanel();
// panel.add(b);
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
} else {
JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Buttons b = new Buttons();
JPanel panel = new JPanel();
// panel.add(b);
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
}
class TextFrame extends JFrame {
public TextFrame() {
setTitle("Instructions");
setSize(400, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void displayText(String text) {
JLabel Text = new JLabel(text);
add(Text);
}
}
答案 3 :(得分:-1)
有些组件可以为您处理此问题。从这里开始:
http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html