缺少GUI输出

时间:2013-06-28 15:41:39

标签: java swing

这个程序是一个三角计算程序。我遇到的问题实际上出现在JFrame内。我似乎无法查明错误。没关系我编辑过的对象或涉及的任何数学。我还编辑了变量,这不是问题。我需要知道的是为什么面板没有出现。它一定是我猜的小事。

public class TrigCalcGUI extends JFrame implements ActionListener
{

    public TrigCalcGUI()
    {
        // title bar text.
        super("Trig Calculator");
        // Corner exit button action.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Object
        //TrigCalc trC = new TrigCalc(sin, cos, tan);

        // Create main panel to add each panel to
        mainPanel = new JPanel();

        // Assign Panel to each variable
        inputPanel = new JPanel();
        sinPanel = new JPanel();
        cosPanel = new JPanel();
        tanPanel = new JPanel();
        cscPanel = new JPanel();
        secPanel = new JPanel();
        cotPanel = new JPanel();
        buttonPanel = new JPanel();

        // Call each constructor
        buildInputPanel();
        buildSinCosTanPanels();
        buildCscSecCotPanels();
        buildButtonPanel();

        // Add each panel to content pane
        mainPanel.add(inputPanel);
        mainPanel.add(sinPanel);
        mainPanel.add(cscPanel);
        mainPanel.add(cosPanel);
        mainPanel.add(secPanel);
        mainPanel.add(tanPanel);
        mainPanel.add(cotPanel);
        mainPanel.add(buttonPanel);

        // size of window to content
        this.pack();

        // display window
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new TrigCalcGUI();
    }

    private void buildInputPanel()
    {
        inputLabel = new JLabel("Enter a Value: ");
        inputTF = new JTextField(5);
    }

    // Building Constructor for sinPanel
    private void buildSinCosTanPanels()
    {
        // Set layout and border for sinPanel
        sinPanel.setLayout(new GridLayout(1,2));
        sinPanel.setBorder(BorderFactory.createTitledBorder("Sin()"));

        // 
        sinLabel = new JLabel("Sin() of " + inputTF.getText() + " ");
        sinTF = new JTextField(5);
        sinTF.setEditable(false);

        sinPanel.add(sinLabel);
        sinPanel.add(sinTF);

        // Set layout and border for cosPanel
        cosPanel.setLayout(new GridLayout(1,2));
        cosPanel.setBorder(BorderFactory.createTitledBorder("Cos()"));

        cosLabel = new JLabel("Cos() of " + inputTF.getText() + " ");
        cosTF = new JTextField(5);
        cosTF.setEditable(false);

        cosPanel.add(cosLabel);
        cosPanel.add(cosTF);

        // Set layout and border for tanPanel
        tanPanel.setLayout(new GridLayout(1,2));
        tanPanel.setBorder(BorderFactory.createTitledBorder("Tan()"));

        tanLabel = new JLabel("Tan() of " + inputTF.getText() + " ");
        tanTF = new JTextField(5);
        tanTF.setEditable(false);

        tanPanel.add(tanLabel);
        tanPanel.add(tanTF);
    }

    // Building Constructor for cscPanel
    private void buildCscSecCotPanels()
    {
        // Set layout and border for cscPanel
        cscPanel.setLayout(new GridLayout(1,2));
        cscPanel.setBorder(BorderFactory.createTitledBorder("Csc()"));

        // 
        cscLabel = new JLabel("Csc() of " + inputTF.getText() + " ");
        cscTF = new JTextField(5);
        cscTF.setEditable(false);

        cscPanel.add(cscLabel);
        cscPanel.add(cscTF);

        // Set layout and border for secPanel
        secPanel.setLayout(new GridLayout(1,2));
        secPanel.setBorder(BorderFactory.createTitledBorder("Sec()"));

        secLabel = new JLabel("Sec() of " + inputTF.getText() + " ");
        secTF = new JTextField(5);
        secTF.setEditable(false);

        secPanel.add(secLabel);
        secPanel.add(secTF);

        // Set layout and border for cotPanel
        cotPanel.setLayout(new GridLayout(1,2));
        cotPanel.setBorder(BorderFactory.createTitledBorder("Cot()"));

        cotLabel = new JLabel("Cot() of " + inputTF.getText() + " ");
        cotTF = new JTextField(5);
        cotTF.setEditable(false);

        cotPanel.add(cotLabel);
        cotPanel.add(cotTF);
    }

    private void buildButtonPanel()
    {
        // Create buttons and add events
        calcButton = new JButton("Calculate");
        calcButton.addActionListener(new CalcButtonListener());
        CancelButton = new JButton("Cancel");
        CancelButton.addActionListener(new CancelButtonListener());
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

    private class CalcButtonListener implements ActionListener
    {
          public void actionPerformed(ActionEvent ae)
          {
              // Set input variable to input text field text
              input = inputTF.getText();

              // Assign double variables to double input
              sin = Double.parseDouble(input);
              cos = Double.parseDouble(input);
              tan = Double.parseDouble(input);
              csc = Double.parseDouble(input);
              sec = Double.parseDouble(input);
              cot = Double.parseDouble(input);

          }
    }

   /**
    *  Private inner class that handles the event when
    *  the user clicks the Exit button. 
    */

   private class CancelButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent ae)
      {
         // Exit the application.
          System.exit(0);
      }
   }
}

1 个答案:

答案 0 :(得分:2)

您没有在ContentPane中添加/设置对象mainPanel

...
mainPanel.add(cotPanel);
mainPanel.add(buttonPanel);

this.getContentPane().add(mainPanel);

// size of window to content
this.pack();
...