手工创建GUI表单 [英] GUI Form Creating By Hand

查看:27
本文介绍了手工创建GUI表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 jframe,其中包含三个文本字段和两个单选按钮(用于选择是或否).

I have a jframe that contains three text fields and two radio button(for choosing of yes or no).

应该是这样的:

但是,我的代码是这样的:

But, with my code, it is this:

我的代码:

public class editFrame extends JFrame {

JButton saveButton;
JButton cancelButton;
JRadioButton radioB1;
JRadioButton radioB2;

public editFrame() {

    JPanel wrapper = new JPanel();
    wrapper.add(createForm());
    add(wrapper, BorderLayout.WEST);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(100, 50, 500, 600);
    this.setVisible(true);
}

public JPanel createForm() {
    String[] labels = {"ID", "Name", "Date"};
    JPanel panel = new JPanel();

    JTextField idtf = new JTextField(10);
    JTextField nametf = new JTextField(10);
    JTextField datetf = new JTextField(10);
    panel.add(idtf);
    panel.add(nametf);
    panel.add(datetf);

    radioB1 = new JRadioButton("Yes");
    radioB2 = new JRadioButton("No");
    ButtonGroup group = new ButtonGroup();
    group.add(radioB1);
    group.add(radioB2);
    panel.add(radioB1);
    panel.add(radioB2);
    saveButton = new JButton("update");
    cancelButton = new JButton("Cancel");
    panel.add(saveButton);
    panel.add(cancelButton);

    SpringLayout sL = new SpringLayout();
    panel.setLayout(sL);
    SpringUtilities.makeCompactGrid(panel, 3, 2, 100, 50, 15, 20);

    return panel;
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            editFrame edF = new editFrame();
        }
    });
}

}借用状态文本不会显示在单选按钮旁边.我的第二个主要问题是,使用此代码,我无法访问文本字段.我稍后需要访问文本字段...

} Borrow Status text don't show beside the radio Buttons. My second main problem is that with this code, i can not access text fields. I need access text fields later...

推荐答案

首先,收音机的标签没有显示,因为您没有创建它并将其添加到面板.创建它并将其添加到 radioB1 之前的面板中.此外,您应该在 radioB2 之前添加一些不可见(empy)标签(或使用其他填充组件来填充单元格 - 也许这会有所帮助:http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#filler).

First, the label for radios is not shown, because you don't create it and add it to panel. Create it and add it to panel before radioB1. Also, you should add some invisible (empy) label before radioB2 (or use some other filler component to fill the cell - perhaps this could help: http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#filler).

此外,您可以向面板添加另一个填充组件而不是 saveButton.然后我将创建另一个面板 (buttonPane),它包含保存和取消按钮,并将这个 buttonPane 添加到主面板而不是取消按钮.按钮面板可以使用 BoxLayout,按钮之间可以使用水平胶水(如您在上面的链接中所见).

Also, you could add another filler component to panel instead of saveButton. Then I would create another panel (buttonPane), that would contain both save and cancel buttons and add this buttonPane to the main panel instead of cancelButton. The panel for buttons could use BoxLayout and in between buttons could be horizontal glue (as you can see in the link above).

应该是这样.但这里有一个警告.您正在以这种方式在循环中创建 JTextFields,以后无法引用它们,即.何时需要获取文本.您应该将它们的引用保留为单独的变量,或者将它们添加到数组中,以便在按下更新按钮时获取值.

That should be it. But there's one warning here. You are creating JTextFields in the loop in such way, that you cannot refer to them later, ie. when you will need to get the text. You should keep their references as individual variables, or add them to an array, so you can get the values, when the update button is pressed.

这篇关于手工创建GUI表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆