2

我正在尝试使用 Window Builder 用 Ja​​va 编写我的第一个 GUI 类。好的,GUI 已经准备好,但还没有任何功能(它包含 10 个复选框(是的,它们都是必需的)和 2 个按钮和一个 JTextField,所以没什么特别的)。所以我只是将复选框和按钮拖放到窗口中,我还没有编写任何代码。

我有两个类: GUI.java -> 仅用于布局 Main.java -> 应该从 GUI.java 获取“输入”

现在用户应该选中或取消选中复选框,然后最后按下按钮。

但是,如果复选框被选中,我如何“读出” - 我有两个班级?

我必须补充一点,我是 Java 的初学者,尤其是 GUI 编程的初学者,但我只想学习它。我很乐意收到一些帮助,但不是完整的代码。

4

6 回答 6

3

基本上,您从 Main.java 实例化您的 GUI 对象。然后您可以访问此 GUI(假设您有 setter/getter-methods 或 GUI 中的组件是公共的)。在 GUI 构造器中,您调用所有构建器方法,因此当您new GUI()从 Main.java 调用时,您将 a) GUI 运行,b) 从 Main.java 访问它

对于您对复选框的具体问题,您可以致电

nameOfCheckbox.isSelected()

无论复选框是否被选中,它都会返回一个布尔值。

反之亦然:由于您的 Main.java 具有(或应该具有)静态方法(如 main-method),因此您可以简单地Main.anyMethodName()从 GUI.java 调用(假设 this anyMethod 是static)并将数据从“可视区域”传递到“逻辑区域”(建议尽可能将这两个组件分开)。

于 2012-06-19T16:05:13.793 回答
1

我知道你说你不想要完整的代码,但这不是真的,只是你想做的一个非常基本的工作演示

在此处输入图像描述

桂.java

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Gui {
JFrame frame;
JButton button;
JCheckBox checkBox;
JLabel label;

public Gui() {
    frame = new JFrame("demo");
    button = new JButton("is it checked?");
    checkBox = new JCheckBox();
    label = new JLabel("no");
    JPanel panel = new JPanel();
    panel.add(checkBox);
    panel.add(button);
    panel.add(label);
    frame.add(panel);
    frame.pack();
    //frame.setSize(200, 60);
    frame.setResizable(false);
    frame.setLocation(400, 400);
    frame.setVisible(true);
}

// method to add an action listener to the gui's
// private button
public void setButtonActionListener(ActionListener al) {
    button.addActionListener(al);
}

// gui method to check if box is checked
public boolean isBoxChecked() {
    return checkBox.isSelected();
}

// method to set lable
public void setText(String text) {
    label.setText(text);
}

}

主.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Main {

public static void main(String[] args) {

    // create an instance of your gui class
    final Gui gui = new Gui();

    // add the action listener to the button
            // notice how we reference the gui here by running the methods in the
            // gui class
            // this action listener could be created in the gui
            // class but in general you don't want to do that because actions will 
            // involve multiple classes
    gui.setButtonActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            gui.setText((gui.isBoxChecked() ? "yes" : "no"));
        }
    });
}

}

于 2012-06-19T16:11:45.253 回答
0

假设您的复选框是一个名为“myCheckBox”的对象,您可以使用以下方法检查它是否被选中:

myCheckBox.isSelected()

如果复选框被选中,则返回 true。

我建议您查看有关如何使用各种 GUI 组件的 Java 教程,例如: http ://docs.oracle.com/javase/tutorial/uiswing/components/button.html

于 2012-06-19T16:10:07.127 回答
0

附上一个ItemListener.

您可以使用以下行来确定该框是上升(未选中到选中)还是下降(选中到未选中)itemStateChanged(ItemEvent e)

boolean selected = e.getStateChange() == ItemEvent.SELECTED;

要确定是否在不更改时检查它,只需使用isSelected()

于 2012-06-19T16:06:09.323 回答
0

要么这样:

checkbox.isSelected();

或者这将是 Itemlistener 的一种方式,如果有变化,它将被调用:

checkbox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.DESELECTED) {
                foo = false;

            } else {
                foo = true;
            }
        }
    });
于 2012-06-19T16:06:14.753 回答
0

You could expose all of your gui objects as fields in your gui class

public JTextField getNameTextField() {
    return nameTextField;
}

public JCheckBox getCheckBox1() {
    return checkBox1;
}

and then in main:

if (gui.getCheckBox1().isSelected())
    // do stuff
于 2012-06-19T16:07:07.763 回答