Java 如何将文件内容打印到GUI
对于我的程序,我们要做的主要事情之一就是将.csv文件的内容打印到GUI上。我已经弄清楚了GUI和排序方法,但是我们在弄清楚如何将文件打印到GUI上时遇到了困难。有什么办法吗?以下是GUI的代码:Java 如何将文件内容打印到GUI,java,swing,user-interface,Java,Swing,User Interface,对于我的程序,我们要做的主要事情之一就是将.csv文件的内容打印到GUI上。我已经弄清楚了GUI和排序方法,但是我们在弄清楚如何将文件打印到GUI上时遇到了困难。有什么办法吗?以下是GUI的代码: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.*; pu
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class FirstGUI2 {
private static JTextArea textAreaMon;
private static JTextArea textAreaTue;
private static JTextArea textAreaWed;
private static JTextArea textAreaThu;
private static JTextArea textAreaFri;
private static JTextArea description;
private static String gE1 = "Group Exam 1";
private static String gE2 = "Group Exam 2";
private static String gE3 = "Group Exam 3";
private static String gE4 = "Group Exam 4";
private static String gE5 = "Group Exam 5";
private static String gE6 = "Group Exam 6";
private static String gE7 = "Group Exam 7";
private static String gE8 = "Group Exam 8";
private static String gE9 = "Group Exam 9";
private Container pane;
final static boolean fill = true;
final static boolean weightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static String mon = "";
public static String tues = "";
public static String wed = "";
public static String thu = "";
public static String fri = "";
public static void addComponents(Container pane) {
if (RIGHT_TO_LEFT)
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
button = new JButton("Open File");
c.gridx = 1;
c.gridy = 1;
c.insets = new Insets(5, 5, 5, 5);
pane.add(button, c);
textAreaMon = new JTextArea(mon);
//textAreaMon.append(mon.substring(0));
textAreaMon.setLineWrap(true);
textAreaMon.setEditable(false);
c.gridx = 1;
c.gridy = 2;
pane.add(textAreaMon, c);
textAreaTue = new JTextArea(tues);
textAreaTue.setLineWrap(true);
textAreaTue.setEditable(false);
c.gridx = 1;
c.gridy = 3;
pane.add(textAreaTue, c);
textAreaWed = new JTextArea(wed);
textAreaWed.setLineWrap(true);
textAreaWed.setEditable(false);
c.gridx = 1;
c.gridy = 4;
pane.add(textAreaWed, c);
textAreaThu = new JTextArea(thu);
textAreaThu.setLineWrap(true);
textAreaThu.setEditable(false);
c.gridx = 1;
c.gridy = 5;
pane.add(textAreaThu, c);
textAreaFri = new JTextArea(fri);
textAreaFri.setLineWrap(true);
textAreaFri.setEditable(false);
c.gridx = 1;
c.gridy = 6;
pane.add(textAreaFri, c);
description = new JTextArea(
"Click the open button in order to select and import your .csv file. Your scehdule and times will be displayed in the schedule below.");
description.setEditable(false);
c.gridx = 1;
c.gridy = 0;
pane.add(description, c);
// attaching the file opener to the open file button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
new InterfacePanel();
}
});
}
//creates the frame and showing the GUI to the user
public static void makeGUI(){
JFrame frame = new JFrame("Final Exam Scheduler");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponents(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void printToGUI(){
//JText Fields
//textAreaMon
mon= "Monday: \n \n 8:00 - 10:00:\t Group Exam 1 \n 10:15-12:15:\t Group Exam 2 \n 12:45 - 2:45:\t";
while(!ArrayListsForClasses.time1.isEmpty()){
for(int i = 0; i < ArrayListsForClasses.time1.size(); i++){
mon += ArrayListsForClasses.time1.get(i);
mon += ", ";
}
}
mon += "\n 3:00 - 5:00:\t";
while(!ArrayListsForClasses.time2.isEmpty()){
for(int i = 0; i < ArrayListsForClasses.time2.size(); i++){
mon += ArrayListsForClasses.time2.get(i);
mon += ", ";
}
}
mon += "\n 5:30 - 7:30:\t Group Exam 3 \n \n 7:45 - 9:45:\t";
while(!ArrayListsForClasses.time3.isEmpty()){
for(int i = 0; i < ArrayListsForClasses.time3.size(); i++){
mon += ArrayListsForClasses.time3.get(i);
mon += ", ";
}
}
mon+= "\n \n";
// textAreaTues
tues = "Tuesday: \n \n 8:00 - 10:00: \t";
while (!ArrayListsForClasses.time4.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time4.size(); i++) {
tues += ArrayListsForClasses.time4.get(i);
tues += ", ";
}
}
tues += "\n 10:15 - 12:15:\t Group Exam 4 \n \n 12:45 - 2:45:\t ";
while (!ArrayListsForClasses.time5.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time5.size(); i++) {
tues += ArrayListsForClasses.time5.get(i);
tues += ", ";
}
}
tues += "\n 3:00 - 5:00:\t ";
while (!ArrayListsForClasses.time6.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time6.size(); i++) {
tues += ArrayListsForClasses.time6.get(i);
tues += ", ";
}
}
tues += "\n 5:30 - 7:30:\t ";
while (!ArrayListsForClasses.time7.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time7.size(); i++) {
tues += ArrayListsForClasses.time7.get(i);
tues += ", ";
}
}
tues += "\n 7:45 - 9:45:\t ";
while (!ArrayListsForClasses.time8.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time8.size(); i++) {
tues += ArrayListsForClasses.time8.get(i);
tues += ", ";
}
}
//TextAreaWed
wed = "Wednesday \n \n 8:00 - 10:00:\t";
while(!ArrayListsForClasses.time9.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time9.size(); i++) {
wed += ArrayListsForClasses.time9.get(i);
wed += ", ";
}
}
wed += "\n 10:15-12:15:\t";
while(!ArrayListsForClasses.time10.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time10.size(); i++) {
wed += ArrayListsForClasses.time10.get(i);
wed += ", ";
}
}
wed += "\n 12:45 - 2:45 :\t Group Exam 5 \n 3:00 - 5:00:\t";
while(!ArrayListsForClasses.time11.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time11.size(); i++) {
wed += ArrayListsForClasses.time11.get(i);
wed += ", ";
}
}
wed += "\n 5:30 - 7:30:\t";
while(!ArrayListsForClasses.time12.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time12.size(); i++) {
wed += ArrayListsForClasses.time12.get(i);
wed += ", ";
}
}
wed += "\n 7:45 - 9:45:\t";
while(!ArrayListsForClasses.time13.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time13.size(); i++) {
wed += ArrayListsForClasses.time13.get(i);
wed += ", ";
}
}
//TextAreaThurs
thu = "Thursday \n \n 8:00 - 10:00:\t";
while(!ArrayListsForClasses.time14.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time14.size(); i++) {
thu += ArrayListsForClasses.time14.get(i);
thu += ", ";
}
}
thu += "\n 10:15-12:15 :\t Group Exam 6 \n 12:45 - 2:45:\t";
while(!ArrayListsForClasses.time15.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time15.size(); i++) {
thu += ArrayListsForClasses.time15.get(i);
thu += ", ";
}
}
thu += "\n 3:00 - 5:00:\t";
while(!ArrayListsForClasses.time16.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time16.size(); i++) {
thu += ArrayListsForClasses.time16.get(i);
thu += ", ";
}
}
thu += "\n 5:30 - 7:30:\t Group Exam 7 \n 7:45 - 9:45:\t";
while(!ArrayListsForClasses.time17.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time17.size(); i++) {
thu += ArrayListsForClasses.time17.get(i);
thu += ", ";
}
}
//TextAreaFri
fri = "Friday \n \n 8:00 - 10:00:\t Group Exam 8 \n 10:15-12:15:\t";
while(!ArrayListsForClasses.time18.isEmpty()) {
for (int i = 0; i < ArrayListsForClasses.time18.size(); i++) {
fri += ArrayListsForClasses.time18.get(i);
fri += ", ";
}
}
fri += "\n 12:45 - 2:45:\t Group Exam 9";
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
makeGUI();
}
});
}
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.IOException;
导入javax.swing.*;
公共类FirstGUI2{
私有静态JTextArea textAreaMon;
私有静态jtextareatextareatue;
私有静态JTextArea textAreaWed;
私有静态JTextArea textAreaThu;
私有静态jtextareatextareafri;
私有静态区域描述;
私有静态字符串gE1=“组检查1”;
私有静态字符串gE2=“组考试2”;
私有静态字符串gE3=“组考试3”;
私有静态字符串gE4=“组检查4”;
私有静态字符串gE5=“组检查5”;
私有静态字符串gE6=“组检查6”;
私有静态字符串gE7=“组检查7”;
私有静态字符串gE8=“组检查8”;
私有静态字符串gE9=“组检查9”;
私家货柜窗格玻璃;
最终静态布尔填充=真;
最终静态布尔权重x=真;
最终静态布尔值从右到左=false;
公共静态字符串mon=”“;
公共静态字符串tues=“”;
公共静态字符串wed=“”;
公共静态字符串thu=”“;
公共静态字符串fri=“”;
公共静态void addComponents(容器窗格){
如果(从右到左)
窗格.setComponentOrientation(ComponentOrientation.RIGHT\u至\u LEFT);
按钮;
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 权重x=1;
c、 填充=GridBagConstraints.HORIZONTAL;
按钮=新的JButton(“打开文件”);
c、 gridx=1;
c、 gridy=1;
c、 插图=新插图(5,5,5,5);
窗格。添加(按钮,c);
textAreaMon=新的JTextArea(mon);
//textAreaMon.append(mon.substring(0));
textAreaMon.setLineWrap(true);
textAreaMon.setEditable(false);
c、 gridx=1;
c、 gridy=2;
添加(文本区域菜单,c);
textAreaTue=新的JTextArea(星期二);
textAreaTue.setLineWrap(true);
textAreaTue.setEditable(false);
c、 gridx=1;
c、 gridy=3;
添加(文本区域,c);
textAreaWed=新的JTextArea(wed);
textAreaWed.setLineWrap(true);
textAreaWed.setEditable(false);
c、 gridx=1;
c、 gridy=4;
添加(文本区域,c);
textAreaThu=新的JTextArea(thu);
textAreaThu.setLineWrap(真);
textAreaThu.setEditable(false);
c、 gridx=1;
c、 gridy=5;
窗格。添加(文本区域thu,c);
textAreaFri=新的JTextArea(fri);
textAreaFri.setLineWrap(真);
textAreaFri.setEditable(假);
c、 gridx=1;
c、 gridy=6;
窗格。添加(文本区域FRI,c);
description=新JTextArea(
“单击“打开”按钮以选择并导入.csv文件。您的规则和时间将显示在下面的计划中。”);
description.setEditable(false);
c、 gridx=1;
c、 gridy=0;
窗格。添加(说明,c);
//将文件开启器附加到“打开文件”按钮
addActionListener(新建ActionListener()){
已执行的公共无效行动(行动事件ae){
新接口面板();
}
});
}
//创建框架并向用户显示GUI
公共静态void makeGUI(){
JFrame=新JFrame(“期末考试计划”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponents(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
公共静态void printToGUI(){
//JText字段
//文本区域
mon=“星期一:\n\n 8:00-10:00:\t小组考试1\n 10:15-12:15:\t小组考试2\n 12:45-2:45:\t”;
而(!arraylistsforcasses.time1.isEmpty()){
for(int i=0;itextAreaMon = new JTextArea(mon);
textAreaMon.setText(mon);
String myCsvFile = "mySCVFile.csv"; //you can modify this based on where the file is located
BufferedReader br = null;
String line = "";
try {
br = new BufferedReader(new FileReader(myCsvFile));
while ((line = br.readLine()) != null) {
String[] columns = line.split(","); //you may do extra tweeks in here based on how you want to display the data
for(int x=0; x <columns.length(); x++){
description.append(columns[x]); //I guess this is the textarea you want to display the content of the CSV file
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
class TextAreaLoad extends JPanel
{
private JTextArea edit;
public TextAreaLoad()
{
setLayout( new BorderLayout() );
edit = new JTextArea(30, 60);
add(new JScrollPane(edit), BorderLayout.NORTH);
JButton read = new JButton("Read TextAreaLoad.txt");
read.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
FileReader reader = new FileReader( "TextAreaLoad.txt" );
BufferedReader br = new BufferedReader(reader);
edit.read( br, null );
br.close();
edit.requestFocus();
}
catch(Exception e2) { System.out.println(e2); }
}
});
add(read, BorderLayout.LINE_START);
JButton write = new JButton("Write TextAreaLoad.txt");
write.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
FileWriter writer = new FileWriter( "TextAreaLoad.txt" );
BufferedWriter bw = new BufferedWriter( writer );
edit.write( bw );
bw.close();
edit.setText("");
edit.requestFocus();
}
catch(Exception e2) { System.out.println(e2); }
}
});
add(write, BorderLayout.LINE_END);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("TextArea Load");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TextAreaLoad());
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}