【问题标题】:Code Executing in different orders, Jframe, Button and TextArea以不同顺序执行代码,Jframe、Button 和 TextArea
【发布时间】:2014-02-04 13:23:22
【问题描述】:

基本上我有一些代码来创建一个允许我提交请求并从 txt 文件中提取必要信息的界面。出于某种原因,当我为代码执行 StartUp 时,有时按钮不存在,一个文本框在屏幕上占主导地位,所有文本框都重叠......这很奇怪。

GUI 代码如下

public class Menu {

SubmitCode submit = new SubmitCode();

    public static JFrame frame;
    public static JTextField field;
    public static Button btn;
    public static TextArea txtComm;
    public static TextArea txtSites;
    public static TextArea txtProg;
    public static Dimension dim = new Dimension(40, 10);

    public Menu() {
        frame = new JFrame();

        frame.setTitle("Welcome :)");
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

    }

    public static void open()   {
        Menu.main(null);    // Opens up the main method of the class
    }

    public static void main(String args[])  {
        field = new JTextField();
        btn = new Button();
        txtComm = new TextArea();
        txtSites = new TextArea();
        txtProg = new TextArea();

        field.setText("What do you want to do?");
        field.setSize(390, 20);
        field.setLocation(0, 125);

        btn.setVisible(true);
        btn.setLabel("Click to Submit");
        btn.setSize(90, 20);
        btn.setLocation(400, 125);

        txtComm.setVisible(true);
        txtComm.setText("Commands: ");
        txtComm.setSize(150, 100);
        txtComm.setLocation(10, 10);
        txtComm.setEditable(false);
        frame.add(txtComm);

        txtSites.setVisible(true);
        txtSites.setText("Sites: ");
        txtSites.setSize(150, 100);
        txtSites.setLocation(170, 10);
        txtSites.setEditable(false);
        frame.add(txtSites);

        txtProg.setVisible(true);
        txtProg.setText("Programmes: ");
        txtProg.setSize(150, 100);
        txtProg.setLocation(330, 10);
        txtProg.setEditable(false);
        frame.add(txtProg);

        frame.setSize(500, 175);
        frame.add(field, BorderLayout.SOUTH);
        frame.add(btn);

        btn.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Do Something Clicked");

                SubmitCode.main(null);
            }
        });
    }
}

【问题讨论】:

    标签: java swing jframe awt jtextfield


    【解决方案1】:

    建议:

    • 请勿使用静态方法/字段,除非出现特定需求或主要方法。你在这里不需要。
    • 改为使用有效的类、带有构造函数的类、实例(非静态)字段和实例方法。
    • 不要不必要地混合使用 AWT 和 Swing 组件,而应仅使用 Swing 组件。所以JTextArea,不是TextArea,JButton,不是Button,等等......
    • 例如,您的 Menu 构造函数是浪费的代码,由于您对静态的滥用和过度使用而从未调用过。
    • 不要设置大小,使用 null 布局和绝对定位,例如 setBounds。
    • 改为阅读并使用布局管理器。
    • 不要在代码中添加无用的位,例如您对setVisible(true) 的大部分调用。
    • 在顶层窗口调用setVisible(true),这里是你的JFrame,添加所有组件之后。
    • 请阅读相关教程,因为那里对此进行了很好的解释。谷歌 Java Swing Tutorials 并检查第一个命中。
    • 这有点吓到我了:SubmitCode.main(null); 并建议您尝试从 GUI 中调用另一个类的静态 main 方法。您应该避免这样做,而是让您的 SubmitCode 类使用良好的 OOP 技术,包括非静态方法和字段、构造函数等...

    【讨论】:

    • 谢谢,我是新手,这些都是非常有用的技巧,我认为我需要将我的一些变量实例化为静态,我认为 setVisible 必须应用于所有组件,很多赞赏
    • @user3200016:请改用this link
    猜你喜欢
    • 2018-10-02
    • 2020-12-25
    • 2020-03-09
    • 2012-04-21
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多