【发布时间】:2014-03-07 01:24:27
【问题描述】:
问题来了:
“命理”
使用 JFrame 创建一个程序,该程序允许将单词的字符串输入到文本框中,然后 计算输出的每个字母的值的总和。一定要包括一个“计算” 和“退出”按钮。 例如:sky = 55,因为 s = 19,k = 11,y = 25,所以 19 + 11 + 25 = 55。
import java.awt.*; //Container, GridLayout, *, or etc...
import javax.swing.*; //JFrame, JLabel, *, or etc...
import java.awt.event.*;
public class NumerologyEC extends JFrame
{
private static final int Width = 400;
private static final int Height = 100;
private JLabel wordJL;
private JTextField wordTF;
private JButton calculateJB, exitJB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
public NumerologyEC()
{
setTitle ("Numerology Extra Credit");
wordJL = new JLabel ("Enter a word: ", SwingConstants.RIGHT);
wordTF = new JTextField(10);
calculateJB = new JButton ("Calculate");
cbHandler = new CalculateButtonHandler();
calculateJB.addActionListener (cbHandler);
exitJB = new JButton ("Exit");
ebHandler = new ExitButtonHandler();
exitJB.addActionListener (ebHandler);
Container pane = getContentPane();
pane.setLayout (new GridLayout (2, 2));
pane.add(wordJL);
pane.add(wordTF);
pane.add(calculateJB);
pane.add(exitJB);
setSize(Width, Height);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String word;
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit (0);
}
}
public static void main (String[] args)
{
NumerologyEC rectObject = new NumerologyEC();
}
}
我应该使用什么方法来解决问题?我已经设置了我的 jframe 现在我只需要一种方法来解决这个问题。我只是一个初学者,所以在编程方面我仍在努力解决问题。非常感谢任何提示。
【问题讨论】: