【问题标题】:JOptionPane text showing in JFrameJFrame 中显示的 JOptionPane 文本
【发布时间】:2016-06-08 19:27:42
【问题描述】:

好的,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 ); 
    }
}

结果如下:

【问题讨论】:

  • 请不要将图片作为外部链接发布。链接将来可能会中断。 Stackoverflow 支持添加图片 ;)
  • 没有足够的“声誉点”来做到这一点
  • PaintComponent 在你的组件需要被绘制时被调用,所以每次,你的 JOptionPane 都会被显示出来,每次......
  • paintComponent 用于绘画,不应用于其他任何用途。请参阅 Painting in AWT and Swing 和 Performing Custom Painting 了解有关绘画工作原理的更多详细信息。绘画可以在任何时候出于任何原因发生,这意味着您的绘画方法将被重复调用,并且可能会在短时间内连续调用,它们还应该尽快返回以保持 UI 响应

标签: java swing jframe joptionpane


【解决方案1】:

我将JOptionPanes 移出paintComponent 方法。问题立即消失了。它必须与覆盖该组件有关。我敢打赌,你会在那个覆盖中调用super(),但我自己还没有测试过这个假设。当您将JOptionPanes 移到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);
    }
}

【讨论】:

  • paintComponent 用于绘画,不得用于其他任何用途。请参阅Painting in AWT and Swing 和Performing Custom Painting,了解有关绘画工作原理的更多详细信息。你的假设通常是错误的。下一个问题是对static 的依赖通常是一个坏主意,也是设计不佳的标志
  • 好,不要用static,OP没有,不要引入更多的坏习惯
  • 那么我们究竟如何在 main 方法的静态上下文中引用 int 字段呢?否则会出现编译错误。
  • 切换到非静态上下文
猜你喜欢
  • 2012-07-20
  • 1970-01-01
  • 2015-04-22
  • 2015-11-20
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多