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 = 200;
private JLabel wordJL, sumJL;
private JTextField wordTF, sumTF;
private JButton calculateJB, exitJB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
public NumerologyEC()
{
setTitle ("Numerology Extra Credit");
wordJL = new JLabel ("Enter a word: ", SwingConstants.RIGHT);
sumJL = new JLabel ("Sum of letters: ", SwingConstants.RIGHT);
wordTF = new JTextField(10);
sumTF = 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 (3, 2));
pane.add(wordJL);
pane.add(wordTF);
pane.add(calculateJB);
pane.add(exitJB);
pane.add(sumJL);
pane.add(sumTF);
setSize(Width, Height);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
double letterSum;
letterSum = sumCharValues;
}
}
private int sumCharValues (String input){
String total = input.toLowerCase();
int result = 0;
for (int i = 0, n = total.length(); i < n; i++) {
char c = total.charAt(i);
result += (c - 'a' + 1);
}
return result;
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit (0);
}
}
public static void main (String[] args)
{
NumerologyEC rectObject = new NumerologyEC();
}
}
Как реализовать функцию sumcharvalues в «приватном классе CalculateButtonHandler реализует ActionListener?» im пытается добавить значение символов. просто новичок здесь, ребята. заблаговременно за помощьДобавление значений символов в java
Работает ли она? Попытайтесь объяснить проблему лучше. Также не забывайте, что вызов функции подобен 'sumCharValues ()' not 'sumCharValues'. – Christian