【问题标题】:How can i use multiple panels with different layouts for my gui?如何为我的 gui 使用具有不同布局的多个面板?
【发布时间】:2020-08-06 18:36:34
【问题描述】:

在我的主面板中嵌套 2 个不同的按钮面板时遇到问题。它对我来说只有一个 1 面板。现在通过我的实际嵌套,我在主面板和第一个按钮面板上看到了最后添加的面板。此外,我添加的“editButton”没有显示出来。我编译时没有错误。看来我只是在安排上犯了一个错误。 如果有任何建议会非常好。这是我的代码sn-p: 提前致谢!

private final String name;
private JLabel catLabel;
private JButton cat1Button;
private JButton cat2Button;
private JButton cat3Button;
private JButton cat4Button;
private JButton editButton;
private JButton deleteButton;
private JButton insertButton;

public JPanel panelMain;
public JPanel panel;
public JPanel panel1;


public ReadwriteQuiz readwrite;

public GUIEdit(String name){

    this.name = name;
    this.setTitle(name);

    this.setLocationRelativeTo(null);
    this.setLayout((null));
    this.setSize(400,300);
    this.setLocation(400,150);
    this.setResizable(false);

    panelMain = new JPanel();
    panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));

    panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.setBackground(Color.lightGray);

    panel1 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
    panel1.setBackground(Color.ORANGE);

    panelMain.add(panel);
    panelMain.add(panel1);

    catLabel= new JLabel("Willkommen zum Quizzeln ! \n Wählen Sie Ihre Kategorie");
    catLabel.setBounds(90,10,260,40);
    panel.add(catLabel);

    cat1Button = new JButton("Kategorie 1");
    cat1Button.setBounds(52,90,120,40);
    panel.add(cat1Button);
    cat1Button.addActionListener((e) -> {
        try {
            readwrite.readFile("kategorie1.txt");
        } catch (FileNotFoundException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
        }
    });

    cat2Button = new JButton("Kategorie 2");
    cat2Button.setBounds(220,90,120,40);
    panel.add(cat2Button);

    cat3Button = new JButton("Kategorie 3");
    cat3Button.setBounds(52,160,120,40);
    panel.add(cat3Button);

    cat4Button = new JButton("Kategorie 4");
    cat4Button.setBounds(220,160,120,40);
    panel.add(cat4Button);

    editButton = new JButton("Frage editieren");
    editButton.setBounds(52,400,120,40);
    panel1.add(editButton);

    this.add(panelMain);
    this.add(panel);
    this.add(panel1);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {

}

【问题讨论】:

    标签: java user-interface panel


    【解决方案1】:

    您有一个主面板,并且您已将其他嵌套面板添加到主面板 所以您只需将该主面板添加到父面板 但是您要添加主面板(包括其他 2 个)并再次添加第一个面板并再次添加第二个面板

    所以

    this.add(panelMain);
    

    应该够了


    全班

    public class app {
        private JTextField textField1;
        private JPanel panel;
        private JLabel label;
        private JLabel catLabel;
        private JButton cat1Button;
        private JButton cat2Button;
        private JButton cat3Button;
        private JButton cat4Button;
        private JButton editButton;
        private JButton deleteButton;
        private JButton insertButton;
    
        public JPanel panelMain;
        public JPanel panel2;
        public JPanel panel1;
    
        public app(JFrame frame) {
    
            panelMain = new JPanel();
            panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
    
            panel2 = new JPanel();
            panel2.setLayout(new FlowLayout());
            panel2.setBackground(Color.lightGray);
    
            panel1 = new JPanel();
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
            panel1.setBackground(Color.ORANGE);
    
            panelMain.add(panel2);
            panelMain.add(panel1);
    
            catLabel= new JLabel("Willkommen zum Quizzeln ! \n Wählen Sie Ihre Kategorie");
            catLabel.setBounds(90,10,260,40);
            panel2.add(catLabel);
    
            cat1Button = new JButton("Kategorie 1");
            cat1Button.setBounds(52,90,120,40);
            panel2.add(cat1Button);
    
            cat2Button = new JButton("Kategorie 2");
            cat2Button.setBounds(220,90,120,40);
            panel2.add(cat2Button);
    
            cat3Button = new JButton("Kategorie 3");
            cat3Button.setBounds(52,160,120,40);
            panel2.add(cat3Button);
    
            cat4Button = new JButton("Kategorie 4");
            cat4Button.setBounds(220,160,120,40);
            panel2.add(cat4Button);
    
            editButton = new JButton("Frage editieren");
            editButton.setBounds(52,400,120,40);
            panel1.add(editButton);
            frame.add(panelMain);
        }
    
        public static void main(String[] args) {
            JFrame fram = new JFrame("app");
            new app(fram);
            fram.pack();
            fram.setVisible(true);
            }
        }
    

    【讨论】:

    • 好吧,是的,这对我来说很有意义,但不幸的是它并没有改变任何东西......我还在添加按钮后将 panelMain.add(panel) 和 panelMain.add(panel1) 放在了位置
    • 我刚试了一下,效果不错,我猜你的观点应该是这样的www2.0zz0.com/2020/08/06/22/765528528.png
    • @JanLi this 指的是什么?
    • 这应该是指框架/窗口
    • 好吧,我在 Java GUI 方面不是很专家,但除了删除 2 个额外添加之外,我所做的唯一区别是用实际框架更改“this”,这是我的完整课程,我将编辑答案跨度>
    猜你喜欢
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多