0

ふぅ。私はこの質問で長い間立ち往生しています。ポップクイズをシミュレートする GUI プログラムを実行しています。しかし、プログラムをこのようにしたいときに、どのコードを配置すればよいかわかりません...起動

そして、スタートボタンをクリックすると、パネルは次のようになるはずです... 質問

これまでのところ、これは私がスタートアップメニューに持っているものです...

public static void main (String []args){
    JFrame f = new JFrame("Pop Quiz");
    f.setSize(400,300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);
    f.setResizable(false);

    JPanel p1 = new JPanel();
    p1.setSize(400,100);
    p1.setLocation(0,0);
    p1.setLayout(new GridLayout(3,1));
    f.add(p1);

    JLabel l1 = new JLabel("Welcome to POP Quiz!");
    p1.add(l1);

    JLabel l2 = new JLabel("Enter your name:");
    p1.add(l2);

    final JTextField name = new JTextField ();
    p1.add(name);

    JPanel p2 = new JPanel();
    p2.setSize(400,50);
    p2.setLocation(0,225);
    f.add(p2);

    JButton start = new JButton ("Start");
    p2.add(start);

    start.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             String player = name.getText();
             //what should be added here to change the contents of the panel?
         }
    });

    f.show();
}

そして、質問に対して...

public static void main(String[] args){
    JFrame f = new JFrame("Pop Quiz");
    f.setSize(400,300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);
    f.setResizable(false);

    JPanel p1 = new JPanel();
    p1.setSize(400,100);
    p1.setBackground(Color.PINK);
    f.add(p1);

    JLabel question = new JLabel();
    question.setText("In computers, what is the smallest and basic unit of information storage?");
    p1.add(question);

    JPanel p2 = new JPanel();
    p2.setSize(400,175);
    p2.setLocation(0,100);
    p2.setLayout(new GridLayout(2,4));
    f.add(p2);

    JButton a = new JButton("a. Bit");
    p2.add(a);

    JButton b = new JButton("b. Byte");
    p2.add(b);

    JButton c = new JButton("c. Data");
    p2.add(c);        

    JButton d = new JButton("d. Newton");
    p2.add(d);        

    f.show();
}

どなたでもお役に立てれば幸いです。前もって感謝します!良い1日を!:)

4

4 に答える 4

1

を使用しCardLayoutます。ここに示すように。

ゲームビュー ハイスコ​​アビュー

チップ

レイアウト

f.setLayout(null);

レイアウトを使おう! 私はこれを十分に強調することはできません。レイアウトは複雑に見えるかもしれませんが、さまざまなプラットフォーム (PLAF、画面解像度など) で使用することを目的とした GUI でコンポーネントの複雑なグループをレイアウトするための唯一の実行可能なソリューションです。

コントロール

JButton a = new JButton("a. Bit");
p2.add(a);

JButton b = new JButton("b. Byte");
// ..

JComboBoxボタンに使用される文字列の性質を考えると、 、 、JListまたはのボタンが最適であると思われますButtonGroup

非推奨のメソッド

f.show();

このメソッドは推奨されていません。コンパイラは、このメソッドが推奨されていないこと、または無視されている追加の警告があることを警告する必要があります。そのような警告を調べて、修正してください。メソッドは何らかの理由で廃止されています。

于 2013-03-08T08:32:09.833 に答える
0

これを達成するための手順:
1.メインパネルを作成し、に追加しJFrame
ます。 2.メインのwelcomePanelにコンテンツを追加します。3. ユーザーがwelcomePanel の「開始」ボタンを押したら。メインコンテナを呼び出します。4. mainのcontentPanelに 新しいコンテンツを追加し、 mainを更新する呼び出しを行います。 JPanel
removeAll()
JPanelrevalidate()

注: このために jframe の別のインスタンスは必要ありません。

于 2013-03-08T08:48:41.900 に答える
0

f.getContentPane().add(panel); を試してください。

于 2013-03-08T08:30:17.237 に答える
0
Try this.. 


   public class test {
    public static void main(String[] args) {
        final JFrame f = new JFrame("Pop Quiz");
        f.setSize(400, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);
        f.setResizable(false);

        final JPanel p1 = new JPanel();
        p1.setSize(400, 100);
        p1.setLocation(0, 0);
        p1.setLayout(new GridLayout(3, 1));
        f.add(p1);

        JLabel l1 = new JLabel("Welcome to POP Quiz!");
        p1.add(l1);

        JLabel l2 = new JLabel("Enter your name:");
        p1.add(l2);

        final JTextField name = new JTextField();
        p1.add(name);

        final JPanel p2 = new JPanel();
        p2.setSize(400, 50);
        p2.setLocation(0, 225);
        f.add(p2);

        JButton start = new JButton("Start");
        p2.add(start);

        start.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String player = name.getText();
               p1.setVisible(false);
               p2.setVisible(false);
                JPanel testPanel = new JPanel();
                testPanel.setSize(400, 100);
                testPanel.setBackground(Color.PINK);
                f.add(testPanel);

                JLabel question = new JLabel();
                question.setText("<html>In computers, what is the smallest and basic unit<br/> of information storage?</html>");
                testPanel.add(question);

                JPanel p2 = new JPanel();
                p2.setSize(400, 175);
                p2.setLocation(0, 100);
                p2.setLayout(new GridLayout(2, 4));
                f.add(p2);

                JButton a = new JButton("a. Bit");
                p2.add(a);

                JButton b = new JButton("b. Byte");
                p2.add(b);

                JButton c = new JButton("c. Data");
                p2.add(c);

                JButton d = new JButton("d. Newton");
                p2.add(d);

                f.show();
            }
        });

        f.show();
    }
}
于 2013-03-08T08:42:21.920 に答える