如何读取csv文件并在java半透明摆动窗口中显示其内容?

时间:2014-07-23 05:40:42

标签: java swing csv

我需要使用java读取csv文件的内容并在swing窗口中显示它。我能够打印出csv内容,但是我无法看到swing窗口。此外,我希望内容在半透明窗口中打印。我的代码是这样的:

import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Swing extends JFrame {
private JButton aButton = new JButton("I am a Button \n");
public Swing() {
    setUndecorated(true);
    setBackground(new Color(0, 0, 0, 0));
    setSize(new Dimension(600, 500));
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (g instanceof Graphics2D) {
    final int R = 250;
    final int G = 210;
    final int B = 220;
    Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
                            0.0f, getHeight(), new Color(R, G, B, 255), true);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(p);
    g2d.fillRect(0, 0, getWidth(), getHeight());
    Font font = new Font("Serif", Font.PLAIN, 45);
    g2d.setFont(font);
    g2d.setColor(Color.red);
    g2d.drawString("Abcde",40,120); 
    g2d.drawString(line,150,200); 
    }
    }
    };
    setContentPane(panel);
    setLayout(new GridBagLayout());
    add(aButton);
    pack();
    }

    public void runn() {
    String csvFile = "D:\\Html1.csv";
    BufferedReader br = null;
    String line = "";
    String abc = "";
    try {
    br = new BufferedReader(new FileReader(csvFile));
    while (br.ready()) {
      line = br.readLine();
      System.out.println(line);
      abc = line + "--";
      }

    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println("Done");
    }
    public static void main(String[] args) {
        GraphicsEnvironment ge
                = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        boolean isPerPixelTranslucencySupported
                = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
        if (!isPerPixelTranslucencySupported) {
            System.out.println("Per-pixel translucency is not supported");
            System.exit(0);
        }
        SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
        Swing gtw = new Swing();
        gtw.runn();
        gtw.setVisible(true);
       }
    });
    }
    }

2 个答案:

答案 0 :(得分:1)

我在你的代码中发现了一些观点。

  • 您忘记在覆盖super.paintComponent()方法中致电paintComponent()
  • 您正在使用GridBagLayout但在添加组件时未使用GridBagConstraints
  • 您正在创建类Swing的对象两次。

请参阅How to Use GridBagLayout

上的 Swing Tutorial

答案 1 :(得分:1)

有一些基本缺陷......

让我们从main方法开始...

System.out.println("DStarting");
SwingIt swObj = new SwingIt();
swObj.runn();

没有意义,因为你完全忽略它......

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
    SwingIt gtw = new SwingIt();
    gtw.setVisible(true);
}

屏幕上显示的窗口不是您创建的窗口,并且调用runn,这意味着它基本上是不可见的(没有大小或位置)......

您应该使用它的构造函数,而不是使用swingText初始化窗口,而是使用swingText ....

您创建一个名为JTextField的{​​{1}},然后将其丢弃......

jj

这意味着您提供的文本永远不会显示在屏幕上......您似乎很难理解对象/变量引用...

无论如何,您应该丢弃JTextField jj=new JTextField(); jj.setText(txt); add(new JTextField("Text")); 方法并将其替换为类构造函数,例如......

swingText

现在这意味着您需要某种方式来设置private JButton aButton = new JButton("I am a Button \n"); private JLabel label = new JLabel("Label \n"); private JTextArea ta = new JTextArea("Text area", 5, 5); private JTextField jj = new JTextField(5); public SwingIt() { setUndecorated(true); setBackground(new Color(0, 0, 0, 0)); setSize(new Dimension(600, 500)); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); System.out.println("bbb"); if (g instanceof Graphics2D) { final int R = 250; final int G = 210; final int B = 220; Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 0.0f, getHeight(), new Color(R, G, B, 255), true); Graphics2D g2d = (Graphics2D) g; g2d.setPaint(p); g2d.fillRect(0, 0, getWidth(), getHeight()); } } }; setContentPane(panel); setLayout(new GridBagLayout()); add(aButton); add(label); add(ta); add(jj); pack(); } 字段的文本,为此,只需创建一个jj方法,例如......

setText

并使用public void setText(String text) { jj.setText(text); } 方法调用它

runn

返回public void runn() { String csvFile = "D:\\Html1.csv"; BufferedReader br = null; String line = ""; String abc = ""; try { br = new BufferedReader(new FileReader(csvFile)); while (br.ready()) { line = br.readLine(); System.out.println(line); abc = line + "--"; } setText(abc); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); } 方法...摆脱第一个main对象创建,只需使用第二个,确保在其上调用Swing ...

runn

enter image description here

正如已经指出的那样,在进行任何自定义绘画之前,您应该在public static void main(String[] args) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); if (!isPerPixelTranslucencySupported) { System.out.println("Per-pixel translucency is not supported"); System.exit(0); } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { SwingIt gtw = new SwingIt(); gwt.runn(); gtw.setVisible(true); } }); } 中调用super.paintComponent。从(至少)Java 1.4开始,传递给paintComponent方法的Graphics对象保证是paintComponent的实例,因此Graphics2D无意义

请查看Performing Custom Painting了解详情。

您还应该查看Laying Out Components Within a Container以了解如何在Swing中实际组布组件

由于其性质,CSV文件最好在if (g instanceof Graphics2D) {内展示,请查看How to Use Tables以获取更多详情......

您可能需要查看OpenCSV之类的内容,以便阅读您的CSV文件,而不是尝试重新发明已经非常复杂的问题...