Java Swing:jlabel+;jcombox&x2B;jtextfield

Java Swing:jlabel+;jcombox&x2B;jtextfield,java,swing,jlabel,jtextfield,jcombobox,Java,Swing,Jlabel,Jtextfield,Jcombobox,我需要在jtextfield旁边的jcombobox旁边构建一个jlabel。JTextfield必须只接受数字。jtextfield中的文本应存储在字符串中,所选元素也应存储在不同的字符串中。另外,如果我可以添加一个jbutton,以便在单击按钮时解析所有选择,那将是理想的。我目前正在使用这个未完成的代码,但它不会工作。有人能提出所需的补充吗?提前谢谢 public class constraints { private static JTextField tF

我需要在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();
            }
        }
公共类约束{
私有静态JTextField;
私有MyDocumentFilter文档过滤器;
私人JLabel amountLabel;
私有静态字符串amountString=“选择数量(单位:千吨):”;
公共静态字符串str=“”;
私有void displayGUI()
{
JFrame=新JFrame(“约束”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
amountLabel=新的JLabel(amountString);
JPanel contentPane=新的JPanel();
contentPane.setOrder(
createEmptyByOrder(5,5,5,5));
tField=新的JTextField(10);
amountLabel.setLabelFor(tField);
字符串[]PetString={“更少”、“相等”、“更多”};
JComboBox-petList=新的JComboBox(petStrings);
petList.setSelectedIndex(3);
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件){
JComboBox cb=(JComboBox)event.getSource();
字符串petName=(字符串)cb.getSelectedItem();
System.out.println(“petName”);
}
});                         
((AbstractDocument)tField.getDocument()).setDocumentFilter(
新建MyDocumentFilter());
contentPane.add(amountLabel);
contentPane.add(petList);
contentPane.add(tField);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args)
{
Runnable Runnable=新的Runnable()
{
@凌驾
公开募捐
{
新约束().displayGUI();
}
};
invokeLater(可运行);
}
}
类MyDocumentFilter扩展了DocumentFilter
{   
@凌驾
public void insertString(DocumentFilter.FilterBypass fp
,整数偏移量,字符串,属性集(aset)
抛出BadLocationException
{
int len=string.length();
布尔值isValidInteger=true;
对于(int i=0;i
两件事:

String[]petStrings={“更少”、“相等”、“更多”}
JComboBox-petList=新的JComboBox(petStrings)
petList.setSelectedIndex(3)

这将抛出
java.lang.IllegalArgumentException:setSelectedIndex:3超出范围
,因为您的
JComboBox
只有3项(索引从0开始)

String petName=(String)cb.getSelectedItem()
System.out.println(“petName”)

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

System.out.println(petName);
无论如何,我认为您应该使用
itemstener
而不是
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或enter时执行此操作。是吗 可能吗

是的。只需将您的
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);
    }
});
注:注意,如果您不将petList设置为final,则无法在JButton的ActionListener中访问它

String[]petStrings={“更少”、“相等”、“更多”}
JComboBox-petList=新的JComboBox(petStrings)
petList.setSelectedIndex(3)

这将抛出
java.lang.IllegalArgumentException:setSelectedIndex:3超出范围
,因为您的
JComboBox
只有3项(索引从0开始)

String petName=(String)cb.getSelectedItem()
System.out.println(“petName”)

<