0
下面是我的代码。我无法添加所有6个按钮。一次只显示Button1 - 3或Button4-6。无法显示JFrame上的所有按钮
请让我知道我要去哪里错了。
// This class contains the main method and launches the Main screen
import javax.swing.*;
import java.awt.*;
public class LearningHome{
public static void main(String[] args){
JFrame mainFrame = new JFrame("Welcome to the Learning! ");
try {
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(800, 800);
mainFrame.setVisible(true); // Without this property the frame will not be visible
FlowLayout mainLayout = new FlowLayout();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(mainLayout);
mainPanel.add(new JButton(" Button 1 "));
mainPanel.add(new JButton(" Button 2 "));
mainPanel.add(new JButton(" Button 3 "));
JPanel subPanel = new JPanel();
subPanel.setLayout(mainLayout);
subPanel.add(new JButton(" Button 4 "));
subPanel.add(new JButton(" Button 5 "));
subPanel.add(new JButton(" Button 6 "));
mainFrame.add(mainPanel, mainLayout.LEFT);
mainFrame.setLocationRelativeTo(null);
mainFrame.add(subPanel, mainLayout.RIGHT);
}
}
PACK是一个不错的选择。但随着我的网页缩小,我不需要。除此之外,我已经实施了一切。你的解释很棒。非常感谢。 –