Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JList GUI格式_Java_Swing_Jpanel_Jlist - Fatal编程技术网

Java JList GUI格式

Java JList GUI格式,java,swing,jpanel,jlist,Java,Swing,Jpanel,Jlist,我在为正在开发的程序格式化GUI时遇到问题。我需要有四个按钮,上面有一个JList。我的JList一直出现在我所有按钮的旁边,而不是上面。有人能给我指出解决问题的正确方向吗 import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPane

我在为正在开发的程序格式化GUI时遇到问题。我需要有四个按钮,上面有一个JList。我的JList一直出现在我所有按钮的旁边,而不是上面。有人能给我指出解决问题的正确方向吗

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class Test extends JPanel {

    private static final long serialVersionUID = 1L;

    private JList jlist; 

    public static void main(String[] args) {
        JFrame boxOptions = new JFrame("Calculator");
        boxOptions.setSize(0,0);
        boxOptions.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        boxOptions.setResizable(false);
        boxOptions.setLayout(new BorderLayout());
        boxOptions.add(new Test(), BorderLayout.CENTER);
        boxOptions.pack();
        boxOptions.setLocationRelativeTo(null);
        boxOptions.setVisible(true);

    }

    public Test(){  
        setLayout(new GridLayout(1, 4, 5, 5));

        add(new JButton("add"));
        add(new JButton("Check In"));
        add(new JButton("Check Out"));
        add(new JButton("Delete"));


        String[] Titles = {"one", "two", "three"};
        jlist = new JList(Titles);
        jlist.setVisibleRowCount(3);
        add(new JScrollPane(jlist), BorderLayout.NORTH);

    }

}

您正在将Test JPanel类的布局设置为GridLayout,然后尝试使用BorderLayout.NORTH说明符向其添加一个组件——这不是犹太说明符,因为该说明符仅适用于BorderLayout,对使用Test JPanel的GridLayout没有意义

解决方案:嵌套JPanels。让外部JPanel使用BorderLayout,并使用BorderLayout.NORTH说明符将列表的JScrollPane添加到此外部JPanel,然后创建一个使用GridLayout的内部JPanel,该JPanel保存JButton并将其添加到BorderLayout.CENTER位置的主外部JPanel


但最重要的是:请阅读您可以找到的Swing教程,特别是关于使用布局管理器的部分,因为不需要猜测这些内容。

您正在将测试JPanel类的布局设置为GridLayout,然后尝试使用BorderLayout.NORTH说明符向其添加组件——这是不洁的,因为该说明符仅适用于BorderLayout,对于使用Test JPanel的GridLayout没有意义

解决方案:嵌套JPanels。让外部JPanel使用BorderLayout,并使用BorderLayout.NORTH说明符将列表的JScrollPane添加到此外部JPanel,然后创建一个使用GridLayout的内部JPanel,该JPanel保存JButton并将其添加到BorderLayout.CENTER位置的主外部JPanel


但最重要的是:请阅读您可以找到的Swing教程,特别是关于使用布局管理器的部分,因为不需要猜测这些内容。

您正在将测试JPanel类的布局设置为GridLayout,然后尝试使用BorderLayout.NORTH说明符向其添加组件——这是不洁的,因为该说明符仅适用于BorderLayout,对于使用Test JPanel的GridLayout没有意义

解决方案:嵌套JPanels。让外部JPanel使用BorderLayout,并使用BorderLayout.NORTH说明符将列表的JScrollPane添加到此外部JPanel,然后创建一个使用GridLayout的内部JPanel,该JPanel保存JButton并将其添加到BorderLayout.CENTER位置的主外部JPanel


但最重要的是:请阅读您可以找到的Swing教程,特别是关于使用布局管理器的部分,因为不需要猜测这些内容。

您正在将测试JPanel类的布局设置为GridLayout,然后尝试使用BorderLayout.NORTH说明符向其添加组件——这是不洁的,因为该说明符仅适用于BorderLayout,对于使用Test JPanel的GridLayout没有意义

解决方案:嵌套JPanels。让外部JPanel使用BorderLayout,并使用BorderLayout.NORTH说明符将列表的JScrollPane添加到此外部JPanel,然后创建一个使用GridLayout的内部JPanel,该JPanel保存JButton并将其添加到BorderLayout.CENTER位置的主外部JPanel


不过最重要的是:请阅读您可以找到的Swing教程,特别是关于使用布局管理器的部分,因为不需要猜测这些内容。

首先,BorderLayout参数仅在布局为BorderLayout时使用。 您可以改用GridBagLayout。下面是示例代码

    setLayout(new GridBagLayout());

    add(new JButton("add"), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check In"), new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check Out"), new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Delete"), new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));


    String[] Titles = {"one", "two", "three"};
    jlist = new JList(Titles);
    jlist.setVisibleRowCount(3);
    add(new JScrollPane(jlist), new GridBagConstraints(0, 0, 4, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

这是您所期望的吗?

首先,BorderLayout参数仅在布局为BorderLayout时使用。 您可以改用GridBagLayout。下面是示例代码

    setLayout(new GridBagLayout());

    add(new JButton("add"), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check In"), new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check Out"), new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Delete"), new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));


    String[] Titles = {"one", "two", "three"};
    jlist = new JList(Titles);
    jlist.setVisibleRowCount(3);
    add(new JScrollPane(jlist), new GridBagConstraints(0, 0, 4, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

这是您所期望的吗?

首先,BorderLayout参数仅在布局为BorderLayout时使用。 您可以改用GridBagLayout。下面是示例代码

    setLayout(new GridBagLayout());

    add(new JButton("add"), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check In"), new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check Out"), new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Delete"), new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));


    String[] Titles = {"one", "two", "three"};
    jlist = new JList(Titles);
    jlist.setVisibleRowCount(3);
    add(new JScrollPane(jlist), new GridBagConstraints(0, 0, 4, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

这是您所期望的吗?

首先,BorderLayout参数仅在布局为BorderLayout时使用。 您可以改用GridBagLayout。下面是示例代码

    setLayout(new GridBagLayout());

    add(new JButton("add"), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check In"), new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Check Out"), new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    add(new JButton("Delete"), new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));


    String[] Titles = {"one", "two", "three"};
    jlist = new JList(Titles);
    jlist.setVisibleRowCount(3);
    add(new JScrollPane(jlist), new GridBagConstraints(0, 0, 4, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

这是您期望的吗?

您的JList不应该出现在任何地方,因为您正在将它及其JScrollPane添加到p2,而p2是一个从未添加到GUI的JPanel。请向我们展示实际导致您描述的问题的代码。很抱歉,我已经处理了一段时间,并且在遇到问题和发布代码之间做了一些小的更改。代码已被编辑。您的JList不应该出现在任何地方,因为您正在将它及其JScrollPane添加到p2,p2是一个从未添加到GUI的JPanel。请向我们展示实际导致您描述的问题的代码。很抱歉,我已经处理了一段时间,并且在遇到问题和发布代码之间做了一些小的更改。代码已被编辑。您的JList不应该出现在任何地方,因为您正在将它及其JScrollPane添加到p2,p2是一个从未添加到GUI的JPanel。请向我们展示实际导致您描述的问题的代码。很抱歉,我已经处理了一段时间,并且在遇到问题和发布代码之间做了一些小的更改。代码已被编辑。您的JList不应该出现在任何地方,因为您正在将它及其JScrollPane添加到p2,p2是一个从未添加到GUI的JPanel。请向我们展示实际导致您描述的问题的代码。很抱歉,我已经处理了一段时间,并且在遇到问题和发布代码之间做了一些小的更改。代码已被编辑。