我在打开另一个类时遇到问题,因为我有一个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);
}
}
-
这个
if(e.getSource().equals("Convertion"))
不起作用。CCD_ 4返回激发CCD_ 5的对象的对象源。将其与String
进行比较不会有任何结果。 -
对不起,我只是让指出这一点,因为它困扰了我。但是什么"转换"?这是一个词吗。也许你的意思是"转换">
-
只需将
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": ... } } });
-
在您的
converter
以及可能的所有其他框架类中,添加所有组件后的setVisible(true)
。 -
为什么还要使用多个框架。请参阅多个JFrame的使用,良好/不良做法?。你可能会找到一个更好、更清洁的解决方案。
-
Swing应用程序应该在事件调度线程上运行。您可以将
main
的内部包裹在SwingUtilities.invokeLater...
中。查看初始线程的更多详细信息 -
如果不想将
ActionListener
添加到JComboBox
和JButton
中,只需将switch
放在actionPerformed
中即可。不需要检查ActionEvent
的来源,因为您只需要用一个按钮注册该侦听器。