gpt4 book ai didi

java - 向 JFrame 添加更多组件

转载 作者:行者123 更新时间:2023-12-01 15:26:23 30 4
gpt4 key购买 nike

单键(在代码 KeyboardButtons 中)扩展了 JComponent。当我尝试将 JFrame 添加到主 JFrame 时,我可以对单个键执行此操作,当尝试添加另一个键时,第一个键不会显示。

你能看一下代码告诉我问题出在哪里吗?

MainFrame.java:

package keyboard;

import java.awt.*;
import javax.swing.JFrame;

public class MainFrame extends JFrame {


public MainFrame() throws HeadlessException {
setTitle("Keyboard");
setSize(1024, 768);

}

public static void main(String[] args) throws InterruptedException {
JFrame mainWindow = new MainFrame();
mainWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);

Point left5 = new Point(210, 210);
Point left4 = new Point(410, 110);
Point left3 = new Point(580, 120);
Point left2 = new Point(680, 200);
Point left1 = new Point(800, 500);

Keyboard kb = new Keyboard(left1, left2, left3, left4, left5);
KeyboardButton[] buttons = kb.registerKeys();

Container c = mainWindow.getContentPane();
c.add(buttons[0]);
c.add(buttons[1]);
mainWindow.setVisible(true);

}


}

KeyboardButton.java:

package keyboard;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.*;
import javax.swing.JComponent;

public class KeyboardButton extends JComponent implements MouseListener {

Polygon polygon;
boolean isActive;
final Color ACTIVE_COLOR = Color.red;
final Color INACTIVE_COLOR = Color.blue;

public KeyboardButton(Polygon p) {
polygon = p;
addMouseListener(this);
}

private void checkMousePosition(MouseEvent e) {
if (polygon.contains(e.getX(), e.getY())) {
setState(true);
}
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
checkMousePosition(e);
System.out.println(this + " pressed");
}

public void mouseReleased(MouseEvent e) {
setState(false);
System.out.println(this + " released");

}

@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(isActive ? ACTIVE_COLOR : INACTIVE_COLOR);
g.drawPolygon(polygon);

}

void setState(boolean state) {
isActive = state;
repaint();
}
}

键盘.java:

package keyboard;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JComponent;

public class Keyboard extends JComponent {

Point[] leftFingers;
Point leftCenter = new Point(300, 600);

public Keyboard(Point left1, Point left2, Point left3, Point left4, Point left5) {
leftFingers = new Point[5];
leftFingers[0] = left1;
leftFingers[1] = left2;
leftFingers[2] = left3;
leftFingers[3] = left4;
leftFingers[4] = left5;
}

public KeyboardButton[] registerKeys() {
Polygon[] polygons = generateKeyPolygons(calculateBordersOfKeys(calculateCentersBetweenEachTwoFingers(leftFingers)));
KeyboardButton[] buttons = new KeyboardButton[5];
for (int i = 0; i < polygons.length; i++) {
buttons[i] = new KeyboardButton(polygons[i]);
}
return buttons;
}

private Point[] calculateBordersOfKeys(Point[] fingers) {
Point[] centers = calculateCentersBetweenEachTwoFingers(fingers);
Point[] result = new Point[6];

result[0] = calculateCentralSymmetry(centers[0], fingers[0]);
System.arraycopy(centers, 0, result, 1, centers.length);
result[5] = calculateCentralSymmetry(centers[3], fingers[4]);
return result;
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawOval(leftCenter.x - 25, leftCenter.y - 25, 50, 50);

for (int i = 0; i < leftFingers.length; i++) {
g.drawOval(leftFingers[i].x, leftFingers[i].y, 10, 10);

}
}

private Polygon[] generateKeyPolygons(Point[] borders) {
Polygon[] polygons = new Polygon[5];
for (int i = 0; i < borders.length - 1; i++) {
Polygon p = new Polygon();
p.addPoint(leftCenter.x, leftCenter.y);
p.addPoint(borders[i].x, borders[i].y);
p.addPoint(borders[i + 1].x, borders[i + 1].y);
polygons[i] = p;
}

return polygons;
}

private Point[] calculateCentersBetweenEachTwoFingers(Point[] fingers) {
Point[] centers = new Point[4];
for (int i = 0; i < fingers.length - 1; i++) {
centers[i] = new Point(((fingers[i].x + fingers[i + 1].x) / 2), ((fingers[i].y + fingers[i + 1].y) / 2));
}
return centers;
}

private Point calculateCentralSymmetry(Point toReflected, Point center) {
Point reflection = new Point();

if (toReflected.x > center.x) {
reflection.x = center.x - Math.abs(center.x - toReflected.x);
} else {
reflection.x = center.x + Math.abs(center.x - toReflected.x);
}

if (toReflected.y > center.y) {
reflection.y = center.y - Math.abs(center.y - toReflected.y);
} else {
reflection.y = center.y + Math.abs(center.y - toReflected.y);
}

return reflection;
}
}

最佳答案

尝试使用另一个布局管理器,或者在我看来,您似乎正在尝试在屏幕上手动绘制形状,我建议将它们全部绘制在一层上。 (有一个JComponent的paintComponent()方法调用KeyboardButton.paint()和其他绘画方法,那么你可以只添加那个JComponent)

关于java - 向 JFrame 添加更多组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10080029/

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