打开另一堂课的麻烦

时间:2014-03-09 02:16:14

标签: java swing class user-interface jcombobox

我有一个关于打开另一个类的问题,因为我有一个JComboBox,它将包含我经常需要的类的选择,所以这里是我的代码,请检查它。

package Patola;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class imagebut extends JFrame {
    // ImageIcon we = new ImageIcon(getClass().getResource("ban.png"));
    // ImageIcon wer = new ImageIcon(getClass().getResource("ba.png"));

    public static void main(String args[]) {
        imagebut w = new imagebut();
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        w.setSize(300, 300);
        w.setVisible(true);


    }
    String[] cent = {"Conversion", "Adv.Calculator"};
    JComboBox box = new JComboBox(cent);
    JButton bi = new JButton("Patola");

    public imagebut() {
        /* JButton converter = new JButton("Convertion");
        JButton advancecalc = new JButton("Adv.Calculator");
        JButton calc = new JButton("Calculator");
        JButton Multiplication = new JButton("Multiplication");
        JButton Looping = new JButton("Looping");
        JButton Calendar = new JButton("Calendar");
        JButton Remarks = new JButton("Remarks");
        JButton Average = new JButton("Average");
        JButton Magicsq = new JButton("Magic Square");*/


        //      JLabel background = new JLabel(new ImageIcon(getClass().getResource("gif.gif")));   
        JPanel pan = new JPanel();


        box.setBounds(10, 10, 100, 25);
        bi.setBounds(40, 40, 80, 30);
        getContentPane().add(pan);
        //getContentPane().add(background);
        pan.add(box);
        pan.add(bi);
        /*
        background.add(box);
        background.add(bi);
         */


        /* background.add(converter);
        background.add(calc);
        background.add(advancecalc);
        background.add(Magicsq);
        background.add(Remarks);
        background.add(Calendar);
        background.add(Average);
        background.add(Looping);
        background.add(Multiplication);*/


        box.addActionListener(new ActionListener() 
        {

            public void actionPerformed(ActionEvent e) 
            {

                String s = (String) box.getSelectedItem();
                switch(s) 
                {
                        case "Convertion": new converter(); break;
                }
            }
        });



    }
}

因此,如果我选择转换器,然后单击按钮patola我需要打开此框架,这是我需要打开的另一个类。

package Patola;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class converter extends JFrame implements ActionListener
{
        Container con=getContentPane();

        JTextField input=new JTextField("");
        JButton BMI=new JButton("B.M.I");
        JButton dollars=new JButton("Peso to Dollars");
        JButton euro=new JButton("Peso to Euros");
        JButton yen=new JButton("Peso to Yen");
        JButton c=new JButton("Celcius to Farenheit");
        JButton f=new JButton("Farenheit to Celcius");
        JButton m=new JButton("Miles to Kilometers");
        JButton km=new JButton("Kilometers to Miles");
                JTextField inch = new JTextField(15);
                JButton ok = new JButton("Okay");
                JLabel lab = new JLabel("Enter height to inches");
                JFrame fr = new JFrame();

                double a,b,d;



    public converter()
        {
    super("Super Converter");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    con.setLayout(new GridLayout(0,1));
    con.add(input);
    con.add(BMI);
    con.add(dollars);
    con.add(euro);
    con.add(yen);
    con.add(c);
    con.add(f);
    con.add(m);
    con.add(km);
    BMI.addActionListener(this);
    dollars.addActionListener(this);
    yen.addActionListener(this);
    euro.addActionListener(this);
    c.addActionListener(this);
    f.addActionListener(this);
    m.addActionListener(this);
    km.addActionListener(this);
        ok.addActionListener(this);
    setContentPane(con);

            fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                        fr.setVisible(false);
                                        fr.setSize(200,150);
                                        JPanel p = new JPanel();
                                        p.setLayout(new FlowLayout());


                                        p.add(lab);
                                        p.add(inch);
                                        p.add(ok);
                                        fr.add(p);


            }
            public void actionPerformed(ActionEvent e)
                        {

                            {
                                try
                                {
                if(e.getSource()==BMI)
                                {
                    fr.setLocation(500,250);
                                        fr.setVisible(true);

                                }

                                 if(e.getSource() == ok)
                                       {

                                            a=Double.parseDouble(inch.getText());
                                            b=Double.parseDouble(input.getText());
                                            d=b/a;
                                            JOptionPane.showMessageDialog(fr,"BMI: "+d);
                                            input.setText("");
                                            inch.setText("");
                                            fr.setVisible(false);
                                       }
                    else if(e.getSource()== dollars)
                                        {
                        double a,b,d;
                        a=44.63;
                        b=Double.parseDouble(input.getText());
                        d=b/a;
                        JOptionPane.showMessageDialog(null,"$"+d);
                        input.setText("");
                                        }
                        else if(e.getSource()== yen)
                                                {
                        double a,b,d;
                        a=2.28;
                        b=Double.parseDouble(input.getText());
                        d=b*a;
                        JOptionPane.showMessageDialog(null,"�"+d);
                        input.setText("");
                                                }
                                else if(e.getSource()== euro)
                                                                {
                        double a,b,d;
                        a=0.016;
                        b=Double.parseDouble(input.getText());
                        d=b*a;
                        JOptionPane.showMessageDialog(null,"�"+d);
                        input.setText("");
                                                                }
                            else if(e.getSource()== c)
                                                        {
                        double a,b,d,f;
                        a=1.8;
                        f=32.0;
                        b=Double.parseDouble(input.getText());
                        d=((b*a)+f);
                        JOptionPane.showMessageDialog(null,d+"�F");
                        input.setText("");
                                                        }
                                                        else if(e.getSource()== f){
                        double a,b,d,g;
                        a=0.555555555555556;
                        g=32.0;
                        b=Double.parseDouble(input.getText());
                        d=(b-g)*a;
                        JOptionPane.showMessageDialog(null,d+"ºC");
                        input.setText("");}
                        else if(e.getSource()== m)
                                                {
                        double a,b,d,f;
                        a=1.60934;
                        b=Double.parseDouble(input.getText());
                        d=(b*a);
                        JOptionPane.showMessageDialog(null,d+"Kilometers");
                        input.setText("");
                                                }
                        else if(e.getSource()== km)
                                                {

                        double a,b,d,f;
                        a=0.621371;
                                                b=Double.parseDouble(input.getText());
                        d=(b*a);
                        JOptionPane.showMessageDialog(null,d+"miles");
                        input.setText("");

                                                }
                                             }
                                catch(NumberFormatException qwe)
                                {
                                    JOptionPane.showMessageDialog(null,"Exception Thread No value !");
                                }
                    }

                        }



            public static void main (String [] args)
                        {
                converter aFrame=new converter();
                aFrame.setSize(250,350);
                                aFrame.setLocation(500,250);
                                aFrame.pack();
                                aFrame.setVisible(true);


            }
}

1 个答案:

答案 0 :(得分:1)

  1. if(e.getSource().equals("Convertion"))不起作用。 e.getSource()返回触发ActionEvent的对象的对象源。将其与String进行比较将无处可寻。

  2. 抱歉,我只是 指出了这一点,因为它让我烦恼。但是什么“转换”?这甚至是一个字。也许你的意思是“转换”

  3. 只需将ActionListener添加到JComboBox

    即可
    box.addActionListener(new ActionListener(){
        public void actionPerfomed(ActionEvent e) {
            String s = (String)box.getSelectedItem();
            switch(s) {
                case "Convertion": new conveter(); break;
                case "Adv.Calculator": ...
            }
        }
    });
    
  4. converter以及可能所有其他框架类中,添加所有组件之后setVisible(true)

  5. 为什么甚至使用多个帧。见The Use of Multiple JFrames, Good/Bad Practice?。您可能会找到更好,更清洁的解决方案。

  6. 应在Event Dispatch Thread上运行Swing应用程序。您可以将main的内部包裹在SwingUtilities.invokeLater...中。请参阅Initial Threads

  7. 了解更多详情
  8. 如果您想要将ActionListener添加到JComboBoxJButton,只需添加switchactionPerformed内。没有必要检查ActionEvent的来源,因为你只用一个按钮注册了那个听众。