简体   繁体   English

如何使用Swing创建简单的Java表单?

[英]How to create a simple Java form with Swing?

I'm very new to swing and have trouble dealing with JFrame s so I thought you guys could help. 我是新手,在处理JFrame遇到麻烦,所以我认为你们可以帮忙。

I'm trying to make a simple window show up with 3 text fields and a check box. 我试图使一个简单的窗口显示3个文本字段和一个复选框。 At the bottom there should be a "done" button which closes the form. 在底部应该有一个“完成”按钮,用于关闭表单。

I can make the JFrame with the JTextField s and JCheckBox but how do I retrieve the input? 我可以使用JTextFieldJCheckBox制作JFrame ,但是如何检索输入呢?

Thanks in advance 提前致谢

As stated in the Swing tutorial , you can add an ActionListener to the JButton , which will be called when the button has been pressed. Swing教程所述 ,您可以将ActionListener添加到JButton ,当按下按钮时将调用它。

To retrieve the text from the JTextField , use the JTextField#getText() method 要从JTextField检索文本,请使用JTextField#getText()方法

To determine whether the JCheckBox is actually selected, use the JCheckBox#isSelected() method 为了确定是否JCheckBox实际选择,使用JCheckBox#isSelected()方法

But a good starting point is reading the Swing tutorial from the start 但是一个好的起点是从头开始阅读Swing教程

It would be helpful if you would post some code showing how you show your JFrame , then I could give you a more specific example. 如果您发布一些代码来展示如何显示JFrame ,这将很有帮助,那么我可以给您一个更具体的示例。

In general, you'll have a class that extends JFrame , JDialog , etc. In that class you'll have getters and setters that will get and set the values of the controls on the form. 通常,您将拥有一个扩展JFrameJDialog等的类。在该类中,您将具有将获取和设置表单上控件值的getter和setter。

In your case, once "Done" is clicked you could have a listener, either on the "Done" button or on the frame itself (listening for the closing event) retrieve the values from your form and do something with them. 对于您的情况,单击“完成”后,您可以在“完成”按钮上或框架本身(侦听关闭事件)上都有一个侦听器,以从表单中检索值并对它们进行处理。

If this isn't clear please post some code and perhaps I can give you a concrete example. 如果不清楚,请发布一些代码,也许我可以举一个具体的例子。

public class MyFrame extends JFrame {

    private JTextField textField = new JTextField();
    private JButton doneBtn = new JButton("Done");

    // rest of your form
}

If you want to get the contents of textField when the doneBtn is pressed, you need to attach an event listener to the button: 如果要在按下doneBtn时获取textField的内容,则需要在按钮上附加一个事件侦听器:

public class MyFrame extends JFrame {

    private JTextField textField = new JTextField();
    private JButton doneBtn = new JButton("Done");

    public MyFrame() {
        // Here we attach an event listener to the button.
        // Whenever you press the button, the code inside actionPerformed will be executed
        doneBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                System.out.println(textField.getText()); // Or do whatever you like
            }
        });
    }

    // rest of your form
}

But, honestly, you should learn this yourself. 但是,老实说,您应该自己学习。 If you haven't read a tutorial about check boxes, of course you won't know how they work. 如果您还没有阅读有关复选框的教程,那么您当然不会知道它们是如何工作的。 Read first, and if you had a question, then ask for help. 请先阅读,如果有问题,请寻求帮助。 You haven't read enough and yet you are asking questions. 您尚未阅读足够的内容,但仍在提出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM