在同一 windows java 中显示多个文本
Display many text in same windows java
我想在同一个 windows 中显示很多文本,但只显示最后一个文本,有我的 2 class :
import Test.Graph;
import javax.swing.JFrame;
public class InterfaceGraphique extends JFrame {
private static final long serialVersionUID = 1L;
public InterfaceGraphique() {
this.setTitle("My first Window");
this.setSize(800,1000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new Graph("test 1", 150,300,"erreur"));
this.setContentPane(new Graph("test 2 ", 250,400,"normal"));
this.setContentPane(new Graph("test3", 350,500,"valide"));
this.setVisible(true);
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
public class Graph extends JPanel {
private static final long serialVersionUID = 1L;
private String fichier;
private int x;
private int y;
String etat;
public Graph(String fichier, int x, int y, String etat) {
this.fichier = fichier;
this.x = x;
this.y = y;
this.etat= etat;
}
@Override
public void paintComponent(Graphics g){
System.out.println("Je suis exécutée !");
if(etat.contentEquals("erreur")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.red);
}
if(etat.contentEquals("normal")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.black);
}
if(etat.contentEquals("valide")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.green);
}
g.drawString(fichier, x, y);
}
public static void main(String[] args){
@SuppressWarnings("unused")
InterfaceGraphique ig = new InterfaceGraphique();
}
}
当我编译测试时 class 我有一个 window 只显示 "test 3" 为绿色。
感谢您的帮助,抱歉英语不好。
PS:我是 JAVA 的新手,尤其是 GUI 方面的新手,所以您可以告诉我其他错误以解决,谢谢!
问题是下面的代码行实际上替换了 ContentPane:
this.setContentPane(new Graph("test 1", 150,300,"erreur"));
this.setContentPane(new Graph("test 2 ", 250,400,"normal"));
this.setContentPane(new Graph("test3", 350,500,"valide"));
第一行设置为"erreur",
然后第二个将其替换为 "normal",
thrid 再次将其替换为 "valide"
您看到 "valide" 的结果只是因为它是当前值和上次替换值。
您将需要一个 class 可以容纳 "Graph" 的数组或 ArrayList。
顺便说一句:欢迎来到 Java..!!
这里是 classes:
import javax.swing.JFrame;
public class InterfaceGraphique extends JFrame{
private static final long serialVersionUID = 1L;
public InterfaceGraphique() {
this.setTitle("My first Window");
this.setSize(800, 1000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// First Grapth Object
Graph graph = new Graph();
// Create points;
Point point1 = new Point("test 1", 150, 300, "erreur");
Point point2 = new Point("test 2 ", 250, 400, "normal");
Point point3 = new Point("test3", 350, 500, "valide");
graph.addPoint(point1);
graph.addPoint(point2);
graph.addPoint(point3);
this.setContentPane(graph);
this.setVisible(true);
}
}
public class Point {
private String fichier;
private int x;
private int y;
private String etat;
public Point(String fichier, int x, int y, String etat) {
this.fichier = fichier;
this.x = x;
this.y = y;
this.etat= etat;
}
public String getFichier() {
return fichier;
}
public void setFichier(String fichier) {
this.fichier = fichier;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getEtat() {
return etat;
}
public void setEtat(String etat) {
this.etat = etat;
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class Graph extends JPanel{
List<Point> points = new ArrayList<Point>();
@Override
public void paintComponent(Graphics g){
System.out.println("Je suis exécutée !");
for (Point point : points) {
if(point.getEtat().contentEquals("erreur")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.red);
} else if(point.getEtat().contentEquals("normal")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.black);
}else if(point.getEtat().contentEquals("valide")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.green);
}
g.drawString(point.getFichier(), point.getX(), point.getX());
}
}
public void addPoint(Point point) {
points.add(point);
}
public static void main(String[] args){
@SuppressWarnings("unused")
InterfaceGraphique ig = new InterfaceGraphique();
}
}
这是输出:
如果我能提供更多帮助,请告诉我。
谢谢
我有最后一个问题,现在我想显示文件名,它在命令行中有效,但在图形界面中无效,你知道我必须添加什么吗?
import java.io.File;
导入java.io.IOException;
导入javax.swing.JFrame;
public class InterfaceGraphique 扩展 JFrame {
private static final long serialVersionUID = 1L;
public InterfaceGraphique() {
this.setTitle("My first Window");
this.setSize(800,1000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// First Graph Object
Graph graph = new Graph();
String Path = "./TEST";
try {
parcourir2(Path);
} catch (IOException e) {
e.printStackTrace();
}
this.setContentPane(graph);
this.setVisible(true);
}
public static void parcourir2 (String Path) throws IOException {
String[] Nom = {"Point1", "Point2", "Point3","Point4","Point5","Point6","Point7","Point8","Point9","Point10"};
Graph graph = new Graph();
File repertoire = new File(Path);
File[] liste = repertoire.listFiles();
int y = 150;
if (liste != null) {
for (int i = 0; i < liste.length; i++) {
if (liste[i].isDirectory()) {
parcourir2(liste[i].getAbsolutePath());
}
if(liste[i].isFile()) {
Point Nom[i] = new Point(liste[i].getName(),150,y,"normal");
graph.addPoint(Nom[i]);
System.out.println(liste[i] + "\n");
Test.TestFile.Afficher(liste[i]);
}
y=+50;
}
} else {
System.err.println( "Nom de repertoire invalide");
}
}
}
修改了InterfaceGraphique.java
package com.java.graphics;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
public class InterfaceGraphique extends JFrame{
private static final long serialVersionUID = 1L;
public InterfaceGraphique() {
System.out.println("Craetion");
this.setTitle("My first Window");
this.setSize(800, 1000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// First Grapth Object
Graph graph = new Graph();
try {
parcourir2("./Test", graph, new Integer(150));
} catch (IOException e) {
e.printStackTrace();
}
this.setContentPane(graph);
this.setVisible(true);
}
public static int parcourir2 (String Path, Graph graph, Integer xValue) throws IOException {
File repertoire = new File(Path);
File[] liste = repertoire.listFiles();
if (liste != null) {
for (int i = 0; i < liste.length; i++) {
if (liste[i].isDirectory()) {
xValue = parcourir2(liste[i].getAbsolutePath(), graph, xValue);
}
if(liste[i].isFile()) {
Point nom = new Point(liste[i].getName(),xValue, 150,"normal");
graph.addPoint(nom);
}
xValue = xValue + 50;
}
} else {
System.err.println( "Nom de repertoire invalide");
}
return xValue;
}
}
输出:
我想在同一个 windows 中显示很多文本,但只显示最后一个文本,有我的 2 class :
import Test.Graph;
import javax.swing.JFrame;
public class InterfaceGraphique extends JFrame {
private static final long serialVersionUID = 1L;
public InterfaceGraphique() {
this.setTitle("My first Window");
this.setSize(800,1000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new Graph("test 1", 150,300,"erreur"));
this.setContentPane(new Graph("test 2 ", 250,400,"normal"));
this.setContentPane(new Graph("test3", 350,500,"valide"));
this.setVisible(true);
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
public class Graph extends JPanel {
private static final long serialVersionUID = 1L;
private String fichier;
private int x;
private int y;
String etat;
public Graph(String fichier, int x, int y, String etat) {
this.fichier = fichier;
this.x = x;
this.y = y;
this.etat= etat;
}
@Override
public void paintComponent(Graphics g){
System.out.println("Je suis exécutée !");
if(etat.contentEquals("erreur")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.red);
}
if(etat.contentEquals("normal")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.black);
}
if(etat.contentEquals("valide")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.green);
}
g.drawString(fichier, x, y);
}
public static void main(String[] args){
@SuppressWarnings("unused")
InterfaceGraphique ig = new InterfaceGraphique();
}
}
当我编译测试时 class 我有一个 window 只显示 "test 3" 为绿色。 感谢您的帮助,抱歉英语不好。 PS:我是 JAVA 的新手,尤其是 GUI 方面的新手,所以您可以告诉我其他错误以解决,谢谢!
问题是下面的代码行实际上替换了 ContentPane:
this.setContentPane(new Graph("test 1", 150,300,"erreur"));
this.setContentPane(new Graph("test 2 ", 250,400,"normal"));
this.setContentPane(new Graph("test3", 350,500,"valide"));
第一行设置为"erreur", 然后第二个将其替换为 "normal", thrid 再次将其替换为 "valide"
您看到 "valide" 的结果只是因为它是当前值和上次替换值。
您将需要一个 class 可以容纳 "Graph" 的数组或 ArrayList。
顺便说一句:欢迎来到 Java..!!
这里是 classes:
import javax.swing.JFrame;
public class InterfaceGraphique extends JFrame{
private static final long serialVersionUID = 1L;
public InterfaceGraphique() {
this.setTitle("My first Window");
this.setSize(800, 1000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// First Grapth Object
Graph graph = new Graph();
// Create points;
Point point1 = new Point("test 1", 150, 300, "erreur");
Point point2 = new Point("test 2 ", 250, 400, "normal");
Point point3 = new Point("test3", 350, 500, "valide");
graph.addPoint(point1);
graph.addPoint(point2);
graph.addPoint(point3);
this.setContentPane(graph);
this.setVisible(true);
}
}
public class Point {
private String fichier;
private int x;
private int y;
private String etat;
public Point(String fichier, int x, int y, String etat) {
this.fichier = fichier;
this.x = x;
this.y = y;
this.etat= etat;
}
public String getFichier() {
return fichier;
}
public void setFichier(String fichier) {
this.fichier = fichier;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getEtat() {
return etat;
}
public void setEtat(String etat) {
this.etat = etat;
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class Graph extends JPanel{
List<Point> points = new ArrayList<Point>();
@Override
public void paintComponent(Graphics g){
System.out.println("Je suis exécutée !");
for (Point point : points) {
if(point.getEtat().contentEquals("erreur")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.red);
} else if(point.getEtat().contentEquals("normal")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.black);
}else if(point.getEtat().contentEquals("valide")) {
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.green);
}
g.drawString(point.getFichier(), point.getX(), point.getX());
}
}
public void addPoint(Point point) {
points.add(point);
}
public static void main(String[] args){
@SuppressWarnings("unused")
InterfaceGraphique ig = new InterfaceGraphique();
}
}
这是输出:
如果我能提供更多帮助,请告诉我。 谢谢
我有最后一个问题,现在我想显示文件名,它在命令行中有效,但在图形界面中无效,你知道我必须添加什么吗?
import java.io.File;
导入java.io.IOException;
导入javax.swing.JFrame;
public class InterfaceGraphique 扩展 JFrame {
private static final long serialVersionUID = 1L;
public InterfaceGraphique() {
this.setTitle("My first Window");
this.setSize(800,1000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// First Graph Object
Graph graph = new Graph();
String Path = "./TEST";
try {
parcourir2(Path);
} catch (IOException e) {
e.printStackTrace();
}
this.setContentPane(graph);
this.setVisible(true);
}
public static void parcourir2 (String Path) throws IOException {
String[] Nom = {"Point1", "Point2", "Point3","Point4","Point5","Point6","Point7","Point8","Point9","Point10"};
Graph graph = new Graph();
File repertoire = new File(Path);
File[] liste = repertoire.listFiles();
int y = 150;
if (liste != null) {
for (int i = 0; i < liste.length; i++) {
if (liste[i].isDirectory()) {
parcourir2(liste[i].getAbsolutePath());
}
if(liste[i].isFile()) {
Point Nom[i] = new Point(liste[i].getName(),150,y,"normal");
graph.addPoint(Nom[i]);
System.out.println(liste[i] + "\n");
Test.TestFile.Afficher(liste[i]);
}
y=+50;
}
} else {
System.err.println( "Nom de repertoire invalide");
}
}
}
修改了InterfaceGraphique.java
package com.java.graphics;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
public class InterfaceGraphique extends JFrame{
private static final long serialVersionUID = 1L;
public InterfaceGraphique() {
System.out.println("Craetion");
this.setTitle("My first Window");
this.setSize(800, 1000);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// First Grapth Object
Graph graph = new Graph();
try {
parcourir2("./Test", graph, new Integer(150));
} catch (IOException e) {
e.printStackTrace();
}
this.setContentPane(graph);
this.setVisible(true);
}
public static int parcourir2 (String Path, Graph graph, Integer xValue) throws IOException {
File repertoire = new File(Path);
File[] liste = repertoire.listFiles();
if (liste != null) {
for (int i = 0; i < liste.length; i++) {
if (liste[i].isDirectory()) {
xValue = parcourir2(liste[i].getAbsolutePath(), graph, xValue);
}
if(liste[i].isFile()) {
Point nom = new Point(liste[i].getName(),xValue, 150,"normal");
graph.addPoint(nom);
}
xValue = xValue + 50;
}
} else {
System.err.println( "Nom de repertoire invalide");
}
return xValue;
}
}
输出: