我试图设置JFrame的水平半部分以显示光标所在的颜色。 这是我到目前为止所得到的。
enter code here
package finalproject;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.PointerInfo;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.*;
public class FinalProject {
public static void main(String[] args) throws AWTException {
JFrame frame = new JFrame();
JLabel rgbLabel = new JLabel();
rgbLabel.setVerticalAlignment(SwingConstants.NORTH);
rgbLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.setLocationRelativeTo(null);
frame.add(rgbLabel);
rgbLabel.setAlignmentX(50);
rgbLabel.setAlignmentY(10);
rgbLabel.setVisible(true);
frame.setLocation(650, 350);
frame.setVisible(true);
frame.pack();
frame.setTitle(display.TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(display.WIDTH, display.HEIGHT);
frame.setResizable(true);
frame.setVisible(true);
while (true){
PointerInfo cursorLocation = MouseInfo.getPointerInfo();
Point position = cursorLocation.getLocation();
int x = (int)position.getX();
int y = (int)position.getY();
Robot robot = new Robot();
Color pixelColor = robot.getPixelColor(x, y);
int colorRed = pixelColor.getRed();
int colorGreen = pixelColor.getGreen();
int colorBlue = pixelColor.getBlue();
rgbLabel.setText("Red: " + colorRed + " Green: " + colorGreen + " Blue: " + colorBlue + "\n" );
}
}
public static class display {
public static final int WIDTH = 250;
public static final int HEIGHT = 135;
public static final String TITLE = "Colorblind Assistant";
}
}
我知道放
frame.getContantPane().setBackground(pixelColor);
while循环下的会将整个框架更改为颜色,但我只需要框架的下半部分就是那种颜色。
一些帮助将不胜感激。 谢谢。
答案 0 :(得分:2)
while (true)
。