2016-02-25 153 views
1

好吧,JOptionPane文本顯示在JFrame窗口中,甚至我的CS教授都不知道爲什麼。這是一個簡單繪製3行的程序,但無論使用哪種編譯器,它都在JFrame窗口中顯示JOptionPane文本。繼承人我的代碼。JOptionPane在JFrame中顯示的文字

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

這裏的結果: enter image description here

+0

請不要將圖片作爲外部鏈接發佈。鏈接可能在未來破裂。 Stackoverflow支持添加圖像;) –

+0

沒有足夠的「聲望點」來做到這一點 –

+0

當你的組件需要被繪製時調用PaintComponent,所以每次你的JOptionPane被顯示,每一次... – MadProgrammer

回答

2

我相信這是因爲你使用JOptionPane.showInputDialogpaintComponent方法裏面發生的事情。這並不是真正意圖完成的任務,並且會給你帶來不少問題。

我會建議從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 */ 
    } 
} 

這不會想盡組件希望重新繪製的時間來閱讀這些價值觀,並應糾正故障的圖形。

+0

@Joathan Knight如果此解決方案解決了您的問題,您可以通過點擊答案旁邊空心的勾號來接受它。除了用於繪圖的代碼之外,您不應該在paintComponent(Graphics)方法中添加任何代碼。如果你的問題沒有解決,你可以添加評論。 – user3437460

0

我將JOptionPanepaintComponent中移出。問題立即消失。它必須重寫該組件。我敢打賭,事實上,你可以撥打super(),但我沒有親自測試過這個假設。當你將JOptionPane移到paintComponent之外時,無論是什麼原因,問題都會消失。另外,我覺得用戶可以更容易地告訴用戶輸入座標,然後將這些座標解析成jxjy等變量。簡單,只有三個窗格,而不是六個。

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

'paintComponent'用於繪畫,不應該用於其他任何東西。請參閱[在AWT和Swing中繪畫](http://www.oracle.com/technetwork/java/painting-140037.html)和[執行自定義繪畫](https://docs.oracle.com/javase/tutorial/) uiswing/painting /)來獲得更多關於油漆如何工作的細節。你的假設通常是錯誤的。接下來的問題是對「靜態」的依賴通常是一個糟糕的主意和糟糕的設計標誌 – MadProgrammer

+0

設計是OP的。我試圖讓它工作。 – ifly6

+1

好,不要使用'static',OP沒有,不要引入更多的壞習慣 – MadProgrammer