-1

コード : ユーザーが値を入力すると、フレームが小さすぎて 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);

    }
}
4

1 に答える 1

0

コンテンツを JScrollPane に入れるだけです (次に JScrollPane を JFrame に追加します)。それはトリックを行います。

于 2012-12-16T15:57:51.600 に答える