【问题标题】:Attempting to set the layout to BoxLayout尝试将布局设置为 BoxLayout
【发布时间】:2020-02-05 08:39:39
【问题描述】:

我似乎无法在线找到解决方案,说明为什么我在尝试运行时遇到此错误

我正在为不同的程序制作一个简单的测试系统,当按钮按下时会在文本框中产生值。我希望它们位于不同的行以使其更清洁,因此我研究了布局。我认为 Box Layout 最适合我。在尝试此操作之前,我查看了不同的示例,我的代码最终看起来像这样,(为凌乱的代码道歉)

更新

让盒子布局错误消失,但代码不会将它们放在面板/框架上。当文本字段变得非常大时,标签和按钮左对齐。我不需要这样做

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import static javax.swing.BoxLayout.Y_AXIS;
import static javax.swing.SwingConstants.CENTER;

public class button extends JFrame {
static JFrame f;
static JButton b;
static JLabel l;

// main class
public static void main(String[] args)
{
    // create a new frame to stor text field and button
    f = new JFrame("panel");
    BoxLayout layout = new BoxLayout(f, BoxLayout.Y_AXIS);
    f.setLayout(layout);

    // create a label to display text
    l = new JLabel("panel label");
    b = new JButton("button1");
    JTextField textArea = new JTextField(5);
    textArea.setEditable(false);
    //textArea.append("Hello World");


    // create a panel to add buttons
    JPanel p = new JPanel();

    // add buttons and textfield to panel
    f.add(p);
    f.setSize(300, 300);
    p.add(l);
    p.add(b);
    p.setBackground(Color.white);
    p.add(textArea);
    f.show();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            Random r = new Random();
            textArea.setText(String.valueOf(r));

        }
    });
}
}






Error
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at java.desktop/javax.swing.BoxLayout.checkContainer(BoxLayout.java:461)
at java.desktop/javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:245)
at java.desktop/javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:278)
at java.desktop/java.awt.Container.addImpl(Container.java:1152)
at java.desktop/java.awt.Container.add(Container.java:1029)
at java.desktop/javax.swing.JFrame.addImpl(JFrame.java:553)
at java.desktop/java.awt.Container.add(Container.java:436)
at button.main(button.java:36)

我希望将这三件物品一个一个叠放在另一个上面,并在它们之间留出空间。顺序现在无关紧要。

【问题讨论】:

  • 你说你“得到这个错误”;但并不完全清楚“这个错误”是什么。请澄清。
  • BoxLayout 设置为JPanelp 的布局管理器,而不是JFrame
  • 我查看了不同的示例 - 示例的最佳来源是 Oracle Swing Tutorial,将所有 Swing 基础知识集中在一个地方。 How to Use BoxLayout 部分有工作代码供您下载和测试。这段代码应该是起点,因为它还展示了一种更好的代码结构方式。例如,您不应该 1) 扩展 JFrame 2) 使用静态变量。您应该 1) 在事件调度线程上创建 GUI。

标签: java swing layout-manager boxlayout


【解决方案1】:

Swing 于 1998 年首次添加到 JDK 中,此后经历了很多变化。不幸的是,当您阅读有关 Swing 的网页时,该页面最后一次更新的时间并不明显。因此,您可能正在学习编写 Swing 代码的过时技术。

首先,根据您发布的代码,button 类不需要扩展JFrame 类,因为您使用静态变量作为应用程序的JFrame。另外,JFrame 是一个顶级容器,这使它成为一种特殊的容器,而不是与JPanel 相同的容器。您需要为JPanel 设置布局管理器,然后将JLabelJTextFieldJButton 添加到JPanel。然后将JPanel 添加到JFrame

调用JFrame 类的方法pack() 将自动为JFrame 中的组件设置首选大小。它出现在下面的代码中。

还请查看Java coding conventions,它可以让其他人更轻松地阅读和理解您的代码。请注意,根据这些约定,我将您的类从 button 重命名为 Buttons 并且还因为 JDK 中已经有几个名为 Button 的类。

这是我对你的代码的重写...

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Buttons implements Runnable {

    public void run() {
        createAndShowGui();
    }

    private void createAndShowGui() {
        JFrame f = new JFrame("Box");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        BoxLayout layout = new BoxLayout(p, BoxLayout.Y_AXIS);
        p.setLayout(layout);
        JLabel l = new JLabel("panel label");
        JTextField textField = new JTextField(5);
        JButton b = new JButton("button1");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Random r = new Random();
                textField.setText(String.valueOf(r.nextBoolean()));
            }
        });
        p.add(l);
        p.add(textField);
        p.add(b);
        f.add(p);
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Buttons instance = new Buttons();
        EventQueue.invokeLater(instance);
    }
}

【讨论】:

  • 因此,您可能正在学习编写 Swing 代码的过时技术。 - 所以最好的起点是Swing Tutorial,因为它包含大多数 Swing 基础知识的示例,包括How to Use a BoxLayout 部分。
猜你喜欢
  • 2014-04-07
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 2012-11-08
  • 1970-01-01
相关资源
最近更新 更多