java 按行读入的文本如何在jtextfield中换行显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 按行读入的文本如何在jtextfield中换行显示相关的知识,希望对你有一定的参考价值。

我把jtextfield的大小设置为30,现在想在新的frame窗口里换行显示读取的text文件。请大侠指教,非常感谢!

//用JTextArea
//包你满意

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;

/**
* @author wsj
* @date 2010-5-27, 下午09:10:10
*/
public class JTextAreaTest extends JFrame implements ActionListener
JTextArea area=new JTextArea(20, 50);
private JButton btn=new JButton("读取文件到JTextArea...");

public JTextAreaTest(String title) throws HeadlessException
super(title);
setBounds(100,100,900,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
area.setFont(new Font("宋体", Font.PLAIN,16));
area.setForeground(Color.red);
add(new JScrollPane(area),BorderLayout.CENTER);
JPanel panel=new JPanel();
panel.add(btn);
btn.addActionListener(this);
add(panel,BorderLayout.SOUTH);
validate();


public static void main(String[] args)
new JTextAreaTest("JTextArea测试");


@Override
public void actionPerformed(ActionEvent e)
if(e.getSource()==btn)
new FileReadThread(this).start();//开启文件读取线程



/**
* 文件读取线程
*/
class FileReadThread extends Thread
private JTextAreaTest test;

public FileReadThread(JTextAreaTest test)
this.test = test;


@Override
public void run()
JFileChooser chooser=new JFileChooser("d:/");
chooser.setFileFilter(new FileFilter() //定义文件过滤器,仅显示文件夹和txt文本

@Override
public String getDescription()
return null;


@Override
public boolean accept(File file)
if(file.isDirectory()||file.getName().endsWith(".txt"))
return true;
return false;

);
int option=chooser.showOpenDialog(test);
if(option==JFileChooser.APPROVE_OPTION)
File selFile=chooser.getSelectedFile();
try
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(selFile),"gbk"));
String line=null;
while ((line=reader.readLine())!=null)
test.area.append(line+"\n");
Thread.sleep(30);//线程暂停,以看到读取过程效果

catch (Exception e1)
e1.printStackTrace();

JOptionPane.showMessageDialog(test, "读取完毕");




参考资料:还有其他问题的话,给我发百度消息

参考技术A 换行加上\n就可以了本回答被提问者采纳

单击提交后如何在带有java bean值的JTextField上设置文本

【中文标题】单击提交后如何在带有java bean值的JTextField上设置文本【英文标题】:how do you set text on JTextField that carries the java bean value after clicking submit 【发布时间】:2011-07-09 01:51:44 【问题描述】:

当单击一个类的 JButton(提交)时,另一个类应该替换 JTextField 的文本。新文本值是从 bean 收集的。新值出现在控制台上执行 system.out.println 但它为什么不替换 JTextField 中的值。当程序最初编译时,JTextField 被初始化为当前库存读取的内容,但是一旦用户提交新订单,JTextField 库存应该显示新的库存值。 (减去订购的书籍数量和当前库存)

inventoryTextField.setText(String.valueOf(currentInventory));

【问题讨论】:

请发布您的代码,不清楚您在问什么。 【参考方案1】:

这是我一直在使用的代码:

submit.addActionListener(new ActionListener() 
        public void actionPerformed(ActionEvent ae) 
            info.setBillingStreet(billingStreetAddressTextField.getText()); //This is to test the Edit menu item from the BookOrder class
            System.out.print(billingStreetAddressTextField.getText());
            info.setQuantityPurchase(quantityPurchaseTextField.getText());
            info.setCurrentInventory(Integer.parseInt(quantityPurchaseTextField.getText()));
            int n = info.getCurrentInventory();  //UPDATED VALUE
            inventory.createInventoryDetailsPanel(n);//PASSING THE UPDATED VALUE TO THE INVENTORY CLASS
        
    );


 import java.awt.FlowLayout;  
 import javax.swing.BoxLayout;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTabbedPane;
 import javax.swing.JTextField;

 public class Inventory extends JPanel 

private JTabbedPane pane = null;
final JTextField inventoryTextField = new JTextField();

OrderInfo info2 = new OrderInfo();
JPanel mainPanel = new JPanel();
JPanel panel2;
JPanel panel22;
int currentInventory = 5000;
int n;

public Inventory() 
    JPanel componentsPanel = new JPanel();
    componentsPanel.setLayout(new BoxLayout(componentsPanel,
            BoxLayout.PAGE_AXIS));
    componentsPanel.add(createInventoryPanel());
    add(mainPanel);
    mainPanel.setVisible(true);
    mainPanel.add(componentsPanel);
    add(mainPanel);


public JTabbedPane createInventoryPanel() 
    pane = new JTabbedPane();
    pane.addTab("Inventory Details", createInventoryDetailsPanel(n));
    pane.addTab("Inventory Reordering", createInventoryOrderingPanel());
    return pane;


JPanel createInventoryDetailsPanel(int n) 
    panel22 = new JPanel();
    mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    JPanel panel1 = new JPanel();
    panel2 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
    panel1.add(new JLabel("Item", JLabel.CENTER));
    panel1.add(new JLabel(" Java How To Program : Deitel and Deitel "));
    panel2.add(new JLabel("Quantity", JLabel.CENTER));
    panel2.add(inventoryTextField);
    System.out.print("Current Inventory is " + n);
    if (n != 0) //THIS IS WHERE I'M HAVING PROBLEMS TRYING TO PASS THE BEAN from the New Order class
                //SO THE NEW VALUE WILL SHOW IN THE TEXT FIELD FOR INVENTORY DETAILS. 
                //IT ALWAYS SHOWS 5000 NO MATTER WHAT (INITIALIZED ABOVE)
        inventoryTextField.setText(String.valueOf(n));
     else 
        inventoryTextField.setText(String.valueOf(currentInventory));
    
    panel22.add(panel1);
    panel22.add(panel2);
    return panel22;


private JPanel createInventoryOrderingPanel() 
    JPanel panel = new JPanel();
    JTextField txt1 = new JTextField(10);
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    JPanel panel1 = new JPanel();
    panel2 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
    panel1.add(new JLabel("Item", JLabel.CENTER));
    panel1.add(new JLabel(" Java How To Program : Deitel and Deitel "));
    panel2.add(new JLabel("Quantity", JLabel.CENTER));
    panel2.add(txt1);
    panel.add(panel1);
    panel.add(panel2);
    return panel;


【讨论】:

以上是关于java 按行读入的文本如何在jtextfield中换行显示的主要内容,如果未能解决你的问题,请参考以下文章

单击提交后如何在带有java bean值的JTextField上设置文本

java中如何实现在jtextfield中输入多行文字

从文本文件读取到 JTextfield

java 如何设置jTextField文本框只能输入数字?

java中怎么在文本框(JTextField)中输出Int型的数字

java如何获取输入框内容并赋值