我有一个程序,可以在不同位置显示一个类的许多元素。为此,我多次使用该类的构造函数。但是问题在于,仅显示了最后一个元素,而没有显示其他元素。
框架的初始化以及构造函数的调用位置:
private void initComponents() {
setExtendedState(MAXIMIZED_BOTH);
int width = (int) getContentPane().getBounds().getWidth();
int height = (int) getContentPane().getBounds().getHeight();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new gleis(0, 0, 0, 0));
add(new gleis(1, 0, 1, 0));
add(new gleis(2, 0, 2, 0));
this.setVisible(true);
}
元素的类称为“ gleis”:
public class gleis extends JPanel {
int xposition;
int yposition;
int id;
int status;
public gleis(int xpos, int ypos, int id, int status){
this.xposition = xpos * 11;
this.yposition = ypos * 11 + 4;
this.id = id;
this.status = status;
System.out.println(this.id);
}
public void paintComponent(Graphics g1) {
super.paintComponent(g1);
Graphics2D g = (Graphics2D) g1;
g.setColor(Color.black);
g.fillRect(this.xposition, this.yposition, 11, 4);
if (this.status == 0)
g.setColor(Color.gray);
else if (this.status == 1)
g.setColor(Color.red);
else if (this.status == 2)
g.setColor(Color.yellow);
else
g.setColor(Color.gray);
g.fillRect(this.xposition + 2, this.yposition + 1, 7, 2);
}
}
那么如何显示我想要拥有的所有元素?
最佳答案
您可能会将组件添加到JFrame的contentPane中,该组件使用BorderLayout作为其默认布局,因此,最后输入的组件将覆盖所有其他组件,因为它们默认被添加到BorderLayout.CENTER位置。
根据您的代码和对用例的假设,建议更改整个方法:
使用一个将JPanel扩展为绘图JPanel的单一类,并覆盖其paintComponent方法。
将此JPanel添加到您的JFrame。
将您的gleis类的名称更改为Gleis,使其符合Java命名约定,并使其成为逻辑类,而不是GUI组件类,因此不要覆盖paintComponent。
给Gleis一个public void draw(Graphics2D g2)
方法。
将尽可能多的Gleis对象添加到图形JPanel中,然后通过迭代图形JPanel所保存的Gleis的ArrayList并在每个Gleis实例上调用draw(...)
来在JPanel的paintComponent方法中绘制它们。
例如:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class GleisMain {
private static void createAndShowGui() {
GleisPainter gleisPainter = new GleisPainter();
gleisPainter.addGleis(new Gleis(10, 10, 0, 0));
gleisPainter.addGleis(new Gleis(20, 20, 1, 1));
gleisPainter.addGleis(new Gleis(30, 30, 2, 2));
// I try to avoid having classes extend JFrame and instead use JFrames when needed
JFrame frame = new JFrame("Gleis Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(gleisPainter);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
// a single class that extend JPanel and that draws all the Gleis objects
class GleisPainter extends JPanel {
// an ArrayList to hold all the Gleis objects
private List<Gleis> gleisList = new ArrayList<>();
// to allow outside classes to add Gleis objects
public void addGleis(Gleis gleis) {
gleisList.add(gleis);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// loop through our gleis list and draw each instance
Graphics2D g2 = (Graphics2D) g;
for (Gleis gleis : gleisList) {
gleis.draw(g2); // by calling its draw(...) method
}
}
}
// don't have this extend JPanel
class Gleis {
// fields should be private
private int xposition;
private int yposition;
private int id;
private int status;
public Gleis(int xpos, int ypos, int id, int status) {
this.xposition = xpos * 11;
this.yposition = ypos * 11 + 4;
this.id = id;
this.status = status;
System.out.println(this.id);
}
// give this a public draw method where each instance can draw itself
public void draw(Graphics2D g2) {
g2.setColor(Color.black);
// you'll want to avoid "magic" numbers like you're using here:
g2.fillRect(this.xposition, this.yposition, 11, 4);
if (this.status == 0)
g2.setColor(Color.gray);
else if (this.status == 1)
g2.setColor(Color.red);
else if (this.status == 2)
g2.setColor(Color.yellow);
else
g2.setColor(Color.gray);
// ditto about use of "magic" numbers
g2.fillRect(this.xposition + 2, this.yposition + 1, 7, 2);
}
}