gpt4 book ai didi

java - 获取自定义 JPanel 以显示自定义 JComponents (Java Swing)

转载 作者:行者123 更新时间:2023-11-29 07:00:44 24 4
gpt4 key购买 nike

我对这个家伙感到很茫然。我正在尝试创建一个自定义 LED 显示屏以显示排列为 8 的 7 个栏。我有一个自定义 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);
}
}

我可以初始化一个条形并让它显示在框架中,但我无法让 LED(面板)在其中显示条形。此外,这是测试组件时打印出的字符串。我添加的所有条形都没有设置它们的值:它们都是水平的,它们的 x 位置都设置为 0。我不是要放弃的人,但这个程序让我想改变我的专业。

最佳答案

所以你很接近。我认为你能做的最好的事情就是比使用 ArrayList 更能降低复杂性,并且只使用已经存在的组件。下面是一个使用 Horizo​​ntalBox 的更简单示例。

我只展示了一种方向和一种尺寸,但我想你可以自己解决剩下的问题。 paintComponent() 例程中可能存在一个错误,该错误偏离一个像素。我无法在屏幕上眯着足够的眼睛来确定。

也不要忘记整个多线程的事情。请在事件调度线程上。我的 IDE 中有几个宏,基本上可以在大约两秒钟内为 EDT 构建代码。没有任何借口。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class LedBar extends JComponent
{
private final Dimension SIZE = new Dimension( 20, 50 );
private float value = 50f; // range 0-100

public void setValue( float value )
{
this.value = value;
}

public float getValue()
{
return value;
}

@Override
public void paintComponent( Graphics g ) {
super.paintComponent( g );
g.setColor( Color.RED );
g.drawRect( 0, 0, SIZE.width, SIZE.height );
g.setColor( Color.RED.darker() );
final int top = (int)(SIZE.height*(100f-value)/100f+1);
final int bottom = (int)(SIZE.height*(value)/100f-1);
g.fillRect( 1, top, SIZE.width-1, bottom);
}

@Override
public Dimension getPreferredSize() {
return SIZE;
}
}

class Test
{
public static void main( String[] args ) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JFrame frame = new JFrame();

Box hbox = Box.createHorizontalBox();
for (int i = 0; i < 5; i++) {
LedBar led = new LedBar();
led.setValue( i * 20f );
hbox.add( led );
}
frame.add( hbox );

frame.pack();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
} );
}
}

关于java - 获取自定义 JPanel 以显示自定义 JComponents (Java Swing),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com