【发布时间】:2014-10-26 01:56:53
【问题描述】:
我对这个家伙很茫然。我正在尝试创建一个自定义 LED 显示屏以显示 7 个排列为 8 的条。我有一个自定义 JComponent(条)显示在 JFrame 内部,但我无法让条显示在我的自定义面板内部创造。这是我的类中的构造方法和绘制方法的代码,以及我用来测试这些类的主要方法。
自定义 JComponent:
public class Bar extends JComponent
{
// instance variables - replace the example below with your own
private static boolean litUp = false;
private static boolean vertical = false;
private static boolean rotated = false;
private static boolean rotClockwise = false;
private static int positionX;
private static int positionY;
public Bar(boolean lit, boolean vert, int posX, int posY)
{
litUp = lit;
vertical = vert;
positionX = posX;
positionY = posY;
repaint();
System.out.println("The bar is being initialized");
}
public void paintComponent(Graphics g)
{
System.out.println("BAR: Paint Component being called");
super.paintComponent(g);
Graphics2D g2D = (Graphics2D)g;
if(vertical == true)
{
if(litUp == true)
{
g2D.setColor(Color.red);
}
else
{
g2D.setColor(Color.black);
}
g2D.drawRect(positionX , positionY, 10, 30);
g2D.fillRect(positionX , positionY, 10, 30);
System.out.println("BAR: fillRect is being called for a vertical bar");
if(rotated == true)
{
if(rotClockwise == true)
{
g2D.rotate(0.3398);
}
else
{
g2D.rotate(-0.3398);
}
}
}
else{
System.out.println("BAR: fillRect is being called for a horizontal bar");
if(litUp == true)
{
g2D.setColor(Color.red);
}
else
{
g2D.setColor(Color.black);
}
g2D.drawRect(positionX,positionY, 30, 10);
g2D.fillRect(positionX,positionY, 30, 10);
}
}
}
自定义 JPanel:
public class LED extends JPanel
{
// instance variables - replace the example below with your own
//private static Bar[] bars = new Bar[7];
//private static int xPos;
//private static int yPos;
private Bar barZero;
private Bar barOne;
private Bar barTwo;
private Bar barThree;
private Bar barFour;
private Bar barFive;
private Bar barSix;
/**
* Constructor for objects of class LED
*/
public LED()
{
barZero = new Bar(false, false, 0, 0);
this.add(barZero);
// barZero.setDirection(false);
barOne = new Bar(false, true, 0, 11);
this.add(barOne);
//barOne.setDirection(true);
barTwo = new Bar(false, true, 20, 11);
this.add(barTwo);
//barTwo.setDirection(true);
barThree = new Bar(false, false, 0, 42);
this.add(barThree);
//barThree.setDirection(false);
barFour = new Bar(false, true, 0, 53);
this.add(barFour);
//barFour.setDirection(true);
barFive = new Bar(false, true, 20, 53);
this.add(barFive);
//barFive.setDirection(true);
barSix = new Bar(false, false, 0, 64);
this.add(barSix);
//barSix.setDirection(false);
System.out.println("The LED class is being accessed");
repaint();
}
@ Override public void paintComponent(Graphics g)
{
System.out.println("LED: PaintComponent being called");
//barOne.paintComponent(g);
System.out.println("LED: barZero being painted| " + barZero.orientation() + "| " + barZero.coordX());
System.out.println("LED: barOne being painted| " + barOne.orientation() + "| " + barOne.coordX());
//barTwo.paintComponent(g);
System.out.println("LED: barTwo being painted| " + barTwo.orientation() + "| " + barTwo.coordX());
//barThree.paintComponent(g);
System.out.println("LED: barThree being painted| " + barThree.orientation() + "| " + barThree.coordX());
//barFour.paintComponent(g);
System.out.println("LED: barFour being painted| " + barFour.orientation() + "| " + barFour.coordX());
//barFive.paintComponent(g);
System.out.println("LED: barFive being painted| " + barFive.orientation() + "| " + barFive.coordX());
//barSix.paintComponent(g);
System.out.println("LED: barSix being painted| " + barSix.orientation() + "| " + barSix.coordX());
super.paintComponent(g);
}
}
以及测试方法:
public class DrawRect {
public static void main(String[] a) {
JFrame window = new JFrame();
LED led = new LED()
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(led);
window.setVisible(true);
}
}
我可以初始化一个 Bar 并让它显示在 Frame 中,但我无法让 LED(面板)在其中显示 bar。此外,这里是从测试组件中打印出来的字符串。我添加的所有条都没有设置它们的值:它们都是水平的,并且它们的 x 位置都设置为 0。我不是要放弃的人,但是这个程序让我想改变我的专业。
【问题讨论】:
-
为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)。这意味着一个包含导入的源文件(可能包含多个类文件),而不是三个。
-
自定义组件应覆盖
getPreferredSize()以向布局管理器提供有关大小的提示。 -
一个不相关的问题,但您的 Bar 类对 static 修饰符有多种不当使用。大多数这些字段应该是实例字段,而不是静态字段。
-
另一个建议:使用数组或列表将
Bars 存储在您的自定义面板中。这将使使用 for 循环绘制它们变得非常容易。 -
取出静态修饰符并将条添加到数组中。 Bar 类的 getPreferredSize() 方法有点模糊。该组件将有两种尺寸。您仍然建议将其覆盖吗?
标签: java swing jpanel jcomponent