求助大神,需要编写一个加法计算程序,需要达到这个效果不知道错在哪里了,求助
写的代码如下:
import javax.swing.JFrame;public class Adder{
/**
The main method creates an instance of the
Adder class, which displays
its window on the screen.
*/ public static void main(String[] args)
{
final int WINDOW_WIDTH = 520; // Window width
final int WINDOW_HEIGHT = 100; // Window height
AdderFrame adder = new AdderFrame();
// Specify what happens when the close button is clicked.
adder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the size of the window.
adder.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Display the window.
adder.setVisible(true);
}
}
import javax.swing.*; // Needed for Swing classes
import java.awt.event.*; // Needed for ActionListener Interface/**
The AdderFrame class displays a JFrame that
lets the user to add two integers
*/public class AdderFrame extends JFrame
{
private JPanel panel; // To reference a panel
private JLabel messageLabelAdd; // To reference a label
private JLabel messageLabelequal; // To reference a label
private JTextField firstNumber; // To reference a text field
private JTextField secondNumber; // To reference a text field
private JLabel result;
private JButton calcButton; // To reference a button /**
Constructor
*/
public AdderFrame()
{
super("Integer Adder");
/**
The buildPanel method adds a label, text field, and
and a button to a panel.
*/
// Build the panel and add it to the frame.
// Create a label to display instructions.
messageLabelAdd = new JLabel(" + ");
messageLabelequal = new JLabel(" = ");
result = new JLabel("");
// Create two text fields 10 characters wide.
firstNumber = new JTextField(10);
secondNumber = new JTextField(10);
// Create a button with the caption "Calculate".
calcButton = new JButton("Calculate");
String a = firstNumber.getText();
String b = secondNumber.getText();
int c = Integer.valueOf(a).intValue();
int d = Integer.valueOf(b).intValue();
int sum = c + d;
String s = Double.toString(sum);
result.setText(s);
// Create a JPanel object and let the panel
// field reference it.
panel = new JPanel(); // Add the label, text field, and button
// components to the panel.
panel.add(firstNumber);
panel.add(messageLabelAdd);
panel.add(secondNumber);
panel.add(messageLabelequal);
panel.add(result);
panel.add(calcButton); // Add the panel to the frame's content pane.
add(panel);
}
}
写的代码如下:
import javax.swing.JFrame;public class Adder{
/**
The main method creates an instance of the
Adder class, which displays
its window on the screen.
*/ public static void main(String[] args)
{
final int WINDOW_WIDTH = 520; // Window width
final int WINDOW_HEIGHT = 100; // Window height
AdderFrame adder = new AdderFrame();
// Specify what happens when the close button is clicked.
adder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the size of the window.
adder.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Display the window.
adder.setVisible(true);
}
}
import javax.swing.*; // Needed for Swing classes
import java.awt.event.*; // Needed for ActionListener Interface/**
The AdderFrame class displays a JFrame that
lets the user to add two integers
*/public class AdderFrame extends JFrame
{
private JPanel panel; // To reference a panel
private JLabel messageLabelAdd; // To reference a label
private JLabel messageLabelequal; // To reference a label
private JTextField firstNumber; // To reference a text field
private JTextField secondNumber; // To reference a text field
private JLabel result;
private JButton calcButton; // To reference a button /**
Constructor
*/
public AdderFrame()
{
super("Integer Adder");
/**
The buildPanel method adds a label, text field, and
and a button to a panel.
*/
// Build the panel and add it to the frame.
// Create a label to display instructions.
messageLabelAdd = new JLabel(" + ");
messageLabelequal = new JLabel(" = ");
result = new JLabel("");
// Create two text fields 10 characters wide.
firstNumber = new JTextField(10);
secondNumber = new JTextField(10);
// Create a button with the caption "Calculate".
calcButton = new JButton("Calculate");
String a = firstNumber.getText();
String b = secondNumber.getText();
int c = Integer.valueOf(a).intValue();
int d = Integer.valueOf(b).intValue();
int sum = c + d;
String s = Double.toString(sum);
result.setText(s);
// Create a JPanel object and let the panel
// field reference it.
panel = new JPanel(); // Add the label, text field, and button
// components to the panel.
panel.add(firstNumber);
panel.add(messageLabelAdd);
panel.add(secondNumber);
panel.add(messageLabelequal);
panel.add(result);
panel.add(calcButton); // Add the panel to the frame's content pane.
add(panel);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货