在同一windows java中显示多个文本
我想在同一窗口中显示多个文本,但只显示最后一个文本,这是我的2类:在同一windows java中显示多个文本,java,graphics,window,draw,drawstring,Java,Graphics,Window,Draw,Drawstring,我想在同一窗口中显示多个文本,但只显示最后一个文本,这是我的2类: import Test.Graph; import javax.swing.JFrame; public class InterfaceGraphique extends JFrame { private static final long serialVersionUID = 1L; public InterfaceGraphique() { this.setTitle("My f
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);
}
}
当我编译测试类时,我有一个只显示绿色“test3”的窗口。
谢谢你的帮助,很抱歉英语不好。
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”,
然后第二个将其替换为“正常”,
第三次用“valide”代替
您只会看到“valide”的结果,因为它是当前值和上次替换的值
您将需要一个可以保存“Graph”的数组或ArrayList的类
顺便说一句:欢迎来到Java
以下是课程:
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 javax.swing.JFrame;
公共类接口Graphique扩展JFrame{
私有静态最终长serialVersionUID=1L;
公共接口{
这个.setTitle(“我的第一个窗口”);
此.setSize(8001000);
此.setLocationRelativeTo(空);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//第一图形对象
图形=新图形();
//创建点;
点1=新点(“测试1”,150,300,“错误”);
点2=新点(“测试2”,250,400,“正常”);
点3=新点(“test3”,350500,“valide”);
图.添加点(点1);
图.添加点(点2);
图.添加点(点3);
这个.setContentPane(图形);
此.setVisible(true);
}
}
公共课点{
私有字符串fichier;
私人INTX;
私营企业;
私有字符串etat;
公共点(字符串fichier、int x、int y、字符串etat){
this.fichier=fichier;
这个.x=x;
这个。y=y;
this.etat=etat;
}
公共字符串getFichier(){
返回菲舍尔;
}
公共无效设置fichier(字符串fichier){
this.fichier=fichier;
}
公共int getX(){
返回x;
}
公共无效集合x(整数x){
这个.x=x;
}
公共int getY(){
返回y;
}
公共空间设置(整数y){
这个。y=y;
}
公共字符串getEtat(){
返回etat;
}
公共无效集合(字符串集合){
this.etat=etat;
}
}
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JPanel;
公共类图扩展了JPanel{
列表点=新的ArrayList();
@凌驾
公共组件(图形g){
System.out.println(“jesuis exécutée!”);
用于(点:点){
if(point.getEtat().contentEquals(“erreur”)){
Font Font=新字体(“Courier”,Font.BOLD,20);
g、 setFont(字体);
g、 setColor(Color.red);
}else if(point.getEtat().contentEquals(“正常”)){
Font Font=新字体(“Courier”,Font.BOLD,20);
g、 setFont(字体);
g、 设置颜色(颜色为黑色);
}else if(point.getEtat().contentEquals(“valide”)){
Font Font=新字体(“Courier”,Font.BOLD,20);
g、 setFont(字体);
g、 setColor(Color.green);
}
g、 抽绳(point.getFichier(),point.getX(),point.getX());
}
}
公共无效添加点(点-点){
点。添加(点);
}
公共静态void main(字符串[]args){
@抑制警告(“未使用”)
InterfaceGraphique ig=新的InterfaceGraphique();
}
}
以下是输出:
如果我能提供更多帮助,请告诉我。
谢谢。问题是下面几行代码实际上正在替换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”,
然后第二个将其替换为“正常”,
第三次用“valide”代替
您只会看到“valide”的结果,因为它是当前值和上次替换的值
您将需要一个可以保存“Graph”的数组或ArrayList的类
顺便说一句:欢迎来到Java
以下是课程:
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 javax.swing.JFrame;
公共类接口Graphique扩展JFrame{
私有静态最终长serialVersionUID=1L;
公共接口{
这个.setTitle(“我的第一个窗口”);
此.setSize(8001000);
此.setLocationRelativeTo(空);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//第一图形对象
图形=新图形();
//创建点;
点1=新点(“测试1”,150,300,“错误”);
点2=新点(“测试2”,250,400,“正常”);
点3=新点(“test3”,350500,“valide”);
图.添加点(点1);
图.添加点(点2);
图.添加点(点3);
这个.setContentPane(图形);
此.setVisible(true);
}
}
公共课点{
私有字符串fichier;
私人INTX;
私营企业;
私有字符串etat;
公共点(字符串fichier、int x、int y、字符串etat){
this.fichier=fichier;
这个.x=x;
这个。y=y;
this.etat=etat;
}
公共字符串getFichier(){
返回菲舍尔;
}
公共无效设置fichier(字符串fichier){
this.fichier=fichier;
}
公共int getX(){
返回x;
}
公共无效集合x(整数x){
这个.x=x;
}
公共int getY(){
返回y;
}
公共空间设置(整数y){
这个。y=y;
}
公共字符串getEtat(){
返回etat;
}
公共无效集合(字符串集合){
this.etat=etat;
}
}
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JPanel;
公共类图扩展了JPanel{
列表点=新的ArrayList();
@凌驾
公共组件(图形g){
System.out.println(“jesuis exécutée!”);
用于(点:点){
if(point.getEtat().contentEquals(“erreur”)){
Font Font=新字体(“Courier”,Font.BOLD,20);
g、 setFont(字体);
g、 setColor(Color.red);
}else if(point.getEtat().contentEquals(“正常”)){
Font Font=新字体(“Courier”,Font.BOLD,20);
g、 setFont(字体);
g、 设置颜色(颜色为黑色);
}else if(point.getEtat().contentEquals(“valide”)){
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");
}
}
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;
}
}