简体   繁体   English

JFrame中显示的JOptionPane文本

[英]JOptionPane text showing in JFrame

Okay so the JOptionPane text is showing in the JFrame window and not even my CS professor can figure out why. 好的,因此JOptionPane文本显示在JFrame窗口中,甚至连我的CS教授都无法弄清楚原因。 It is a program to simply draw 3 lines, but it has shown the JOptionPane text in the JFrame window no matter which compiler that I use. 它是一个仅绘制3行的程序,但是无论我使用哪个编译器,它都已在JFrame窗口中显示了JOptionPane文本。 Heres my code. 这是我的代码。

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 ); 
    }
}

Here's the result: 结果如下: 在此处输入图片说明

I believe this is happening because you're using JOptionPane.showInputDialog from inside of the paintComponent method. 我相信这是因为您是从paintComponent方法内部使用JOptionPane.showInputDialog而发生的。 That's not really how it's intended to be done, and is going to cause you no small amount of problems. 这并不是真正的打算,并且会给您带来很多问题。

I'd suggest pulling that part out of the paintComponent method, and instead simply setting values on the object which are referenced by paintComponent . 我建议将这一部分从paintComponent方法中拉出来,而不是简单地在paintComponent引用的对象上设置值。

So your code would become something like this: 因此您的代码将变成这样:

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 */
    }
}

This won't try to read those values every time the component wants to be repainted, and should correct the graphical glitches. 这不会在每次要重新绘制组件时尝试读取这些值,而是应纠正图形故障。

I moved the JOptionPane s out of the paintComponent method. 我将JOptionPane移出了paintComponent方法。 The problem went away immediately. 问题立刻消失了。 It has to do something with overriding that component. 它必须做一些重写该组件的事情。 My bet would be the fact that you call super() in that override, but I haven't tested that hypothesis myself. 我敢打赌,您会在该覆盖中调用super() ,但我自己尚未检验该假设。 The problem, whatever its cause, goes away when you move your JOptionPane s outside paintComponent . 当您将JOptionPane移到paintComponent之外时,无论其原因是什么,问题都会消失。

Also, I feel it would be much easier on the user to tell the use to input coordinates, then parsing those coordinates into the jx , jy , etc. variables. 另外,我觉得让用户知道使用输入坐标,然后将这些坐标解析为jxjy等变量会容易得多。 Simpler and only three panes instead of six. 更简单,只有三个窗格,而不是六个。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM