好的,JOptionPane
窗口中显示了JFrame
文字,即使是我的CS教授也无法找出原因。这是一个简单绘制3行的程序,但无论我使用哪种编译器,它都会在JFrame窗口中显示JOptionPane
文本。这是我的代码。
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.JFrame;
public class Lab5_1 extends JPanel {
public void paintComponent( Graphics g ) {
super.paintComponent(g);
String ia = JOptionPane.showInputDialog ("Enter the beginning x point of the line");
String iab = JOptionPane.showInputDialog ("Enter the beginning y point of the line");
String ja = JOptionPane.showInputDialog ("Enter the end x point of the line");
String jab = JOptionPane.showInputDialog ("Enter the end y point of the line");
int jx = Integer.parseInt(ja);
int jy = Integer.parseInt(jab);
int ix = Integer.parseInt(ia);
int iy = Integer.parseInt(iab);
String iac = JOptionPane.showInputDialog ("Enter the beginning x point of the line");
String iabc = JOptionPane.showInputDialog ("Enter the beginning y point of the line");
String jac = JOptionPane.showInputDialog ("Enter the end x point of the line");
String jabc = JOptionPane.showInputDialog ("Enter the end y point of the line");
int jxb = Integer.parseInt(jac);
int jyb = Integer.parseInt(jabc);
int ixb = Integer.parseInt(iac);
int iyb = Integer.parseInt(iabc);
String iad = JOptionPane.showInputDialog ("Enter the beginning x point of the line");
String iabd = JOptionPane.showInputDialog ("Enter the beginning y point of the line");
String jad = JOptionPane.showInputDialog ("Enter the end x point of the line");
String jabd = JOptionPane.showInputDialog ("Enter the end y point of the line");
int jxc = Integer.parseInt(jad);
int jyc = Integer.parseInt(jabd);
int ixc = Integer.parseInt(iad);
int iyc = Integer.parseInt(iabd);
g.drawLine(ix,iy,jx,jy);
g.drawLine(ixb,iyb,jxb,jyb);
g.drawLine(ixc,iyc,jxc,jyc);
}
public static void main( String[] args ) {
Lab5_1 panel = new Lab5_1();
JFrame application = new JFrame();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.add( panel );
application.setSize( 500, 290 );
application.setVisible( true );
}
}
答案 0 :(得分:2)
我相信这种情况正在发生,因为您正在JOptionPane.showInputDialog
方法内部使用paintComponent
。这并不是真正意图完成的,并且会给你带来不小的问题。
我建议将该部分从paintComponent
方法中拉出来,而只需在paintComponent
引用的对象上设置值。
所以你的代码会变成这样:
public class Lab5_1 extends JPanel {
int ix;
/* Rest of the fields */
public void paintComponent( Graphics g ) {
super.paintComponent(g);
g.drawLine(ix,iy,jx,jy);
/* Rest of the lines */
}
public static void main(String[] args){
Lab5_1 panel = new Lab5_1();
String ia = JOptionPane.showInputDialog ("Enter the beginning x point of the line");
panel.ix = Integer.parseInt(ia);
/* Rest of your initialization code */
}
}
每次想要重新绘制组件时,都不会尝试读取这些值,并且应该纠正图形故障。
答案 1 :(得分:0)
我将JOptionPane
移出了paintComponent
方法。问题立即消失了。它必须做一些事情来覆盖该组件。我的赌注是你在覆盖中调用super()
,但我自己没有测试过这个假设。当你将JOptionPane
移到paintComponent
之外时,无论其原因是什么问题都会消失。
另外,我觉得用户告诉用户输入坐标,然后将这些坐标解析为jx
,jy
等变量会更容易。更简单,只有三个窗格,而不是六个。
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Lab5_1 extends JPanel {
private static final long serialVersionUID = 1L;
private static int jx;
private static int jy;
private static int ix;
private static int iy;
private static int jxb;
private static int jyb;
private static int ixb;
private static int iyb;
private static int jxc;
private static int jyc;
private static int ixc;
private static int iyc;
@Override public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(ix, iy, jx, jy);
g.drawLine(ixb, iyb, jxb, jyb);
g.drawLine(ixc, iyc, jxc, jyc);
}
public static void main(String[] args) {
SwingTesting panel = new SwingTesting();
JFrame application = new JFrame();
String ia = JOptionPane.showInputDialog("Enter the beginning x point of the line");
String iab = JOptionPane.showInputDialog("Enter the beginning y point of the line");
String ja = JOptionPane.showInputDialog("Enter the end x point of the line");
String jab = JOptionPane.showInputDialog("Enter the end y point of the line");
jx = Integer.parseInt(ja);
jy = Integer.parseInt(jab);
ix = Integer.parseInt(ia);
iy = Integer.parseInt(iab);
String iac = JOptionPane.showInputDialog("Enter the beginning x point of the line");
String iabc = JOptionPane.showInputDialog("Enter the beginning y point of the line");
String jac = JOptionPane.showInputDialog("Enter the end x point of the line");
String jabc = JOptionPane.showInputDialog("Enter the end y point of the line");
jxb = Integer.parseInt(jac);
jyb = Integer.parseInt(jabc);
ixb = Integer.parseInt(iac);
iyb = Integer.parseInt(iabc);
String iad = JOptionPane.showInputDialog("Enter the beginning x point of the line");
String iabd = JOptionPane.showInputDialog("Enter the beginning y point of the line");
String jad = JOptionPane.showInputDialog("Enter the end x point of the line");
String jabd = JOptionPane.showInputDialog("Enter the end y point of the line");
jxc = Integer.parseInt(jad);
jyc = Integer.parseInt(jabd);
ixc = Integer.parseInt(iad);
iyc = Integer.parseInt(iabd);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(500, 290);
application.setVisible(true);
}
}