我需要这方面的帮助
Instead of creating buttons and panels separately, define a class that extends the JPanel class. Place two buttons in your panel class, and create two panels from the user-defined panel class.
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();
}
}
最佳答案
启动:
JPanel p1 = new TwoButtonPanel();
JPanel p2 = new TwoButtonPanel();
public class TwoButtonPanel extends JPanel {
public TwoButtonPanel() {
}
}
关于java - 需要理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58540747/