我在滚动窗格中使用 JTextArea 时遇到问题。我可以从文本文件中读取,但内容都显示在一行上。我尝试了各种方法,使用 append 并试图打破界限,但没有成功。
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class SplashScreen extends JFrame implements ActionListener
{
JButton mobile;
JButton browser;
JLabel welc;
JPanel home;
File file = new File("C:\\Users\\Jimbob\\Desktop\\DisWorkspace\\TrustWizard\\welcometwo.txt");
BufferedReader reader = null;
int lines = 10;
public String read()
{
String savetext = "";
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while((text = reader.readLine()) != null)
{
savetext += text;
}
}
catch(IOException jim)
{
jim.printStackTrace();
}
return savetext;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(mobile))
{
MobileHome openB = new MobileHome();
this.dispose();
}
if(e.getSource().equals(browser))
{
BrowserHome openB = new BrowserHome();
this.dispose();
}
}
public SplashScreen()
{
super("Trust Wizard");
JTextArea homeText = new JTextArea(25, 30);
homeText.setText(read());
JScrollPane homeScroll = new JScrollPane(homeText);
welc = new JLabel("Welcome To The Trust Wizard");
home = new JPanel();
mobile = new JButton("Mobile Wizard");
browser = new JButton("Browser Wizard");
home.add(welc);
home.add(homeScroll);
home.add(mobile);
home.add(browser);
ImageIcon img = new ImageIcon("hand.jpg");
setIconImage(img.getImage());
mobile.addActionListener(this);
browser.addActionListener(this);
getContentPane().add(home);
home.repaint();
setSize(450, 530);
setVisible(true);
}
public static void main(String args[])
{
SplashScreen test = new SplashScreen();
}
}