0

だから私は独学でJavaを教えていて、ちょっとした失速に遭遇しました。演習の 1 つとして、ここに説明があります。説明の助けをいただければ幸いです。これが私が問題を抱えている部分での私の試みです。

import java.awt.Dimension;  
import java.awt.GridLayout;  
import javax.swing.JButton;  
import javax.swing.JPanel;  
import javax.swing.JScrollPane;  
import javax.swing.JTextArea;  

public class ButtonPanel extends JPanel {


    public ButtonPanel(JButton[] buttons, JTextArea textArea) {
        //TODO: Create a sub-panel with a 4 row, 3 column GridLayout

        setLayout(new GridLayout(4,3)); //Layout of subPanel1

        JButton b1 = new JButton ("A");
        JButton b2 = new JButton ("B");
        JButton b3 = new JButton ("C");
        JButton b4 = new JButton ("1");
        JButton b5 = new JButton ("2");
        JButton b6 = new JButton ("3");
        JButton b7 = new JButton ("X");
        JButton b8 = new JButton ("Y");
        JButton b9 = new JButton ("Z");

        add(b1);
        add(b2);
        add(b3);
        add(b4);
        add(b4);
        add(b5);

        //TODO: Populate the grid with buttons

        //TODO: Add the grid panel to this panel

        //TODO: Create a JScrollPane containing textArea

        JButton cr = new JButton();

        //TODO: Set the preferred size of the scroll pane to 80x120
        setPreferredSize (new Dimension(80, 120));

        //TODO: Add the scroll pane to this panel

    }


}
4

1 に答える 1

1

これは初歩的な概念です。

コンポーネントをコンテナに追加するには、次のことが必要です。

  1. コンテナを作成する
  2. そのコンテナーにレイアウト マネージャーを適用します。
  3. そのコンテナにコンポーネントを追加します
  4. トップレベルのコンテナに(何らかの方法で)アタッチされている親コンテナにコンテナを追加します

例えば

public void ButtonPanel(JButton[] buttons, JTextArea textArea) {
    //TODO: Create a sub-panel with a 4 row, 3 column GridLayout

    JPanel buttonPanel = new JPanel(new GridLayout(4,3)); //Layout of subPanel1

    JButton b1 = new JButton ("A");
    JButton b2 = new JButton ("B");
    JButton b3 = new JButton ("C");
    JButton b4 = new JButton ("1");
    JButton b5 = new JButton ("2");
    JButton b6 = new JButton ("3");
    JButton b7 = new JButton ("X");
    JButton b8 = new JButton ("Y");
    JButton b9 = new JButton ("Z");

    buttonPanel.add(b1);
    buttonPanel.add(b2);
    buttonPanel.add(b3);
    buttonPanel.add(b4);
    buttonPanel.add(b4);
    buttonPanel.add(b5);

    //TODO: Populate the grid with buttons

    //TODO: Add the grid panel to this panel

    //TODO: Create a JScrollPane containing textArea

    JButton cr = new JButton();

    //TODO: Set the preferred size of the scroll pane to 80x120
    // This is a bad idea
    setPreferredSize (new Dimension(80, 120));

    //TODO: Add the scroll pane to this panel

}

時間をかけて Swing を使用した UI の作成を読み、理解してください。

于 2013-04-25T23:56:02.920 に答える