1

这个程序是一个三角计算程序。我有一个问题,它实际上出现在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);
      }
   }
}
4

1 回答 1

2

您没有添加 / 设置为 ContentPane 您的对象mainPanel

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

this.getContentPane().add(mainPanel);

// size of window to content
this.pack();
...
于 2013-06-28T15:46:12.247 回答