Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame中显示的JOptionPane文本_Java_Swing_Jframe_Joptionpane - Fatal编程技术网

Java JFrame中显示的JOptionPane文本

Java JFrame中显示的JOptionPane文本,java,swing,jframe,joptionpane,Java,Swing,Jframe,Joptionpane,好的,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

好的,
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 ); 
    }
}
结果如下:

我将
作业窗格
s移出
paintComponent
方法。问题马上就解决了。它必须重写该组件。我敢打赌,你在这个覆盖中调用了
super()
,但我自己还没有测试过这个假设。当您将
JOptionPane
s移到
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);
    }
}

我将
JOptionPane
s移出
paintComponent
方法。问题马上就解决了。它必须重写该组件。我敢打赌,你在这个覆盖中调用了
super()
,但我自己还没有测试过这个假设。当您将
JOptionPane
s移到
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);
    }
}

请不要将图片作为外部链接发布。联系可能在将来中断。Stackoverflow支持添加图像;)没有足够的“信誉点”来完成这项工作,当您的组件需要绘制时,会调用paintComponent,因此每次都会显示您的JOptionPane,每次都…
paintComponent
用于绘制,不应用于其他任何用途。有关绘制工作原理的更多详细信息,请参见和。绘制可以在任何时候出于任何原因进行,这意味着您的绘制方法将被重复调用,并且可能会在内部快速连续调用,它们还应尽快返回以保持UI响应。请不要将图像作为外部链接发布。联系可能在将来中断。Stackoverflow支持添加图像;)没有足够的“信誉点”来完成这项工作,当您的组件需要绘制时,会调用paintComponent,因此每次都会显示您的JOptionPane,每次都…
paintComponent
用于绘制,不应用于其他任何用途。有关绘制工作原理的更多详细信息,请参见和。绘制可以在任何时候出于任何原因进行,这意味着您的绘制方法将被重复调用,并且可能会在中连续快速调用,它们还应尽快返回,以保持UI响应性
paintComponent
用于绘制,不应用于其他任何用途。有关绘制工作原理的更多详细信息,请参见和。你的假设通常是错误的。下一个问题是依赖于
静态
通常是一个坏主意,是一个糟糕设计的标志。好的,不要使用
静态
,OP没有,让我们不要引入更多坏习惯。那么,我们到底如何在主方法中从静态上下文引用
int
字段呢?否则会发生编译错误。切换到非静态上下文
paintComponent
用于绘制,不应用于其他任何内容。有关绘制工作原理的更多详细信息,请参见和。你的假设通常是错误的。下一个问题是依赖于
静态
通常是一个坏主意,是一个糟糕设计的标志。好的,不要使用
静态
,OP没有,让我们不要引入更多坏习惯。那么,我们到底如何在主方法中从静态上下文引用
int
字段呢?否则会发生编译错误。请切换到非静态上下文