我的JFrame出现了一个小问题。当我运行我的代码时,代码会先取8个值,然后把它们画到jframe窗口上,它确实是这样做了,但是这个窗口似乎捕捉到了我的windows桌面上框架后面的任何东西。所以,你可以看到在图片中,它捕捉到了我的Jintelligence框,而在另一张图片中,它捕捉到了我的壁纸,这就是背景。我只是想要一个普通的正常背景。不知道为什么会出现这种情况。有什么想法吗?
> import javax.swing.*;
import java.awt.*;
import java.lang.*;
public class Distance extends JFrame {
String longer;
String input;
int line1_x1, line1_y1, line1_x2, line1_y2;
int line2_x1, line2_y1, line2_x2, line2_y2;
double l1distance;
double l2distance;
double distl1;
double distl2;
public static void main(String[] args) {
Distance frame = new Distance();
frame.setSize(600, 600);
frame.setTitle("LINE DRAWER");
frame.getvalues();
frame.getdistance();
frame.getlongest();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void getvalues () {
//Line 1
input = JOptionPane.showInputDialog("Enter the X1 coordinate for Line 1 here:");
line1_x1 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("And the Y1 coordinate for Line 1:");
line1_y1 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the X2 coordinate for Line 1 here:");
line1_x2 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Plus the Y2 coordinate for Line 1:");
line1_y2 = Integer.parseInt(input);
//Line 2
input = JOptionPane.showInputDialog("Now, enter the X1 coordinate the 2nd line:");
line2_x1 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("And the Y1 coordinate for the 2nd line:");
line2_y1 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Then the X2 coordinate here:");
line2_x2 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("And lastly, the Y2 coordinate:");
line2_x2 = Integer.parseInt(input);
}
public void getdistance() {
distl1 = Math.pow((line1_y2 - line1_y1),2) + Math.pow((line1_x2 - line1_x1),2);
distl2 = Math.pow((line2_y2 - line2_y1),2) + Math.pow((line2_x2 - line2_x1),2);
//Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));\
l1distance = Math.abs(Math.floor(Math.sqrt(distl1)));
l2distance = Math.abs(Math.floor(Math.sqrt(distl2)));
}
public void getlongest(){
if(l1distance > l2distance){
longer = "Line One";
}
else if(l1distance == l2distance){
longer = "The Lines are Equal";
}
else if(l2distance > l1distance){
longer = "Line Two";
}
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawLine(line1_x1, line1_y1, line1_x2, line1_y2);
g2d.drawString("Line 1 Origin: (" + line1_x1 + "," + line1_y1 +")", line1_x1-15, line1_y1-15);
g2d.drawString("Line 1 End: (" + line1_x2 + "," + line1_y2 +")", line1_x2+15, line1_y2+ 15);
g2d.drawLine(line2_x1, line2_y1, line2_x2, line2_y2);
g2d.drawString("Line 2 Origin: (" + line2_x1 + "," + line2_y1 +")", line2_x1-15, line2_y1-15);
g2d.drawString("Line 2 End: (" + line2_x2 + "," + line2_y2 +")", line2_x2+15, line2_y2+ 15);
g2d.setColor(Color.red);
g2d.setFont(new Font("Batang", Font.BOLD, 10));
g2d.drawString("Line 1's Distance is: " + l1distance + ".", 50,400);
g2d.drawString("Line 2's Distance is: " + l2distance + "." ,50,430);
g2d.drawString("The longer line is: " + longer + "." ,50,450);
}
}
对不起,我是JFrame的新手,所以这可能没有用,但你是否尝试过以下方法 frame. setBackground(Color.WHITE)
?