【问题标题】:Why are some of the items in my JPanel not showing?为什么我的 JPanel 中的某些项目没有显示?
【发布时间】:2016-02-24 12:21:12
【问题描述】:

我正在尝试将几个 JTextField、JButton 和 JLabels 添加到面板中,但是只显示按钮。我已将 JPanel 的布局设置为空,并为每个项目设置了位置/边界。

frame= new JFrame("Find and Replace");
        JPanel panel = new JPanel(null);
        JLabel findLabel = new JLabel("Find:");
        findLabel.setLocation(0, 20);
        JLabel replaceLabel = new JLabel("Replace with:");
        replaceLabel.setLocation(50, 60);
        findField = new JTextField(20);
        findField.setLocation(30, 100);
        replaceField = new JTextField(20);
        replaceField.setLocation(220, 140);
        replaceAllButton = new JButton("Find and Replace All");
        replaceAllButton.setBounds(20, 200, 100, 25);
        replaceButton = new JButton("Replace");
        replaceButton.setBounds(120, 200, 100, 25);

        panel.add(findLabel);
        panel.add(findField);
        panel.add(replaceLabel);
        panel.add(replaceField);
        panel.add(replaceButton);
        panel.add(replaceAllButton);

【问题讨论】:

  • “缺失”组件的大小为 0。但是,真正的问题是使用null 布局。使用布局管理器,因为 swing 是为使用而设计的,组件的大小将自动确定,窗口在调整大小时表现良好,等等。 null 布局乍一看可能看起来更容易,但会带来无穷无尽的麻烦。
  • 嗯,我该如何使用 GroupLayout 呢?我在这里看到了一些不同布局的例子,docs.oracle.com/javase/tutorial/uiswing/layout/visual.html,对我来说 GroupLayout 看起来很不错
  • GroupLayout 可能有效,但手动使用相当困难。我更喜欢组合布局,而不是试图一次解决整个布局问题。

标签: java swing


【解决方案1】:

您可以在这里使用 GridBagLayout 而不是使用 null 布局:

    GridBagConstraints gridBagConstraints;

    panel = new JPanel();
    findLabel = new javax.swing.JLabel();
    findField = new javax.swing.JTextField();
    replaceLabel = new javax.swing.JLabel();
    replaceField = new javax.swing.JTextField();
    replaceAllButton = new javax.swing.JButton();
    replaceButton = new javax.swing.JButton();

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    panel.setLayout(new GridBagLayout());

    findLabel.setText("Find :");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.ipadx = 94;
    gridBagConstraints.ipady = 30;
    panel.add(findLabel, gridBagConstraints);
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.ipadx = 149;
    panel.add(findField, gridBagConstraints);

    replaceLabel.setText("Replace With :");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.ipadx = 34;
    gridBagConstraints.ipady = 23;
    panel.add(replaceLabel, gridBagConstraints);
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.ipadx = 149;
    panel.add(replaceField, gridBagConstraints);

    replaceAllButton.setText("Find And Replace All");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.ipady = 9;
    panel.add(replaceAllButton, gridBagConstraints);

    replaceButton.setText("Replace");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.ipadx = 99;
    gridBagConstraints.ipady = 8;
    panel.add(replaceButton, gridBagConstraints);
    //adding panel to the jframe
    getContentPane().add(panel, BorderLayout.CENTER);

    pack();

【讨论】:

  • 实际上,null 布局不太可能导致 OP 描述的问题,而在某些情况下使用布局管理器可以。关键是(是的,他们应该使用布局管理器),但是如果没有清楚地了解 OP 正在做什么,就无法回答为什么某些组件没有出现
  • @MadProgrammer 没有来自 JPanel 的插入被添加到 JFrame,部分在后面,由 JPanel 覆盖
  • @mKorbel 有可能知道。 JPanel 从未添加到示例代码中的 JFrame 中,这是问题的根本原因还是其他原因?
【解决方案2】:

您将所有对象添加到面板,但忘记将面板添加到框架(JFrame 的对象) 将此行添加到您的代码中 frame.add(panel);

【讨论】:

  • 他没有展示他的全部代码。否则他也无法向我们展示框架的图片(包含组件)。 kiheru 的评论已经回答了这个问题。
猜你喜欢
  • 2021-09-28
  • 2021-04-04
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多