如何组合JTextField和JRadioButton?

时间:2011-11-30 17:00:23

标签: java swing jtextfield jradiobutton

我希望JRadioPanel有三个选项。前两个是硬编码选项,我希望第三个是“其他”按钮。我想有一个JTextField而不是文本作为该按钮的标题,但我不知道该怎么做。我试着简单地将该字段作为单选按钮的参数,但它并不那么喜欢。我没有在网上找到任何东西告诉我如何,除了可能通过NetBeans,这对我没有多大帮助。有没有办法轻松做到这一点,还是我必须对布局做一些奇特的事情?

好的,新问题。按钮看起来都很正确,但由于某种原因,它们是连续而不是列。这是它的代码。不知道为什么会这样做。

    tf2 = new JTextField("Other", 20);
    newName.setActionCommand("newname");
    fulfillment.setActionCommand("fulfillment");
    fulfillment.setSelected(true);
    type.add(fulfillment);
    type.add(newName);
    fulfillment.addActionListener(this);
    newName.addActionListener(this);
    GridBagConstraints rC = new GridBagConstraints();
    JPanel radioPanel3 = new JPanel(new GridBagLayout());
    rC.gridwidth = 2;
    radioPanel3.add(fulfillment);
    rC.gridy = 1;
    radioPanel3.add(newName);
    rC.gridy = 2;
    rC.gridwidth = 1;
    radioPanel3.add(other);
    rC.gridx = 1;
    radioPanel3.add(tf2);
    c.gridx = 10;
    c.gridy = 4;
    pane.add(radioPanel3, c);

3 个答案:

答案 0 :(得分:4)

放置第三个无文字按钮而不添加文字,然后添加JTextField。例如。使用GridBagLayout,其中第一个和第二个radiobutton占2列;在第三行,“空”radiobutton的行= 2 col = 0和JTextField row = 2 col = 1

答案 1 :(得分:2)

你可以创建一个可编辑的自定义组件,你可以添加方法setLabelFor(),但为什么要重新发明轮子

  1. 删除JRadioButtons / JCheckBoxes的任何叙述

  2. 在那里添加JTextFields

    • 设置初始大小,例如JTextField(10)用于在JTextField和下一个JRadioButtons / JCheckBoxes之间创建初始间隙

    • setBorder(空)

    • 的setBackground(空)

    • setEditable(假)

    • setOpaque(false)表示半透明效果

  3. 然后所有JCmponents都放到了JPanel,没有理由改变LayoutManager,因为默认情况下JPanel有FlowLayout

  4. enter image description here

    来自代码

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.LineBorder;
    
    public class AddComponentsAtRuntime {
    
        private JFrame f;
        private JPanel panel;
        private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;
        private JTextField checkValidateTex, checkReValidateTex, checkRepaintTex, checkPackTex;
    
        public AddComponentsAtRuntime() {
            JButton b = new JButton();
            b.setBackground(Color.red);
            b.setBorder(new LineBorder(Color.black, 2));
            b.setPreferredSize(new Dimension(600, 10));
            panel = new JPanel(new GridLayout(0, 1));
            panel.add(b);
            f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(panel, "Center");
            f.add(getCheckBoxPanel(), "South");
            f.setLocation(200, 200);
            f.pack();
            f.setVisible(true);
        }
    
        private JPanel getCheckBoxPanel() {
            checkValidateTex = new JTextField(10);
            checkValidateTex.setText("validate");
            //checkValidateTex.setBorder(null);
            //checkValidateTex.setBackground(null);
            //checkValidateTex.setOpaque(false);
            //checkValidateTex.setEditable(false);
            checkValidate = new JCheckBox();
            //checkValidate = new JCheckBox("validate");
            checkValidate.setSelected(false);
            checkReValidateTex = new JTextField("revalidate");
            checkReValidateTex.setBorder(null);
            checkReValidateTex.setBackground(null);
            checkReValidateTex.setOpaque(false);
            checkReValidateTex.setEditable(false);
            checkReValidate = new JCheckBox();
            //checkReValidate = new JCheckBox("revalidate");
            checkReValidate.setSelected(false);
            checkRepaintTex = new JTextField("repaint");
            checkRepaintTex.setBorder(null);
            checkRepaintTex.setBackground(null);
            checkRepaintTex.setOpaque(false);
            checkRepaintTex.setEditable(false);
            checkRepaint = new JCheckBox();
            //checkRepaint = new JCheckBox("repaint");
            checkRepaint.setSelected(false);
            checkPackTex = new JTextField("pack");
            checkPackTex.setBorder(null);
            checkPackTex.setBackground(null);
            checkPackTex.setOpaque(false);
            checkPackTex.setEditable(false);
            checkPack = new JCheckBox();
            //checkPack = new JCheckBox("pack");
            checkPack.setSelected(false);
            JButton addComp = new JButton("Add New One");
            addComp.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JButton b = new JButton();
                    b.setBackground(Color.red);
                    b.setBorder(new LineBorder(Color.black, 2));
                    b.setPreferredSize(new Dimension(600, 10));
                    panel.add(b);
                    makeChange();
                    System.out.println(" Components Count after Adds :" + panel.getComponentCount());
                }
            });
            JButton removeComp = new JButton("Remove One");
            removeComp.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    int count = panel.getComponentCount();
                    if (count > 0) {
                        panel.remove(0);
                    }
                    makeChange();
                    System.out.println(" Components Count after Removes :" + panel.getComponentCount());
                }
            });
            JPanel panel2 = new JPanel();
            panel2.add(checkValidate);
            panel2.add(checkValidateTex);
            panel2.add(checkReValidate);
            panel2.add(checkReValidateTex);
            panel2.add(checkRepaint);
            panel2.add(checkRepaintTex);
            panel2.add(checkPack);
            panel2.add(checkPackTex);
            panel2.add(addComp);
            panel2.add(removeComp);
            return panel2;
        }
    
        private void makeChange() {
            if (checkValidate.isSelected()) {
                panel.validate();
            }
            if (checkReValidate.isSelected()) {
                panel.revalidate();
            }
            if (checkRepaint.isSelected()) {
                panel.repaint();
            }
            if (checkPack.isSelected()) {
                f.pack();
            }
        }
    
        public static void main(String[] args) {
            AddComponentsAtRuntime makingChanges = new AddComponentsAtRuntime();
        }
    }
    

答案 2 :(得分:0)

你必须对布局做更好的事情。没有内置的东西可以摆动......

虽然实施起来不应该那么难!使用netbeans是GUI的一个好主意。您应该考虑将IDE用于项目的GUI部分。它将为您生成代码,因此在最坏的情况下,您应该考虑以实际源代码或生成jar的形式在您喜欢的IDE中使用生成的代码。我做了很多,因为我最喜欢的编辑器是eclipse,我更喜欢有一个wysiwig gui编辑器。

Eclipse还有许多你可以查看的所见即所得的编辑器插件,但我从来没有这样做过。这是一个:http://www.ibm.com/developerworks/opensource/library/os-ecvisual/