문제

I have a problem about opening another class because i have a JComboBox that will contain the choices of class that i need to often so here is my code please check it .

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;
                }
            }
        });



    }
}

So if I choose converter then click the button patola I needed to open this frame this is the another class I need to open .

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. This if(e.getSource().equals("Convertion")) is not going to work. e.getSource() returns the object source of a the object firing the ActionEvent. Comparing that to a String will get you nowhere.

  2. Sorry, I just had to point this out because it was bugging me. But what is "Convertion"? Is that even a word. Maybe you mean "Conversion"

  3. Just add the ActionListener to your JComboBox instead

    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. In your converter and probably all your other frame classes, setVisible(true) after adding all components.

  5. Why even use multiple frames. See The Use of Multiple JFrames, Good/Bad Practice?. You might find a better, cleaner solution.

  6. Swing apps should be run on the Event Dispatch Thread. You could do so by wrapping the inside of your main in a SwingUtilities.invokeLater.... See more details at Initial Threads

  7. If you don't want to add the ActionListener to the JComboBox and the JButton instead, just put the switch inside the actionPerformed. There's not need to check the source of the ActionEvent becuase you are only registering that listener with one button.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top