前提・実現したいこと
java初心者も初心者です。
電卓を作っています。
ボタンにまとめてアクションを追加できなかったので、ボタン一つ一つにアクションを追加したいと思っています。
それぞれの四則演算ボタンを押したら計算できるようにしたいのですが、どのサイトを調べてもまとめたやり方しか載っていないのでできません。
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
import javax.swing.;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.;
import java.awt.event.*;
public class calculator extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
calculator frame = new calculator();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 100, 350, 300); frame.setTitle("Calculator"); frame.setVisible(true);
}
calculator(){
JTextField result = new JTextField(""); add(result, BorderLayout.NORTH); JButton button1 = new JButton("1"); JButton button2 = new JButton("2"); JButton button3 = new JButton("3"); JButton button4 = new JButton("4"); JButton button5 = new JButton("5"); JButton button6 = new JButton("6"); JButton button7 = new JButton("7"); JButton button8 = new JButton("8"); JButton button9 = new JButton("9"); JButton button10 = new JButton("0"); JButton button11 = new JButton("+"); JButton button12 = new JButton("-"); JButton button13 = new JButton("="); JButton button14 = new JButton("×"); JButton button15 = new JButton("00"); JButton button16 = new JButton("000"); JButton button17 = new JButton("C"); button1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("1"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("2"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button3.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("3"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button4.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("4"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button5.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("5"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button6.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("6"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button7.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("7"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button8.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("8"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button9.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("9"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button10.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("0"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button15.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("00"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); button16.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ String keyNumber = this.getText(); appendResult("000"); } private String getText() { // TODO 自動生成されたメソッド・スタブ return null; } public void appendResult(String c) { boolean afterCalc = false; if (!afterCalc) result.setText(result.getText() + c); else { result.setText(c); afterCalc = false; } } } ); JPanel keyPanel = new JPanel(); keyPanel.setLayout(new GridLayout(4, 4)); add(keyPanel, BorderLayout.CENTER); keyPanel.add(button7); keyPanel.add(button8); keyPanel.add(button9); keyPanel.add(button11); keyPanel.add(button4); keyPanel.add(button5); keyPanel.add(button6); keyPanel.add(button12); keyPanel.add(button1); keyPanel.add(button2); keyPanel.add(button3); keyPanel.add(button14); keyPanel.add(button10); keyPanel.add(button15); keyPanel.add(button16); keyPanel.add(button13);
add (button17, BorderLayout.SOUTH);
button17.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
double stackedValue = 0.0;
boolean isStacked = false;
boolean afterCalc = false;
result.setText("");
}
}
);
this.setVisible(true);
}
}
試したこと
まとめたやり方のコードの一部を切り取ってはるなどして見ましたが、無理でした。
eclipseで起動しています。
ここにより詳細な情報を記載してください。
