Java 在 GUI 上定位列表

发布于 2024-08-31 07:30:42 字数 1929 浏览 2 评论 0原文

我正在尝试将 JList 添加到 GUI,但想知道如何定位它?我希望它出现在 TextArea 的右侧,以显示将发送到 GUI 进行选择的数据。

谁能建议如何做到这一点?这是代码(注意:对于 Java 和 GUI 来说非常陌生)

protected static void createAndShowGUI() {
        GUI predict = new GUI();
        JFrame frame = new JFrame("Phone V1.0");

        frame.setContentPane(predict.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setMinimumSize(new Dimension(300, 400));
        frame.setVisible(true); // Otherwise invisible window
}

private JPanel createContentPane() {
    JPanel pane = new JPanel();

    TextArea = new JTextArea(5, 10);
    TextArea.setEditable(false);
    TextArea.setLineWrap(true);
    TextArea.setWrapStyleWord(true);
    TextArea.setWrapStyleWord(true);
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); 
    //Adds the buttons from Top     to Bottom

    String[] items = {"dsfsdfd"};
    list = new JList(items);
    JScrollPane scrollingList = new JScrollPane(list);
    int orient = list.getLayoutOrientation();

    JPanel window = new JPanel();
    pane.add(window);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(5, 3));

    JButton[] buttons = new JButton[] {
        new JButton("Yes"),
        new JButton(""),
        new JButton("Clr"),
        new JButton("1"),
        new JButton("2abc"),
        new JButton("3def"),
        new JButton("4ghi"),
        new JButton("5jkl"),
        new JButton("6mno"),
        new JButton("7pqrs"),
        new JButton("8tuv"),
        new JButton("9wxyz"),
        new JButton("*+"),
        new JButton("0_"),
        new JButton("^#")
    };  // Array Initialiser

    for (int i = 0; i < buttons.length; i++) {
        buttonPanel.add(buttons[i]);
        buttons[i].addActionListener(this);
    }
    pane.add(TextArea);
    pane.add(list);
    pane.add(buttonPanel);


    return pane;
}

I'm trying to add a JList to a GUI, but am wondering how to position it? I want it to appear on the right hand side of the TextArea for data that will be sent to the GUI for selection.

Can anyone suggest how to do this? Here is the code (note: very new to Java and GUI's)

protected static void createAndShowGUI() {
        GUI predict = new GUI();
        JFrame frame = new JFrame("Phone V1.0");

        frame.setContentPane(predict.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setMinimumSize(new Dimension(300, 400));
        frame.setVisible(true); // Otherwise invisible window
}

private JPanel createContentPane() {
    JPanel pane = new JPanel();

    TextArea = new JTextArea(5, 10);
    TextArea.setEditable(false);
    TextArea.setLineWrap(true);
    TextArea.setWrapStyleWord(true);
    TextArea.setWrapStyleWord(true);
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); 
    //Adds the buttons from Top     to Bottom

    String[] items = {"dsfsdfd"};
    list = new JList(items);
    JScrollPane scrollingList = new JScrollPane(list);
    int orient = list.getLayoutOrientation();

    JPanel window = new JPanel();
    pane.add(window);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(5, 3));

    JButton[] buttons = new JButton[] {
        new JButton("Yes"),
        new JButton(""),
        new JButton("Clr"),
        new JButton("1"),
        new JButton("2abc"),
        new JButton("3def"),
        new JButton("4ghi"),
        new JButton("5jkl"),
        new JButton("6mno"),
        new JButton("7pqrs"),
        new JButton("8tuv"),
        new JButton("9wxyz"),
        new JButton("*+"),
        new JButton("0_"),
        new JButton("^#")
    };  // Array Initialiser

    for (int i = 0; i < buttons.length; i++) {
        buttonPanel.add(buttons[i]);
        buttons[i].addActionListener(this);
    }
    pane.add(TextArea);
    pane.add(list);
    pane.add(buttonPanel);


    return pane;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

叹沉浮 2024-09-07 07:30:42

阅读 Swing 教程中关于使用布局管理器的部分。不需要只使用单个布局管理器。您可以嵌套布局管理器以获得所需的效果。

Read the section from the Swing tutorial on Using Layout Mananger. There is no need to only use a single layout manager. You can nest layout managers to get the desired effect.

话少情深 2024-09-07 07:30:42

使用 BorderLayout 管理器。基本上,BorderLayout 管理器允许您使用北、南、东、西和中心坐标来排列组件。中心的组件占用所有可用空间,因为父容器有更多可用空间。

private JPanel createContentPane() {
    JPanel pane = new JPanel();  //this is your main panel
    JPanel textAreaPanel = new JPanel(new BorderLayout()); //the wrapper

    //Some more code...

    //Then at the end
    //Make your TextArea take the center
    textAreaPanel.add(TextArea, BorderLayout.CENTER);
    //And the list to the east
    textAreaPanel.add(list, BorderLayout.EAST);
    pane.add(textAreaPanel);
    pane.add(buttonPanel);

    return pane;
}

最酷的事情是您可以将面板嵌套在其他面板中,为它们添加不同的布局管理器以获得您想要的布局。

在不相关的注释中,请尝试遵循 Java 命名约定。使用 JTextArea textArea 代替 JTextArea TextArea。它使您和阅读您的代码的人更容易理解它。

Wrap your TextArea and list in a new panel with a BorderLayout manager. Basically the BorderLayout manager lets you arrange components using north, south, east, west and center coordinates. The components at the center takes all available space as the parent container has more space available to it.

private JPanel createContentPane() {
    JPanel pane = new JPanel();  //this is your main panel
    JPanel textAreaPanel = new JPanel(new BorderLayout()); //the wrapper

    //Some more code...

    //Then at the end
    //Make your TextArea take the center
    textAreaPanel.add(TextArea, BorderLayout.CENTER);
    //And the list to the east
    textAreaPanel.add(list, BorderLayout.EAST);
    pane.add(textAreaPanel);
    pane.add(buttonPanel);

    return pane;
}

The cool thing is that you can nest panels inside other panels, adding them different layout managers to get your desired layout.

On an unrelated note, try to follow Java naming conventions. Instead of JTextArea TextArea use JTextArea textArea. It makes it easier for you and people reading your code to understand it.

溇涏 2024-09-07 07:30:42

您可以使用诸如 Mig Layout 之类的布局管理器来进行此类定位。

替代文本
(来源:miglayout.com

You could use a layout manager like Mig Layout for that kind of positionning.

alt text
(source: miglayout.com)

虐人心 2024-09-07 07:30:42

我可以推荐您FormLayout。在找到这个布局之前,我对 GridBagLayout 感到非常痛苦。 FormLayout功能更强大,学习和使用更方便,而且免费。给它一个机会。

I could recommend you FormLayout. Before I found this layout I had a real pain with GridBagLayout. FormLayout is more powerful and much more convenient to learn and use and it is free. Give it a chance.

坚持沉默 2024-09-07 07:30:42

正如其他人所建议的,熟悉布局管理器的概念。有几个附带标准 Swing API 和几个优秀的第 3 方 API。

此外,您还需要将JList 添加到滚动窗格(JScrollPane)。您可能需要考虑将其添加到拆分窗格 (JSplitPane)。 考虑我的意思并不是“因为网上有人这么说而这样做”,我的意思是“如果这对您的最终用户有意义,就这样做”。

As others suggested, familiarize yourself with the concept of layout managers. There are several that come with the standard Swing API and several good 3rd party ones out there.

In addition, you will want to add the JList to a scroll pane (JScrollPane). You may want to consider adding it to a split pane (JSplitPane). And by consider I don't mean "do it because some guy on the net said so" I mean "do it if it makes sense for your end users".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文