我需要使用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);
}
});
}
}
答案 0 :(得分:1)
我在你的代码中发现了一些观点。
super.paintComponent()
方法中致电paintComponent()
。GridBagLayout
但在添加组件时未使用GridBagConstraints
。Swing
的对象两次。答案 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
正如已经指出的那样,在进行任何自定义绘画之前,您应该在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文件,而不是尝试重新发明已经非常复杂的问题...