代码:用户输入一个值,代码在一个带有滚动条的框架中返回计算值,因为该框架太小而不能包含 100 行。
package Gui;
import java.awt.*;
import javax.swing.*;
public class Rekenen2 extends JFrame {
public Rekenen2() {
// setLayout(new GridLayout(3,1));
JPanel panel1 = new JPanel();
panel1.setBackground(Color.BLACK);
JButton jbtComputeButton = new JButton("Compute");
JPanel panel2 = new JPanel();
panel2.setBackground(Color.BLACK);
JPanel panel3 = new JPanel();
final JTextField jtxInputTextField = new JTextField(8);
final JLabel outputInPanel = new JLabel();
panel1.add(jbtComputeButton, FlowLayout.LEFT);
panel2.add(jtxInputTextField, FlowLayout.LEFT);
panel3.add(outputInPanel);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
jbtComputeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int total = 0;
int parsedInputValue = Integer.parseInt(jtxInputTextField
.getText());
for (int i = 0; i < 100; i++) {
total = (parsedInputValue * i);
outputInPanel.setText("" + total);
}
}
});
}
public static void main(String[] args) {
JFrame frame = new Rekenen2();
frame.setSize(300, 300);
frame.setTitle("Compute App");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}