【问题标题】:Need some help with GUI in java [closed]java中的GUI需要一些帮助[关闭]
【发布时间】:2011-02-24 12:19:57
【问题描述】:

我正在使用 java 中的 GUI 并被困在移动对象上。

请访问这个 youtube 视频,我为你们制作了一个简短的演示,看看我想要做什么。我对 GUI 很陌生,因为从来没有人教过我做 GUI。

这里是链接:http://www.youtube.com/watch?v=up1LV5r-NSg

【问题讨论】:

  • +1 用于视频和说明。好点子!希望更多的人会做这样的事情。
  • 最后一句话:“我现在只需要你”让我笑了。 :)
  • @Bart K:lolx 没有意识到这一点,只是觉得这是一首好歌!虽然装了视频不是吗?我现在需要你帮忙!大声笑
  • @james1,你是对的,这首歌非常适合你的“视频问题”!我喜欢它。 :)

标签: java swing gui-designer


【解决方案1】:

我看到您正在使用 GUI 设计器。我强烈建议“手动”构建您的 GUI,而不是在这种情况下,您的代码在 IMO 上更清晰(我并不是说所有 GUI 设计人员都会产生糟糕的代码,但它几乎总是更难阅读,如果不使用它,编辑它会很困难完全相同的 GUI 设计器)。一旦您对手工 GUI 设计感到满意,然后尝试 GUI 设计器,看看是什么让您更舒服。

见:http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

在您的情况下,您可以创建一个BorderLayout,并且在面板/框架的“南”,您可以放置​​一个带有FlowLayout 的面板,将其组件向左对齐。然后使用 FlowLayout 将您的按钮添加到面板中。

一个小演示:

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

public class LayoutDemo extends JFrame {

    LayoutDemo() {
        super("LayoutDemo");
        super.setSize(400, 200);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createGUI();
        super.setVisible(true);
    }

    private void createGUI() {
        // set the layout of this frame
        super.setLayout(new BorderLayout());

        // create a panel to put the button on
        final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        // create a text area to put in the center
        final JTextArea textArea = new JTextArea();

        // create the search button
        final JButton searchButton = new JButton("search");

        // add a listener to the button that add some text to the text area
        searchButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText(textArea.getText() + "pressed search on " + (new Date()) + "\n");
            }
        });

        // add the button to the bottom panel
        bottomPanel.add(searchButton);

        // wrap a scroll-pane around the text area and place it on the center of this frame
        super.add(new JScrollPane(textArea), BorderLayout.CENTER);

        // put the bottom panel (containing the button) on the 'south' of this frame
        super.add(bottomPanel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new LayoutDemo();
            }
        });
    }
}

产生:

alt text http://img689.imageshack.us/img689/5874/guiq.png


编辑

要将按钮向上移动一点,请使用构造函数new FlowLayout(FlowLayout.LEFT, int hgap, int vgap)

其中hgap 是左右组件之间的间隙(以像素为单位),vgap 是上下组件之间的间隙(以像素为单位)。

试试:

final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 10));

请注意,按钮和文本区域之间的空间也略有增加!

【讨论】:

  • 赞成 BorderLayout 建议。这是我通过反复试验找到的解决方案。 stackoverflow.com/questions/2889300/…
  • 那么,如果你想将按钮向上移动一点(只是一点点),你会改变什么?
  • 根据我的经验,手写代码中几乎总是存在错误。没有什么能比得上 NetBeans Swing GUI 构建器(对于 Java 桌面)...了解 NetBeans 的“思考方式”并不费力,一旦实现,您将永远不会收到损坏的代码...
  • @Xorty,我没有说手写代码没有错误。但是在不知道如何“手动”构建(简单)GUI的情况下使用GUI设计器并不是IMO的必经之路。此外,使用 GUI 设计器的一个缺点是其他人无法在没有该 GUI 设计器的情况下在 IDE/编辑器中简单地编辑 GUI。
  • @Bart K. - 听起来更好,我取消了我的否决票(编辑:不是真的,stackoverflow 不会让我自动取款:)。至于学习——好吧,至于严肃的事情——不,从来没有。节省您的时间(用于编写和调试)并让团队知道如何使用 IDE GUI 设计器...
【解决方案2】:

学习 fest swing test 和 miglayout!巨星摇摆测试使您能够运行您的 gui 脚本。 和Miglayout,这是我的看法,也是易于使用的布局库。

Fest: http://fest.easytesting.org/swing/wiki/pmwiki.php
MigLayout: http://www.miglayout.com/

【讨论】:

  • 看起来不错,会努力学习的。非常感谢
  • 我用它们作为我的毕业设计。 GUI开发的主要问题是布局问题。但是 mig 可以让您在没有任何可视化编辑器的情况下做出好的设计。
【解决方案3】:

如果您不想学习 Java Swing 的细节,也不想创建一些花哨的 GUI,那么像您正在使用的 GUI 设计器应该没问题。

您无法做的似乎是您的特定 IDE 的限制,因此尝试Netbeans 可能会有所帮助。您始终可以获取生成的 GUI 代码(尽管它可能很丑),然后将其重新插入到您的其他 IDE 中的项目中。

【讨论】:

  • 是的,我知道 netbeans 会这样做,但我想在 java 中学习它并理解代码,这就是为什么我使用那个 Jframe 插件来查看它的实际编写方式。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 2012-02-02
相关资源
最近更新 更多