需要了解

时间:2019-10-24 11:48:32

标签: java swing user-interface

我需要帮助

  

定义一个扩展JPanel类的类,而不是分别创建按钮和面板。在面板类中放置两个按钮,然后根据用户定义的面板类创建两个面板。

import java.awt.*;
import javax.swing.*;

public class P1Q7 extends JFrame {

    public P1Q7() {
        setLayout (new BorderLayout(10,20));

        JPanel P1 = new JPanel();
        P1.setLayout(new GridLayout(1,2));
        P1.add(new JButton("P1Button 1"));
        P1.add(new JButton("P1Button 2"));

        JPanel P2 = new JPanel();
        P2.setLayout(new GridLayout(1,2));
        P2.add(new JButton("P2Button 1"));
        P2.add(new JButton("P2Button 2"));

        add(P1, BorderLayout.CENTER);
        add(P2, BorderLayout.SOUTH);

        setTitle("P1Q7");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 250);
        setVisible(true);
    }

    public static void main(String[] args) {
        P1Q7 frame = new P1Q7();
    }
}

1 个答案:

答案 0 :(得分:2)

启动:

   JPanel p1 = new TwoButtonPanel();
   JPanel p2 = new TwoButtonPanel();

public class TwoButtonPanel extends JPanel {

    public TwoButtonPanel() {
    }
}