![](/img/trans.png)
[英]Using a button in android to display 1 of 3 different lines of text in sequence, then different ones the next day
[英]How to display the next text lines once user presses the next button?
我正在嘗試創建一個程序,允許用戶從他們的目錄中選擇一個文本文件。 然后,它將顯示在使用Swing創建的Frame中的JTextArea
中。
我創建了一個帶有動作命令的按鈕,該按鈕應該允許用戶按下一次從文件中獲取下一個文本行,顯示在文本區域中,直到它到達文件末尾。
為此,我使用子字符串命令來瀏覽String變量並顯示它。 但它似乎不會這樣做,而只是顯示文件中找到的所有文本。
您可以在下面找到允許程序打開文件和顯示的代碼,以及為幫助瀏覽文本而創建的按鈕。
package reader;
public class Viewer extends JFrame{
private static final long serialVersionUID = 1L;
private static JFrame Frame;
private JPanel Cpanel;
JScrollPane scrollPane;
String book = "";
int currentChar = 10;
static JTextArea textArea;
JFileChooser fileChooser;
File f;
public static void DocViewer() {
new Viewer("new document");
}
public Viewer(String s) {
Frame = new JFrame("Reader");
textArea = new JTextArea(20,60);
textArea.setFont(new Font("Calibri", Font.PLAIN, 12));
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
f = new File("C://Program Files//Java//jdk1.6.0//bin//");
fileChooser = new JFileChooser(f);
Frame.getContentPane().setBackground(Color.red);
Frame.setLayout(new GridLayout(3, 1));
Cpanel = new JPanel();
Cpanel.setLayout(new FlowLayout());
Cpanel.setBackground(Color.RED);
JButton StButton = new JButton("open");
JButton QButton = new JButton("back");
JButton TestButton = new JButton("next");
TestButton.setHorizontalTextPosition(SwingConstants.LEFT);
StButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String book = "";
// int currentChar=10;
int returnVal = fileChooser.showOpenDialog(Viewer.this);
try {
File file = fileChooser.getSelectedFile();
FileInputStream fin = new FileInputStream(file);
BufferedReader d = new BufferedReader(
new InputStreamReader(fin, "UTF-8"));
book = "";
if (returnVal == JFileChooser.APPROVE_OPTION) {
while (book != null) {
book = d.readLine();
textArea.append(book + "\n");
System.out.println(book);
}
}
System.out.println("returnVal = " + returnVal
+ " and ba.fileChooser.APPROVE_OPTION = "
+ JFileChooser.APPROVE_OPTION);
fin.close();
} catch (Exception ex) {
}
}
});
QButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.exit(0);
Viewer.EXIT_ON_CLOSE();
Loader.main(null);
}
});
TestButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String page = "";
for (int l = 1; l >= 10; l = l + 2) {
page += book.substring(l, l + 49);
page += "\n";
currentChar += 50;
}
}
});
Cpanel.add(StButton);
Cpanel.add(QButton);
Cpanel.add(TestButton);
Frame.add(Cpanel, BorderLayout.NORTH);
Frame.add(scrollPane, BorderLayout.CENTER);
Frame.setSize(300,300);
Frame.setVisible(true);
}
}
ArrayList<String>
,每次添加一個新行。 我會這樣做一次,也許是在類構造函數中,如果你知道要提前讀取什么文件,可能是為了響應通過JFileChooser獲取文件,如果你不知道。 完成。
編輯
你問了更多問題,所以我會嘗試將這些步驟細分一下:
ArrayList<String>
作為實例字段(非靜態類變量) 如果仍然卡住, 請進一步分解您的步驟,並嘗試找到您被困的位置。 谷歌的解決方案,如果仍然卡住,請帶着您的具體問題和代碼來到這里。
另外,您將需要學習和使用Java命名約定 。 變量名都應以較低的字母開頭,而類名以大寫字母開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.