gpt4 book ai didi

java - actionlistener 在 java 计算器中没有响应

转载 作者:行者123 更新时间:2023-11-30 06:38:15 24 4
gpt4 key购买 nike

请看下面的计算器界面代码,从我初学者的角度来看,“1”应该在按下时显示,但显然我做错了什么。有什么建议吗?

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
*A Class that operates as the framework for a calculator.
*No calculations are performed in this section
*/
public class CalcFrame extends JPanel
{
private CalcEngine calc;

private JFrame frame;
private JTextField display;
private JLabel status;

/**
* Constructor for objects of class GridLayoutExample
*/
//public CalcFrame(CalcEngine engine)
//{

//frame.setVisible(true);
// calc = engine;
// makeFrame();

//}
public CalcFrame() {
makeFrame();
calc = new CalcEngine();
}
class ButtonListener implements ActionListener {
ButtonListener() {
}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("1")) {
System.out.println("1");
}
}
}




/**
* This allows you to quit the calculator.
*/

// Alows the class to quit.
private void quit()
{
System.exit(0);


}

// Calls the dialog frame with the information about the project.
private void showAbout()
{
JOptionPane.showMessageDialog(frame,
"Declan Hodge and Tony O'Keefe Group Project",
"About Calculator Group Project",
JOptionPane.INFORMATION_MESSAGE);
}


// ---- swing stuff to build the frame and all its components ----

/**
* The following creates a layout of the calculator frame.
*/
private void makeFrame()
{
frame = new JFrame("Group Project Calculator");
makeMenuBar(frame);

JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10));





/**
* Insert a text field
*/
display = new JTextField(8);
contentPane.add(display, BorderLayout.NORTH);

//Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(4, 5));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
contentPane.add(new JButton("9"));
contentPane.add(new JButton("8"));
contentPane.add(new JButton("7"));
contentPane.add(new JButton("6"));
contentPane.add(new JButton("5"));
contentPane.add(new JButton("4"));
contentPane.add(new JButton("3"));
contentPane.add(new JButton("2"));
contentPane.add(new JButton("1"));
contentPane.add(new JButton("0"));
contentPane.add(new JButton("+"));
contentPane.add(new JButton("-"));
contentPane.add(new JButton("/"));
contentPane.add(new JButton("*"));
contentPane.add(new JButton("="));
contentPane.add(new JButton("C"));
contentPane.add(new JButton("CE"));
contentPane.add(new JButton("%"));
contentPane.add(new JButton("#"));
//contentPane.add(buttonPanel, BorderLayout.CENTER);

frame.pack();
frame.setVisible(true);
}

/**
* Create the main frame's menu bar.
* The frame that the menu bar should be added to.
*/










private void makeMenuBar(JFrame frame)
{
final int SHORTCUT_MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();


JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);

JMenu menu;
JMenuItem item;

// create the File menu
menu = new JMenu("File");
menubar.add(menu);






// create the Quit menu with a shortcut "Q" key.
item = new JMenuItem("Quit");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { quit(); }
});
menu.add(item);

// Adds an about menu.
menu = new JMenu("About");
menubar.add(menu);

// Displays
item = new JMenuItem("Calculator Project");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { showAbout(); }
});
menu.add(item);

}

}

最佳答案

代码太多了!

您实际上并没有创建 ButtonListener,更不用说将一个添加到按钮了。

关于java - actionlistener 在 java 计算器中没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2474288/

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