编译显示无问题的 可是运行时 在弹出第一个用户登录框后 第二个组件演示框就出不来了
大家能不能帮帮忙
程序有点长的:劳烦大家了
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;public class Ex9_10 implements ActionListener ,ItemListener
{
static JFrame f=null;
ButtonGroup bg;
JComboBox combo;
JSpinner s1;
JLabel L3;
JRadioButton r1,r2,r3,r4;
int op=0;
public Ex9_10()
{
JFrame f=new JFrame("第二类原子组件演示");
Container contentPane=f.getContentPane();
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(1,4));
p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(
Color.blue,2),"选择运算种类",TitledBorder.CENTER,TitledBorder.TOP));
r1=new JRadioButton("+");
r2=new JRadioButton("-");
r3=new JRadioButton("*");
r4=new JRadioButton("/");
p1.add(r1);
p1.add(r2);
p1.add(r3);
p1.add(r4);
bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
bg.add(r4);
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
r4.addItemListener(this);
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(2,2));
p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.blue,2),
"选择或输入操作数",TitledBorder.CENTER,TitledBorder.TOP));
JLabel L1=new JLabel("第一个操作数:");
JLabel L2=new JLabel("第二个操作数:");
String[] data1={"0","10","20","30","40","50","60","70","80","90","100"};
combo=new JComboBox(data1);
combo.setEditable(true);
ComboBoxEditor editor=combo.getEditor();
combo.configureEditor (editor,"请选择或直接输入数字");
SpinnerModel sM1=new SpinnerNumberModel(50,0,100,1);
s1=new JSpinner(sM1);
p2.add(L1);
p2.add(combo);
p2.add(L2);
p2.add(s1);
JPanel p3=new JPanel();
p3.setLayout(new GridLayout(1,2));
JButton button1=new JButton("计算");
L3=new JLabel("",SwingConstants.CENTER);
p3.add(button1);
p3.add(L3);
button1.addActionListener(this);
contentPane.add(p1,BorderLayout.NORTH);
contentPane.add(p2,BorderLayout.CENTER);
contentPane.add(p3,BorderLayout.SOUTH);
f.getRootPane().setDefaultButton(button1);
f.pack();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==r1) op=1;
if(e.getSource()==r2) op=2;
if(e.getSource()==r3) op=3;
if(e.getSource()==r4) op=4;
}
public void actionPerformed(ActionEvent e)
{
double a=Double.parseDouble(combo.getSelectedItem().toString());
double b=Double.parseDouble(s1.getValue().toString());
double c;
switch(op)
{
case 1:
c=a+b;
L3.setText(""+c);
break;
case 2:
c=a-b;
L3.setText(""+c);
break;
case 3:
c=a*b;
L3.setText(""+c);
break;
case 4:
c=a/b;
L3.setText(""+c);
break;
default:
L3.setText("请选择运算符");
}
}
public static void main(String args[])
{
new PassWord(f);
}
}class PassWord implements ActionListener
{
JTextField user;
JPasswordField passWd;
JButton b1,b2;
Container dialogPane;
JDialog d;
JFrame f;
public PassWord(JFrame f)
{
d=new JDialog();
d.setTitle("请输入用户名和密码");
dialogPane=d.getContentPane();
dialogPane.setLayout(new GridLayout(3,2));
dialogPane.add(new JLabel("用户名",SwingConstants.CENTER));
user=new JTextField();
dialogPane.add(user);
dialogPane.add(new JLabel("密码",SwingConstants.CENTER));
passWd=new JPasswordField();
dialogPane.add(passWd);
b1=new JButton ("确定");
b2=new JButton ("退出");
dialogPane.add(b1);
dialogPane.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
d.setBounds(200,150,400,130);
d.getRootPane().setDefaultButton(b1);
d.setVisible(true);
this.f=f;
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("确定"))
{
String name=user.getText();
String passWord=passWd.getText();
if((name.equals("test"))&&(passWord.equals("1234")))
{
d.dispose();
f.setVisible(true);
return;
}
else
{
JOptionPane.showMessageDialog(d,"错误的用户名或密码","请重新输入",JOptionPane.WARNING_MESSAGE);
user.setText("");
passWd.setText("");
}
}
if(cmd.equals("退出")) System.exit(0);
}
}
大家能不能帮帮忙
程序有点长的:劳烦大家了
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;public class Ex9_10 implements ActionListener ,ItemListener
{
static JFrame f=null;
ButtonGroup bg;
JComboBox combo;
JSpinner s1;
JLabel L3;
JRadioButton r1,r2,r3,r4;
int op=0;
public Ex9_10()
{
JFrame f=new JFrame("第二类原子组件演示");
Container contentPane=f.getContentPane();
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(1,4));
p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(
Color.blue,2),"选择运算种类",TitledBorder.CENTER,TitledBorder.TOP));
r1=new JRadioButton("+");
r2=new JRadioButton("-");
r3=new JRadioButton("*");
r4=new JRadioButton("/");
p1.add(r1);
p1.add(r2);
p1.add(r3);
p1.add(r4);
bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
bg.add(r4);
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
r4.addItemListener(this);
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(2,2));
p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.blue,2),
"选择或输入操作数",TitledBorder.CENTER,TitledBorder.TOP));
JLabel L1=new JLabel("第一个操作数:");
JLabel L2=new JLabel("第二个操作数:");
String[] data1={"0","10","20","30","40","50","60","70","80","90","100"};
combo=new JComboBox(data1);
combo.setEditable(true);
ComboBoxEditor editor=combo.getEditor();
combo.configureEditor (editor,"请选择或直接输入数字");
SpinnerModel sM1=new SpinnerNumberModel(50,0,100,1);
s1=new JSpinner(sM1);
p2.add(L1);
p2.add(combo);
p2.add(L2);
p2.add(s1);
JPanel p3=new JPanel();
p3.setLayout(new GridLayout(1,2));
JButton button1=new JButton("计算");
L3=new JLabel("",SwingConstants.CENTER);
p3.add(button1);
p3.add(L3);
button1.addActionListener(this);
contentPane.add(p1,BorderLayout.NORTH);
contentPane.add(p2,BorderLayout.CENTER);
contentPane.add(p3,BorderLayout.SOUTH);
f.getRootPane().setDefaultButton(button1);
f.pack();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==r1) op=1;
if(e.getSource()==r2) op=2;
if(e.getSource()==r3) op=3;
if(e.getSource()==r4) op=4;
}
public void actionPerformed(ActionEvent e)
{
double a=Double.parseDouble(combo.getSelectedItem().toString());
double b=Double.parseDouble(s1.getValue().toString());
double c;
switch(op)
{
case 1:
c=a+b;
L3.setText(""+c);
break;
case 2:
c=a-b;
L3.setText(""+c);
break;
case 3:
c=a*b;
L3.setText(""+c);
break;
case 4:
c=a/b;
L3.setText(""+c);
break;
default:
L3.setText("请选择运算符");
}
}
public static void main(String args[])
{
new PassWord(f);
}
}class PassWord implements ActionListener
{
JTextField user;
JPasswordField passWd;
JButton b1,b2;
Container dialogPane;
JDialog d;
JFrame f;
public PassWord(JFrame f)
{
d=new JDialog();
d.setTitle("请输入用户名和密码");
dialogPane=d.getContentPane();
dialogPane.setLayout(new GridLayout(3,2));
dialogPane.add(new JLabel("用户名",SwingConstants.CENTER));
user=new JTextField();
dialogPane.add(user);
dialogPane.add(new JLabel("密码",SwingConstants.CENTER));
passWd=new JPasswordField();
dialogPane.add(passWd);
b1=new JButton ("确定");
b2=new JButton ("退出");
dialogPane.add(b1);
dialogPane.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
d.setBounds(200,150,400,130);
d.getRootPane().setDefaultButton(b1);
d.setVisible(true);
this.f=f;
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("确定"))
{
String name=user.getText();
String passWord=passWd.getText();
if((name.equals("test"))&&(passWord.equals("1234")))
{
d.dispose();
f.setVisible(true);
return;
}
else
{
JOptionPane.showMessageDialog(d,"错误的用户名或密码","请重新输入",JOptionPane.WARNING_MESSAGE);
user.setText("");
passWd.setText("");
}
}
if(cmd.equals("退出")) System.exit(0);
}
}
解决方案 »
- 请问jdk-6u20为什么没有sun.audio这个包?
- javac编译的一个问题,为什么提示软件包javacard.framework不存在
- Java新手又来提问鸟,关于将int型赋给char和将char型赋int~
- 请问怎莫在jbuilder9中布局????
- 请帮我看看这个通配符泛型错在哪里?
- 我想用java完成一个小的功能,来测试一下我所学的东西,谁有简单一点的题目?
- 请问:java 的applet如何调用本地的c函数?
- java对象
- 用什么方法可以遍历一个类的所有实例?给点代码。谢谢
- 5分钟,得100分,来者有分
- java里如何接受request.getParameter("
- EditPLus 快捷键设置
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Main implements ActionListener, ItemListener {
static JFrame f = null;
ButtonGroup bg;
JComboBox combo;
JSpinner s1;
JLabel L3;
JRadioButton r1, r2, r3, r4;
int op = 0; public Main() {
f = new JFrame("第二类原子组件演示");
Container contentPane = f.getContentPane(); JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 4)); p1.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createLineBorder(Color.blue, 2), "选择运算种类",
TitledBorder.CENTER, TitledBorder.TOP)); r1 = new JRadioButton("+");
r2 = new JRadioButton("-");
r3 = new JRadioButton("*");
r4 = new JRadioButton("/");
p1.add(r1);
p1.add(r2);
p1.add(r3);
p1.add(r4);
bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
bg.add(r4);
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
r4.addItemListener(this); JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2, 2));
p2.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createLineBorder(Color.blue, 2), "选择或输入操作数",
TitledBorder.CENTER, TitledBorder.TOP));
JLabel L1 = new JLabel("第一个操作数:");
JLabel L2 = new JLabel("第二个操作数:"); String[] data1 = { "0", "10", "20", "30", "40", "50", "60", "70", "80",
"90", "100" };
combo = new JComboBox(data1);
combo.setEditable(true);
ComboBoxEditor editor = combo.getEditor();
combo.configureEditor(editor, "请选择或直接输入数字"); SpinnerModel sM1 = new SpinnerNumberModel(50, 0, 100, 1);
s1 = new JSpinner(sM1);
p2.add(L1);
p2.add(combo);
p2.add(L2);
p2.add(s1);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 2));
JButton button1 = new JButton("计算");
L3 = new JLabel("", SwingConstants.CENTER);
p3.add(button1);
p3.add(L3);
button1.addActionListener(this); contentPane.add(p1, BorderLayout.NORTH);
contentPane.add(p2, BorderLayout.CENTER);
contentPane.add(p3, BorderLayout.SOUTH); f.getRootPane().setDefaultButton(button1);
f.pack();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
} public void itemStateChanged(ItemEvent e) {
if (e.getSource() == r1)
op = 1;
if (e.getSource() == r2)
op = 2;
if (e.getSource() == r3)
op = 3;
if (e.getSource() == r4)
op = 4;
} public void actionPerformed(ActionEvent e) {
double a = Double.parseDouble(combo.getSelectedItem().toString());
double b = Double.parseDouble(s1.getValue().toString());
double c;
switch (op) {
case 1:
c = a + b;
L3.setText("" + c);
break;
case 2:
c = a - b;
L3.setText("" + c);
break;
case 3:
c = a * b;
L3.setText("" + c);
break;
case 4:
c = a / b;
L3.setText("" + c);
break;
default:
L3.setText("请选择运算符");
}
} public static void main(String args[]) {
new PassWord(new Main().f); }
}class PassWord implements ActionListener {
JTextField user;
JPasswordField passWd;
JButton b1, b2;
Container dialogPane;
JDialog d;
JFrame f; public PassWord(JFrame f) {
d = new JDialog();
d.setTitle("请输入用户名和密码");
dialogPane = d.getContentPane();
dialogPane.setLayout(new GridLayout(3, 2));
dialogPane.add(new JLabel("用户名", SwingConstants.CENTER));
user = new JTextField();
dialogPane.add(user);
dialogPane.add(new JLabel("密码", SwingConstants.CENTER)); passWd = new JPasswordField();
dialogPane.add(passWd);
b1 = new JButton("确定");
b2 = new JButton("退出");
dialogPane.add(b1);
dialogPane.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
d.setBounds(200, 150, 400, 130);
d.getRootPane().setDefaultButton(b1);
d.setVisible(true);
this.f = f;
} public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("确定")) {
String name = user.getText();
String passWord = passWd.getText();
if ((name.equals("test")) && (passWord.equals("1234"))) {
d.dispose();
f.setVisible(true);
return;
} else {
JOptionPane.showMessageDialog(d, "错误的用户名或密码", "请重新输入",
JOptionPane.WARNING_MESSAGE);
user.setText("");
passWd.setText("");
}
}
if (cmd.equals("退出"))
System.exit(0);
}
}