2016-06-12 7 views
1

私は、ユーザが3つのフィールドのうちの2つに数字を入力し、私のアプリケーションがこれらの2つの数字でピタゴラスの定理を行い、答えの答えフィールドを設定するスイングアプリケーションを持っています。ただし、3つのフィールド(斜辺、短辺1、短辺2)はすべて0を返します(短辺1と短辺2は異なるフィールドで、:を追加するのを忘れています)。デフォルト値は0です。これは他のウィンドウでは当てはまりませんが、これは[数学]タブの場合のみです。私の質問は、問題は何ですか?ここでJTextFieldのテキストを取得する

は、エラーのスクリーンショットです:

そしてここではコードです:

Entry.java

package Entry; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.util.List; 

import javax.imageio.ImageIO; 
import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JMenuBar; 

import Settings.SettingsLoader; 
import Window.ErrorWindow; 
import Window.SmallLinkWindow; 
import Window.Window; 
import Window.WindowEntry; 

public class Entry { 

    public static JFrame frame; 
    public static File file; 
    public static JInternalFrame currentframe; 

    public static void main(String[] args){ 
     file = new File("settings.txt"); 
     frame = new JFrame("GUI_Base"); 
     JMenuBar menu = new JMenuBar(); 
     JMenuBar bottom = new JMenuBar(); 
     SmallLinkWindow[] smallwindows = WindowEntry.getSmallWindows(); 
     for(int i = 0; i < smallwindows.length; i++){ 
      SmallLinkWindow window = smallwindows[i]; 
      JButton button = window.getButton(); //ActionListener already added at this point. 
      button.addActionListener(getActionListener(window)); 
      bottom.add(button); 
     } 
     List<String> data = readAllData(); 
     SettingsLoader loader = new SettingsLoader(data); 
     loader.obtainSettings(); 
     Window[] windows = WindowEntry.getAllWindows(); 
     for(int i = 0; i < windows.length; i++){ 
      Window window = windows[i]; 
      JButton item = new JButton(window.getName()); 
      item.addActionListener(getActionListener(window)); 
      menu.add(item); 
     } 
     currentframe = windows[0].getInsideFrame(); 
     menu.add(getRefresh(), BorderLayout.EAST); 
     frame.setSize(2000, 1000); 
     frame.add(menu, BorderLayout.NORTH); 
     frame.add(bottom, BorderLayout.SOUTH); 
     frame.getRootPane().setBorder(BorderFactory.createLineBorder(Color.BLACK, 5)); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     System.out.println("Loaded!"); 
    } 


    private static JButton getRefresh() { 
     try { 
      BufferedImage image = ImageIO.read(new File("refresh.png")); 
      int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType(); 
      image = resizeImage(image, type, 25, 25); 
      ImageIcon icon = new ImageIcon(image); 
      JButton label = new JButton(icon); 
      label.addActionListener(getActionListener()); 
      return label; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    private static ActionListener getActionListener() { 
     return new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       frame.repaint(); 

      } 
      }; 
    } 

    //Copied from http://www.mkyong.com/java/how-to-resize-an-image-in-java/ 
    public static BufferedImage resizeImage(BufferedImage originalImage, int type, int width, int height){ 
     BufferedImage resizedImage = new BufferedImage(width, height, type); 
     Graphics2D g = resizedImage.createGraphics(); 
     g.drawImage(originalImage, 0, 0, width, height, null); 
     g.dispose(); 

     return resizedImage; 
     } 

    private static ActionListener getActionListener(SmallLinkWindow window) { 
     ActionListener listener = new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       Entry.frame.remove(currentframe); 
       JInternalFrame frame = window.getInsideFrame(); 
       frame.setSize(1400, 925); 
       Entry.frame.add(frame); 
       currentframe = frame; 
       frame.setVisible(true); 
      } 
     }; 
     return listener; 
    } 

    private static ActionListener getActionListener(Window window) { 
     ActionListener listener = new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       Entry.frame.remove(currentframe); 
       JInternalFrame frame = window.getInsideFrame(); 
       frame.setSize(1400, 925); 
       Entry.frame.add(frame); 
       currentframe = frame; 
       frame.setVisible(true); 

      } 
     }; 
     return listener; 
    } 

    private static List<String> readAllData() { 
     try { 
      return Files.readAllLines(file.toPath()); 
     } catch (IOException e) { 
      ErrorWindow.forException(e); 
     } 
     ErrorWindow.forException(new RuntimeException("Unable to read file!")); 
     System.exit(1); 
     return null; 
    } 

} 

MathWindow.java

package Window; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JInternalFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSplitPane; 
import javax.swing.JTextField; 

import Math.Pythagoras; 
import Math.Trig; 
import Math.TrigValue; 
import Math.TrigonometryException; 
import Settings.GUISetting; 

public class MathWindow implements Window { 

    private Color colour; 
    private JSplitPane splitPane; 

    @Override 
    public String getName() { 
     return "Maths"; 
    } 

    @Override 
    public JInternalFrame getInsideFrame() { 
     JInternalFrame frame = new JInternalFrame(); 
     JSplitPane pane = new JSplitPane(); 
     pane.setDividerLocation(300); 
     JPanel panel = new JPanel(); 
     panel.setSize(300, 925); 
     JButton pyth = new JButton(); 
     JButton trig = new JButton(); 
     pyth.setText("Pythagoars theorem"); 
     trig.setText("Trigonometry"); 
     pyth.setSize(300, 200); 
     trig.setSize(300, 200); 
     pyth.addActionListener(getActionListenerForPythagoras()); 
     trig.addActionListener(getActionListenerForTrignomotry()); 
     panel.setLayout(new GridLayout(0,1)); 
     panel.add(pyth); 
     panel.add(trig); 
     pane.setLeftComponent(panel); 
     splitPane = pane; 
     frame.getContentPane().add(pane); 
     return frame; 
    } 

    private ActionListener getActionListenerForPythagoras() { 
     return new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent event) { 
       JPanel overseePanel = new JPanel(); 
       JTextField hypField = new JTextField(); 
       JTextField aField = new JTextField(); 
       JTextField bField = new JTextField(); 
       JLabel hypLabel = new JLabel(); 
       JLabel aLabel = new JLabel(); 
       JLabel bLabel = new JLabel(); 
       JButton button = new JButton(); 
       JTextField field = new JTextField(); 
       hypLabel.setText("Hypotenuse"); 
       aLabel.setText("Small side 1"); 
       bLabel.setText("Small side 2"); 
       hypLabel.setSize(400, hypLabel.getHeight()); 
       aLabel.setSize(400, aLabel.getHeight()); 
       bLabel.setSize(400, bLabel.getHeight()); 
       hypField.setText("0"); 
       aField.setText("0"); 
       bField.setText("0"); 
       hypField.setSize(400, hypLabel.getHeight()); 
       aField.setSize(400, aLabel.getHeight()); 
       bField.setSize(400, bLabel.getHeight()); 
       button.setText("Work it out!"); 
       button.addActionListener(getActionListenerForPythagorasFinal(hypField.getText(), aField.getText(), bField.getText(), field)); 
       overseePanel.setLayout(new GridLayout(0,1)); 
       overseePanel.add(hypLabel, BorderLayout.CENTER); 
       overseePanel.add(hypField, BorderLayout.CENTER); 
       overseePanel.add(aLabel, BorderLayout.CENTER); 
       overseePanel.add(aField, BorderLayout.CENTER); 
       overseePanel.add(bLabel, BorderLayout.CENTER); 
       overseePanel.add(bField, BorderLayout.CENTER); 
       overseePanel.add(button); 
       overseePanel.add(field); 
       splitPane.setRightComponent(overseePanel); 
      } 
     }; 
    } 

    protected ActionListener getActionListenerForPythagorasFinal(String hyp, String s1, String s2, JTextField field) { 
     return new ActionListener(){ 

      private Pythagoras p = new Pythagoras(); 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       System.out.println("Hypotenuse: " + hyp); 
       System.out.println("Shorter side 1" + s1); 
       System.out.println("Shorter side 2" + s2); 
       if(hyp.equals("0")){ 
        double a = Double.parseDouble(s1); 
        double b = Double.parseDouble(s2); 
        if(a == 3 && b == 4 || a == 4 && b == 3) System.out.println("The result should be 5!"); 
        field.setText(String.valueOf(p.getHypotenuse(a, b))); 
       }else if(s1.equals("0")){ 
        double c = Double.parseDouble(hyp); 
        double b = Double.parseDouble(s2); 
        field.setText(String.valueOf(p.getShorterSide(b, c))); 
       }else if(s2.equals("0")){ 
        double c = Double.parseDouble(hyp); 
        double a = Double.parseDouble(s1); 
        field.setText(String.valueOf(p.getShorterSide(a, c))); 
       }else throw new IllegalArgumentException("All of the fields have stuff in them!"); 
      } 

     }; 
    } 

    private ActionListener getActionListenerForTrignomotry(){ 
     return new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       JPanel overseePanel = new JPanel(); 

       JTextField hypField = new JTextField(); 
       JTextField aField = new JTextField(); 
       JTextField bField = new JTextField(); 
       JTextField anField = new JTextField(); 
       JLabel hypLabel = new JLabel(); 
       JLabel aLabel = new JLabel(); 
       JLabel bLabel = new JLabel(); 
       JLabel anLabel = new JLabel(); 
       JButton button = new JButton(); 
       JTextField field = new JTextField(); 
       hypLabel.setText("Hypotenuse"); 
       aLabel.setText("Opposite"); 
       bLabel.setText("Adjacent"); 
       anLabel.setText("Angle size"); 
       hypLabel.setSize(400, hypLabel.getHeight()); 
       aLabel.setSize(400, aLabel.getHeight()); 
       bLabel.setSize(400, bLabel.getHeight()); 
       anLabel.setSize(400, anLabel.getHeight()); 
       hypField.setText("0"); 
       aField.setText("0"); 
       bField.setText("0"); 
       anField.setText("0"); 
       hypField.setSize(400, hypLabel.getHeight()); 
       aField.setSize(400, aLabel.getHeight()); 
       bField.setSize(400, bLabel.getHeight()); 
       anField.setSize(400, anLabel.getHeight()); 
       button.setText("Work it out!"); 
       button.addActionListener(getActionListenerForTrigonomotryFinal(hypField.getText(), aField.getText(), bField.getText(), anField.getText(), field)); 
       overseePanel.setLayout(new GridLayout(0,1)); 
       overseePanel.add(hypLabel, BorderLayout.CENTER); 
       overseePanel.add(hypField, BorderLayout.CENTER); 
       overseePanel.add(aLabel, BorderLayout.CENTER); 
       overseePanel.add(aField, BorderLayout.CENTER); 
       overseePanel.add(bLabel, BorderLayout.CENTER); 
       overseePanel.add(bField, BorderLayout.CENTER); 
       overseePanel.add(anLabel, BorderLayout.CENTER); 
       overseePanel.add(anField, BorderLayout.CENTER); 
       overseePanel.add(button); 
       overseePanel.add(field); 
       splitPane.setRightComponent(overseePanel); 
      } 

     }; 
    } 
    //a == opposite, b == adjacent 
    protected ActionListener getActionListenerForTrigonomotryFinal(String hyp, 
      String a, String b, String an, JTextField field) { 
      return new ActionListener(){ 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        Trig trigonometry = new Trig(); 
        double value = 0.000; 
        if(an == "0"){ 

         if(hyp == "0"){ 
          int shorta = Integer.parseInt(a); 
          int shortb = Integer.parseInt(b); 
          try { 
           TrigValue tA = new TrigValue(TrigValue.OPPOSITE, shorta); 
           TrigValue tB = new TrigValue(TrigValue.ADJACENT, shortb); 
           value = trigonometry.getAngleSize(tA, tB); 
          } catch (TrigonometryException e1) { 
           e1.printStackTrace(); 
          } 
         }else if(a == "0"){ 
          int hypotenuse = Integer.parseInt(hyp); 
          int shortb = Integer.parseInt(b); 
          try { 
           TrigValue tH = new TrigValue(TrigValue.HYPOTENUSE, hypotenuse); 
           TrigValue tB = new TrigValue(TrigValue.ADJACENT, shortb); 
           value = trigonometry.getAngleSize(tH, tB); 
          } catch (TrigonometryException e1) { 
           e1.printStackTrace(); 
          } 
         }else if(b == "0"){ 
          int hypotenuse = Integer.parseInt(hyp); 
          int shorta = Integer.parseInt(a); 
          try { 
           TrigValue tA = new TrigValue(TrigValue.OPPOSITE, shorta); 
           TrigValue tH = new TrigValue(TrigValue.HYPOTENUSE, hypotenuse); 
           value = trigonometry.getAngleSize(tA, tH); 
          } catch (TrigonometryException e1) { 
           e1.printStackTrace(); 
          } 
         } 


        }else{ 
         int angle = Integer.parseInt(an); 
         if(angle >= 90) throw new IllegalArgumentException("Angle is bigger than 90"); 
         if(hyp.equals("0")){ 
          if(a.equals("?")){ 
           int shortb = Integer.parseInt(b); 
           try { 
            TrigValue tB = new TrigValue(TrigValue.ADJACENT, shortb); 
            TrigValue tA = new TrigValue(TrigValue.OPPOSITE); 
            value = trigonometry.getSideLength(tB, angle, tA); 
           } catch (TrigonometryException e1) { 
            e1.printStackTrace(); 
           } 
          }else if(b.equals("?")){ 
           int shorta = Integer.parseInt(a); 
           try { 
            TrigValue tB = new TrigValue(TrigValue.ADJACENT); 
            TrigValue tA = new TrigValue(TrigValue.OPPOSITE, shorta); 
            value = trigonometry.getSideLength(tA, angle, tB); 
           } catch (TrigonometryException e1) { 
            e1.printStackTrace(); 
           } 
          }else throw new IllegalArgumentException("We already know what we want to know."); 
         }else if(a.equals("0")){ 
          if(hyp.equals("?")){ 
           int shortb = Integer.parseInt(b); 
           try { 
            TrigValue tB = new TrigValue(TrigValue.ADJACENT, shortb); 
            TrigValue tH = new TrigValue(TrigValue.HYPOTENUSE); 
            value = trigonometry.getSideLength(tB, angle, tH); 
           } catch (TrigonometryException e1) { 
            e1.printStackTrace(); 
           } 
          }else if(b.equals("?")){ 
           int h = Integer.parseInt(hyp); 
           try { 
            TrigValue tB = new TrigValue(TrigValue.ADJACENT); 
            TrigValue tH = new TrigValue(TrigValue.HYPOTENUSE, h); 
            value = trigonometry.getSideLength(tH, angle, tB); 
           } catch (TrigonometryException e1) { 
            e1.printStackTrace(); 
           } 
          }else throw new IllegalArgumentException("We already know what we want to know."); 

         }else if(b.equals("0")){ 
          if(hyp.equals("?")){ 
           int shorta = Integer.parseInt(a); 
           try { 
            TrigValue tA = new TrigValue(TrigValue.OPPOSITE, shorta); 
            TrigValue tH = new TrigValue(TrigValue.HYPOTENUSE); 
            value = trigonometry.getSideLength(tA, angle, tH); 
           } catch (TrigonometryException e1) { 
            e1.printStackTrace(); 
           } 
          }else if(a.equals("?")){ 
           int h = Integer.parseInt(hyp); 
           try { 
            TrigValue tA = new TrigValue(TrigValue.OPPOSITE); 
            TrigValue tH = new TrigValue(TrigValue.HYPOTENUSE, h); 
            value = trigonometry.getSideLength(tH, angle, tA); 
           } catch (TrigonometryException e1) { 
            e1.printStackTrace(); 
           } 
          } 
         } 
        } 
        field.setText(String.valueOf(value)); 
       } 

      }; 
    } 

    @Override 
    public GUISetting[] getSettings() { 
     GUISetting setting = new GUISetting("Show working", "Maths"); 
     GUISetting setting2 = new GUISetting("Round", "Maths"); 
     return new GUISetting[]{setting, setting2}; 
    } 

    @Override 
    public void setColour(Color c) { 
     colour = c; 
    } 

    @Override 
    public Color getCurrentColour() { 
     return colour; 
    } 

} 

他にも追加する必要がある場合は、コメントを追加してください。

+0

[mcve] – mjn

答えて

3

新しいインスタンスをJTextFieldに作成し、textメソッドにtextプロパティを渡すと、フィールドに入力される予定のものがなくなり、初期値( "")のみが入力されます。問題のフィールドで計算を完全に実行することができません

フィールド自体をメソッドに渡すこともできますが、一般的なアドバイスとして、フィールドと関連するアクションを含むカスタムクラスを作成します。必要なときにいつでも作成でき、管理と保守が大幅に簡単になります

+0

あなたはされていますか?大丈夫ですか? –

+1

@HovercraftFullOfEels私は暗い側面に移動し、iOSの開発に行きました。言うまでもなく、私は非常に占領されています:P – MadProgrammer

+0

動きがうまくいて、あなたはかなり成功すると確信しています! –

関連する問題