0

在这个 actionlistener 中,我创建了 9 个名为“a”到“i”的 JTextField。但是当我运行程序时,文本字段“d”和“g”丢失并且 JLabel“投注值”放置不正确。

我是新手,我可能错过了一些东西,所以我希望你们中的一些人能看到哪里出了问题。

所以我要问的是:为什么提到的文本字段没有显示,为什么提到的标签放置不正确。

下面是 actionlistener 的代码:

public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            JFrame frame = new JFrame("Value Bet");
            frame.setVisible(true);
            frame.setSize(500,300);
            GridBagLayout layout = new GridBagLayout();
            frame.setLayout(layout);
            GridBagConstraints c = new GridBagConstraints();

            JLabel label;
            JTextField tf;

            if (shouldFill) {
            //natural height, maximum width
            c.fill = GridBagConstraints.HORIZONTAL;
            }
            if (shouldWeightX) {
            c.weightx = 0.5;
            }

            label = new JLabel("Kamp:");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            frame.add(label, c);

            /*tf = new JTextField("Hvilken kamp?");
            c.fill = GridBagConstraints.HORIZONTAL;
            //c.gridwidth = 3;
            c.gridx = 1;
            c.gridy = 0;
            frame.add(tf, c);*/

            label = new JLabel("1");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 1;
            frame.add(label, c);

            label = new JLabel("X");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 1;
            frame.add(label, c);

            label = new JLabel("2");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 3;
            c.gridy = 1;
            frame.add(label, c);

            label = new JLabel("Chance");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 2;
            frame.add(label, c);

            label = new JLabel("Odds");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 3;
            frame.add(label, c);

            label = new JLabel("Betvalue");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 4;
            frame.add(label, c);

            tf = new JTextField(" a ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 2;
            frame.add(tf, c);

            tf = new JTextField(" b ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 3;
            frame.add(tf, c);

            tf = new JTextField(" c ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 4;
            frame.add(tf, c);

            tf = new JTextField(" d ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 2;
            frame.add(label, c);

            tf = new JTextField(" e ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 3;
            frame.add(tf, c);

            tf = new JTextField(" f ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 4;
            frame.add(tf, c);

            tf = new JTextField(" g ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 3;
            c.gridy = 2;
            frame.add(label, c);

            tf = new JTextField(" h ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 3;
            c.gridy = 3;
            frame.add(tf, c);

            tf = new JTextField(" i ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 3;
            c.gridy = 4;
            frame.add(tf, c);

            JButton button = new JButton("Udregn");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 4;
            c.gridy = 3;
            frame.add(button, c);
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    System.out.println("You clicked the 'Halvgarderings Bet' button");
                }
            });

            JButton b1 = new JButton("SPIL!");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 4;
            c.gridy = 5;
            frame.add(b1, c);
            b1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    System.out.println("You clicked the 'Halvgarderings Bet' button");
                }
            });

            JCheckBox cb1 = new JCheckBox();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 5;
            frame.add(cb1, c);

            JCheckBox cb2 = new JCheckBox();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 5;
            frame.add(cb2, c);

            JCheckBox cb3 = new JCheckBox();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 3;
            c.gridy = 5;
            frame.add(cb3, c);


        }
    });
4

1 回答 1

1
tf = new JTextField(" d ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 2;
frame.add(label, c);

label应该是tf

于 2012-05-31T18:13:50.707 回答