摆动组件的麻烦

时间:2015-03-16 19:04:22

标签: java swing jframe awt itemlistener

我在Java中遇到了一堆错误,我不知道从哪里开始。我是GUI编程的新手,非常感谢您提供的任何帮助,甚至只是在正确的方向上轻推。

这是我目前的代码:

// DebugFourteen2
// Displays list of payment options
// - credit card, check or cash
// Displays fee for using each - 5%, 2% or 0%
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DebugFourteen2 extends JFrame implements ItemListener {
    FlowLayout flow = new FlowLayout();
    JComboBox payMethod = new JComboBox();
    JLabel payList = new JLabel("Pay List");
    JTextField totFees = new JTextField(25);
    String pctMsg = new String("per cent will be added to your bill");
    int[] fees = {5, 2, 0};
    int feePct = 0;
    String output;
    int fee = 0;

    public void DebugFourteen2() {
        super("Pay List");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(flow);
        payMethod.addItemListener();
        add(payList);
        add(payMethod);
        payMethod.addItem("Credit card");
        payMethod.addItem("Check");
        payMethod.addItem("Cash");
        add(totFees);
    }

    public static void main(String[] arguments) {
        JFrame cframe = new DebugFourteen2();
        cframe.setSize(350, 150);
        cframe.setVisible(true);
    }

    public void itemStateChanged(ItemEvent list) {
        Object source = list.getSource();
        if (source = payMethod)
            int fee = payMethod.getSelectedIndex();
        feePct = fees[x];
        output = feePct + " " + pctMsg;
        totFees.setText(output);
    }
}

2 个答案:

答案 0 :(得分:1)

你去,它编译,你可以运行它(使用我的代码):输出:

enter image description here

你有很多错误..(可能这是一个要求你修复这个不能编译的代码的作业):

1)     “>`” 在第一行。没有java代码可以编译这行

2)

payMethod.addItemListener();

应更改为payMethod.addItemListener(this);,否则侦听器无法在java ..

中工作

3)feePct = fees[x];应更改为feePct = fees[fee];,因为代码中不存在x ......

4)`在最后一行..在课程结束时没有java程序会使用此字符运行...

5)正如@Tom在评论中提到的那样,这是错误的和不必要的int fee = payMethod.getSelectedIndex();。您已经声明了int fee = 0;您不需要(并且不能因为这是一个错误)将其重新声明为int fee = payMethod.getSelectedIndex(); ..仅fee = payMethod.getSelectedIndex();就够了

    // DebugFourteen2
// Displays list of payment options
// - credit card, check or cash
// Displays fee for using each - 5%, 2% or 0%
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DebugFourteen2 extends JFrame implements ItemListener
{
    FlowLayout flow = new FlowLayout();
    JComboBox payMethod = new JComboBox();
    JLabel payList = new JLabel("Pay List");
    JTextField totFees = new JTextField(25);
    String pctMsg = new String("per cent will be added to your bill");
    int[] fees = {5,2,0};
    int feePct = 0;
    String output;
    int fee = 0;
public DebugFourteen2()
{
//super("Pay List");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(flow);
    payMethod.addItemListener(this);
    add(payList);
    add(payMethod);
    payMethod.addItem("Credit card");
    payMethod.addItem("Check");
    payMethod.addItem("Cash");
    add(totFees);
}
public static void main(String[] arguments)
{
    JFrame cframe = new DebugFourteen2();
    cframe.setSize(350, 150);
    cframe.setVisible(true);
}
public void itemStateChanged(ItemEvent list)
{
    Object source = list.getSource();
    if(source == payMethod)
    fee = payMethod.getSelectedIndex();
    feePct = fees[fee];
    output = feePct + " " + pctMsg;
    totFees.setText(output);
}
}

答案 1 :(得分:0)

我会添加一些评论来解释我所做的更改:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DebugFourteen2 extends JFrame implements ItemListener {
    FlowLayout flow = new FlowLayout();
    JComboBox payMethod = new JComboBox();
    JLabel payList = new JLabel("Pay List");
    JTextField totFees = new JTextField(25);
    String pctMsg = new String("per cent will be added to your bill");
    int[] fees = {5,2,0};
    int feePct = 0;
    String output;
    int fee = 0;
    //public void DebugFourteen2(){
       // super("Pay List");  you can use super in this context because
        // DebugFourteen2 is a method, remove the 'void' to make it a constructor
     public  DebugFourteen2(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(flow);
        //payMethod.addItemListener(); you need to pass an 'ItemListener' to this method
        payMethod.addItemListener(this);
        add(payList);
        add(payMethod);
        payMethod.addItem("Credit card");
        payMethod.addItem("Check");
        payMethod.addItem("Cash");
        add(totFees);
    }
    public static void main(String[] arguments){
        JFrame cframe = new DebugFourteen2();
        cframe.setSize(350, 150);
        cframe.setVisible(true);
    }
    public void itemStateChanged(ItemEvent list){
        Object source = list.getSource();
        //if(source = payMethod)// in Java you compere Objects using `==` instead of `=`
        if(source == payMethod){
            int fee = payMethod.getSelectedIndex();
            //feePct = fees[x];  there is no variable `x` probably you meant to use `fee` here
            feePct = fees[fee];
            output = feePct + " " + pctMsg;
            totFees.setText(output);
        }
    }
}