繁体   English   中英

摇摆:jlabel + jcombox + jtextfield

[英]Swing: jlabel+jcombox+jtextfield

我需要在jtextfield旁边的jcombobox旁边构建一个jlabel。 JTextfield必须仅接受数字。 来自jtextfield的文本应存储在字符串中,所选元素也应存储在不同的字符串中。 如果我可以添加jbutton以便单击按钮时解析所有选择,那也是理想的选择。 我目前正在使用此未完成的代码,但无法正常工作。 有人可以建议所需的补充吗? 提前致谢

   public class constraints {

            private static JTextField tField;
            private MyDocumentFilter documentFilter;
            private JLabel amountLabel;
            private static String amountString = "Select Quantity (in ktones): ";
            public static String str = "" ;

            private void displayGUI()
            {
                JFrame frame = new JFrame("Constraints");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                amountLabel = new JLabel(amountString);

                JPanel contentPane = new JPanel();
                contentPane.setBorder(
                    BorderFactory.createEmptyBorder(5, 5, 5, 5));
                tField = new JTextField(10);

                amountLabel.setLabelFor(tField);

                String[] petStrings = { "Less", "Equal", "More"};
                JComboBox petList = new JComboBox(petStrings) ;
                        petList.setSelectedIndex(3);
                    petList.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event) {

                             JComboBox cb = (JComboBox)event.getSource();
                             String petName = (String)cb.getSelectedItem();

                            System.out.println("petName");
                        }
                    });                         

                ((AbstractDocument)tField.getDocument()).setDocumentFilter(
                        new MyDocumentFilter());        
                contentPane.add(amountLabel);
                  contentPane.add(petList);
                contentPane.add(tField); 


                frame.setContentPane(contentPane);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }

            public static void main(String[] args)
            {
                Runnable runnable = new Runnable()
                {
                    @Override
                    public void run()
                    {
                        new constraints().displayGUI();
                    }
                };
                EventQueue.invokeLater(runnable);

            }
        }

        class MyDocumentFilter extends DocumentFilter
        {   
            @Override
            public void insertString(DocumentFilter.FilterBypass fp
                    , int offset, String string, AttributeSet aset)
                                        throws BadLocationException
            {
                int len = string.length();
                boolean isValidInteger = true;

                for (int i = 0; i < len; i++)
                {
                    if (!Character.isDigit(string.charAt(i)))
                    {
                        isValidInteger = false;
                        break;
                    }
                }
                if (isValidInteger)
                    super.insertString(fp, offset, string, aset);
                else
                    Toolkit.getDefaultToolkit().beep();
            }

            @Override
            public void replace(DocumentFilter.FilterBypass fp, int offset
                            , int length, String string, AttributeSet aset)
                                                throws BadLocationException
            {
                int len = string.length();
                boolean isValidInteger = true;

                for (int i = 0; i < len; i++)
                {
                    if (!Character.isDigit(string.charAt(i)))
                    {
                        isValidInteger = false;
                        break;
                    }
                }
                if (isValidInteger)
                    super.replace(fp, offset, length, string, aset);
                else
                    Toolkit.getDefaultToolkit().beep();
            }
        }

几件事情:

String[] petStrings = { "Less", "Equal", "More"};
JComboBox petList = new JComboBox(petStrings);
petList.setSelectedIndex(3);

这将抛出java.lang.IllegalArgumentException: setSelectedIndex: 3 out of bounds因为您的JComboBox只有3个项目(索引从0开始)。

String petName = (String)cb.getSelectedItem();
System.out.println("petName");

这将始终打印“ petName”字符串。 我想你要:

System.out.println(petName);

无论如何,我认为您将要使用ItemListener代替ActionListener

petList.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent event) {
        JComboBox cb = (JComboBox)event.getSource();
        String petName = (String) cb.getSelectedItem();
        System.out.println(petName);
    }
});

更新

我只需要在单击或输入jbutton时执行此操作。 那可能吗?

是的。 只需将JComboBox final,然后将JButton添加到JPanel

final JComboBox petList = new JComboBox(petStrings);
petList.setSelectedIndex(2);

JButton submit = new JButton("Submit");
submit.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String petName = (String) petList.getSelectedItem();
        System.out.println(petName);
    }
});

PS :请注意,如果您不将petList final,则将无法在JButton的ActionListener中对其进行访问

暂无
暂无

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

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