这段代码在我的教科书中,但是我不理解的是方法TestPanels()。它没有返回类型,也没有空。怎么会这样

public class TestPanels extends JFrame {

public TestPanels() {
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(4,3));

    for (int i = 1; i <= 9; i++) {
        p1.add(new JButton(""+i));
    }

    p1.add(new JButton(""+0));
    p1.add(new JButton("Start"));
    p1.add(new JButton("Stop"));

    JPanel p2 = new JPanel(new BorderLayout());
    p2.add(new JTextField("Time to be displayed here"), BorderLayout.NORTH);
    p2.add(p1, BorderLayout.CENTER);

    add(p2, BorderLayout.EAST);
    add(new JButton("Food to be placed here"), BorderLayout.WEST);

}

public static void main(String[] args) {
    TestPanels frame = new TestPanels();
    frame.setTitle("The Front View of a Microwave Oven");
    frame.setSize(400, 250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

最佳答案

它是构造函数,而不是方法。方法将始终具有return-type或void(无返回值)。

09-11 18:03
查看更多