文本框不会立即弹出JFrame

时间:2016-11-12 20:22:37

标签: java jframe

我附上了第一次应该实现的截图,但只是通过调整窗口大小来实现。代码将编译并运行。它应该从屏幕上的任何位置获取颜色值,但我似乎无法使R G B盒第一次位于正确的位置。有人可以看看吗?

enter image description here

请再次注意,这是首次执行时应该发生的事情,但仅通过调整大小来实现。

import static com.sun.javafx.fxml.expression.Expression.add;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class RGBunderCursor {

public static void main(String[] args) throws AWTException{
new RGBunderCursor().TestPane();
}

private JPanel fields;
private JTextField red, green, blue;
private JLabel label;
private BufferedImage img;
PointerInfo info = MouseInfo.getPointerInfo();
Point point = info.getLocation();

public void TestPane() throws AWTException {

JFrame frame = new JFrame("Get the RGB");
Container content = frame.getContentPane();   
fields = new JPanel();
//JTextArea RGBV = new JTextArea("test");
//frame.setLayout(new BorderLayout());
content.setLayout(new BorderLayout());

fields.setVisible(true);
content.add(fields);

frame.setSize(600, 600);
// frame.pack();
frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        fields.setBorder(new EmptyBorder(5, 5, 5, 5));

        red = new JTextField(3);
        green = new JTextField(3);
        blue = new JTextField(3);
        fields.add(red);
        fields.add(green);
        fields.add(blue);
        add(fields, gbc);

Robot robot = new Robot();
robot.delay(2000);

while(true) {
   point = MouseInfo.getPointerInfo().getLocation();       
   Color color = robot.getPixelColor((int)point.getX(), (int)point.getY());
   red.setText(Integer.toString(color.getRed()));
   green.setText(Integer.toString(color.getGreen()));
   blue.setText(Integer.toString(color.getBlue()));
   fields.setBackground(color);
}

 }
 }

0 个答案:

没有答案