gpt4 book ai didi

java - 缺少 GUI 输出

转载 作者:行者123 更新时间:2023-11-30 04:19:49 27 4
gpt4 key购买 nike

这个程序是一个三角函数计算程序。我在它实际显示在 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);
}
}
}

最佳答案

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

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

this.getContentPane().add(mainPanel);

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

关于java - 缺少 GUI 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17368566/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com