一个主类,一个组件
组件代码:
/**
添加学生信息面板
**/
package mytext.mytext;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mytext4 extends JPanel
implements ActionListener
{
private JLabel jl1 =new JLabel("学号:");
private JLabel jl2 =new JLabel("姓名:");
private JLabel jl3 =new JLabel("性别:");
private JLabel jl4 =new JLabel("所学专业:");
private JLabel jl5 =new JLabel("家庭住址:");
private JLabel jl6 =new JLabel("出生日期:");
JTextField jt1 = new JTextField(10);
JTextField jt2 = new JTextField(10);
JTextField jt3 = new JTextField(10);
JTextField jt4 = new JTextField(10);
private JRadioButton jrb1 =new JRadioButton("男",true);
private JRadioButton jrb2 =new JRadioButton("女");
private ButtonGroup bg = new ButtonGroup();
String [] zy ={"计科师范","计科大类","软工","电商"};
private JComboBox xz =new JComboBox(zy);
private JButton jb1 =new JButton("插入");
private JButton jb2 =new JButton("重置");
private JPanel jp1 =new JPanel();
private JPanel jp2 =new JPanel();
private JPanel jp3 =new JPanel();
private JPanel jp4 =new JPanel();
private JPanel jp5 =new JPanel();
public Mytext4()
{
jp1.setLayout(new GridLayout(1,2));
jp1.add(jbr1); jp1.add(jbr2);
bg.add(jbr1); bg.add(jbr2);
jp2.setLayout(new GridLayout(5,1));
jl1.setHorizontalAlignment(SwingConstants.CENTER);
jl2.setHorizontalAlignment(SwingConstants.CENTER);
jl3.setHorizontalAlignment(SwingConstants.CENTER);
jl4.setHorizontalAlignment(SwingConstants.CENTER);
jl5.setHorizontalAlignment(SwingConstants.CENTER);
jl6.setHorizontalAlignment(SwingConstants.CENTER);
jp2.add(jl1); jp2.add(jl2); jp2.add(jl3);
jp2.add(jl4); jp2.add(jl5); jpe.add(jl6);
jp3.setLayout(new GridLayout(5,1));
jp3.add(jt1); jp3.add(jt2); jp3.add(bg);
jp3.add(xz); jp4.add(jt3); jp3.add(jt4);
jp4.setLayout(new GridLayout(1,2));
jp4.add(jp2); jp4.add(jp3);
jp5.setLayout(new FlowLayout(FlowLayout.CENTER));
jp5.add(jb1); jp5.add(jb2);
this.setLayout(new GridLayout(1,2));
this.add(jp4); this.add(jp5);
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String Command = e.getActionCommand();
if(Command.equals("插入"))
{
insertDB();
}
else if (Command.equals("重置"))
{
reset();
}
}
private void reset()
{
jt1.setText=("");
jt2.setText=("");
jt3.setText=("");
jt4.setText=("");
jrb1.setSelected(true);
xz.setSelectedIndex(0);
}
private void insertDB()
{
String number=jt1.getText();
String name = jt2.getText();
String add =jt3.getText();
String date =jt4.getText();
String sub =zy[xz.getSelectedIndex()];
String sex=null;
if(jrb1.setSelected())
{
sex="男";
}
if(jrb2.setSelected())
{
sex="女";
}
System.out.println("看看效果");
System.out.println("学号:"+number);
System.out.println("姓名:"+name);
System.out.println("性别:"+sex);
System.out.println("专业:"+sub);
System.out.println("住址:"+add);
System.out.println("出生日期:"+date);
}} 主类代码:
import mytext.mytext.Mytext4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mytext3
{
int width=300, height=230,x=120,y=150;
private JFrame jfr=null;
private JMenuBar jmb=null;
private JMenu jm=null;
private JMenuItem file_1,file_2,file_3,file_4,file_5;
private Mytext3()
{
JFrame jfr =new JFrame("学生信息管理系统");
jfr.setSize(400,200);
jfr.setVisible(true);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jmb = new JMenuBar();
jfr.setJMenuBar(jmb);
jm = new JMenu("菜单选项");
jmb.add(jm);
jm.add(file_1 = new JMenuItem("查询学生信息"));
jm.addSeparator();
jm.add(file_2 = new JMenuItem("修改学生信息"));
jm.add(file_3 = new JMenuItem("添加学生信息"));
jm.add(file_4 = new JMenuItem("删除学生信息"));
jm.addSeparator();
jm.add(file_5 = new JMenuItem("退出"));
jfr.setVisible(true);
Container con =jfr.getContentPane();
JPanel jp =new JPanel();
jp.setLayout(new BorderLayout());
jp.setVisible(true);
jp.add(new JLabel("欢迎进入学生管理系统!",JLabel.CENTER));
con.add(jp,BorderLayout.CENTER);
AddListener();
}
private void AddListener()
{
file_1.addActionListener(this);
file_2.addActionListener(this);
file_3.addActionListener(this);
file_4.addActionListener(this);
file_5.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{ String Command = e.getActionCommand();
if (Command.equals("添加学生信息"))
{
JDialog dialog =new JDialog(jfr,true);
dialog.setSize(300,230);
dialog.setLocation(120,150);
dialog.setTitle("添加操作:");
dialog.getContentPane().add(mytext4);
dialog.setVisible(true);
} }
public Mytext4 mytext4 = new Mytext4();
public static void main(String[] args)
{
new Mytext3();
}
}
求解释,求明白!
组件代码:
/**
添加学生信息面板
**/
package mytext.mytext;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mytext4 extends JPanel
implements ActionListener
{
private JLabel jl1 =new JLabel("学号:");
private JLabel jl2 =new JLabel("姓名:");
private JLabel jl3 =new JLabel("性别:");
private JLabel jl4 =new JLabel("所学专业:");
private JLabel jl5 =new JLabel("家庭住址:");
private JLabel jl6 =new JLabel("出生日期:");
JTextField jt1 = new JTextField(10);
JTextField jt2 = new JTextField(10);
JTextField jt3 = new JTextField(10);
JTextField jt4 = new JTextField(10);
private JRadioButton jrb1 =new JRadioButton("男",true);
private JRadioButton jrb2 =new JRadioButton("女");
private ButtonGroup bg = new ButtonGroup();
String [] zy ={"计科师范","计科大类","软工","电商"};
private JComboBox xz =new JComboBox(zy);
private JButton jb1 =new JButton("插入");
private JButton jb2 =new JButton("重置");
private JPanel jp1 =new JPanel();
private JPanel jp2 =new JPanel();
private JPanel jp3 =new JPanel();
private JPanel jp4 =new JPanel();
private JPanel jp5 =new JPanel();
public Mytext4()
{
jp1.setLayout(new GridLayout(1,2));
jp1.add(jbr1); jp1.add(jbr2);
bg.add(jbr1); bg.add(jbr2);
jp2.setLayout(new GridLayout(5,1));
jl1.setHorizontalAlignment(SwingConstants.CENTER);
jl2.setHorizontalAlignment(SwingConstants.CENTER);
jl3.setHorizontalAlignment(SwingConstants.CENTER);
jl4.setHorizontalAlignment(SwingConstants.CENTER);
jl5.setHorizontalAlignment(SwingConstants.CENTER);
jl6.setHorizontalAlignment(SwingConstants.CENTER);
jp2.add(jl1); jp2.add(jl2); jp2.add(jl3);
jp2.add(jl4); jp2.add(jl5); jpe.add(jl6);
jp3.setLayout(new GridLayout(5,1));
jp3.add(jt1); jp3.add(jt2); jp3.add(bg);
jp3.add(xz); jp4.add(jt3); jp3.add(jt4);
jp4.setLayout(new GridLayout(1,2));
jp4.add(jp2); jp4.add(jp3);
jp5.setLayout(new FlowLayout(FlowLayout.CENTER));
jp5.add(jb1); jp5.add(jb2);
this.setLayout(new GridLayout(1,2));
this.add(jp4); this.add(jp5);
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String Command = e.getActionCommand();
if(Command.equals("插入"))
{
insertDB();
}
else if (Command.equals("重置"))
{
reset();
}
}
private void reset()
{
jt1.setText=("");
jt2.setText=("");
jt3.setText=("");
jt4.setText=("");
jrb1.setSelected(true);
xz.setSelectedIndex(0);
}
private void insertDB()
{
String number=jt1.getText();
String name = jt2.getText();
String add =jt3.getText();
String date =jt4.getText();
String sub =zy[xz.getSelectedIndex()];
String sex=null;
if(jrb1.setSelected())
{
sex="男";
}
if(jrb2.setSelected())
{
sex="女";
}
System.out.println("看看效果");
System.out.println("学号:"+number);
System.out.println("姓名:"+name);
System.out.println("性别:"+sex);
System.out.println("专业:"+sub);
System.out.println("住址:"+add);
System.out.println("出生日期:"+date);
}} 主类代码:
import mytext.mytext.Mytext4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mytext3
{
int width=300, height=230,x=120,y=150;
private JFrame jfr=null;
private JMenuBar jmb=null;
private JMenu jm=null;
private JMenuItem file_1,file_2,file_3,file_4,file_5;
private Mytext3()
{
JFrame jfr =new JFrame("学生信息管理系统");
jfr.setSize(400,200);
jfr.setVisible(true);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jmb = new JMenuBar();
jfr.setJMenuBar(jmb);
jm = new JMenu("菜单选项");
jmb.add(jm);
jm.add(file_1 = new JMenuItem("查询学生信息"));
jm.addSeparator();
jm.add(file_2 = new JMenuItem("修改学生信息"));
jm.add(file_3 = new JMenuItem("添加学生信息"));
jm.add(file_4 = new JMenuItem("删除学生信息"));
jm.addSeparator();
jm.add(file_5 = new JMenuItem("退出"));
jfr.setVisible(true);
Container con =jfr.getContentPane();
JPanel jp =new JPanel();
jp.setLayout(new BorderLayout());
jp.setVisible(true);
jp.add(new JLabel("欢迎进入学生管理系统!",JLabel.CENTER));
con.add(jp,BorderLayout.CENTER);
AddListener();
}
private void AddListener()
{
file_1.addActionListener(this);
file_2.addActionListener(this);
file_3.addActionListener(this);
file_4.addActionListener(this);
file_5.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{ String Command = e.getActionCommand();
if (Command.equals("添加学生信息"))
{
JDialog dialog =new JDialog(jfr,true);
dialog.setSize(300,230);
dialog.setLocation(120,150);
dialog.setTitle("添加操作:");
dialog.getContentPane().add(mytext4);
dialog.setVisible(true);
} }
public Mytext4 mytext4 = new Mytext4();
public static void main(String[] args)
{
new Mytext3();
}
}
求解释,求明白!
一一列举如下:
1、Mytext3没有实现ActionListener接口,你却直接用addActionListener(this)这样的语句
2、setText = ("");不是这样用的,是setText("");这样
3、if (jrb1.setSelected()) 这里应给用get方法而不是set,应该是这样
if (jrb1.isSelected())
4、出现部分为定义变量改了下整体如下
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Mytext3 implements ActionListener { int width = 300, height = 230, x = 120, y = 150;
private JFrame jfr = null;
private JMenuBar jmb = null;
private JMenu jm = null;
private JMenuItem file_1, file_2, file_3, file_4, file_5; private Mytext3() { JFrame jfr = new JFrame("学生信息管理系统");
jfr.setSize(400, 200);
jfr.setVisible(true);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jmb = new JMenuBar();
jfr.setJMenuBar(jmb);
jm = new JMenu("菜单选项");
jmb.add(jm);
jm.add(file_1 = new JMenuItem("查询学生信息"));
jm.addSeparator();
jm.add(file_2 = new JMenuItem("修改学生信息"));
jm.add(file_3 = new JMenuItem("添加学生信息"));
jm.add(file_4 = new JMenuItem("删除学生信息"));
jm.addSeparator();
jm.add(file_5 = new JMenuItem("退出"));
jfr.setVisible(true);
Container con = jfr.getContentPane();
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
jp.setVisible(true);
jp.add(new JLabel("欢迎进入学生管理系统!", JLabel.CENTER));
con.add(jp, BorderLayout.CENTER);
AddListener();
} private void AddListener() {
file_1.addActionListener(this);
file_2.addActionListener(this);
file_3.addActionListener(this);
file_4.addActionListener(this);
file_5.addActionListener(this);
} public void actionPerformed(ActionEvent e) {
String Command = e.getActionCommand(); if (Command.equals("添加学生信息")) {
JDialog dialog = new JDialog(jfr, true);
dialog.setSize(300, 230);
dialog.setLocation(120, 150);
dialog.setTitle("添加操作:");
dialog.getContentPane().add(mytext4);
dialog.setVisible(true);
} } public Mytext4 mytext4 = new Mytext4(); public static void main(String[] args) { new Mytext3(); }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Mytext4 extends JPanel implements ActionListener {
private JLabel jl1 = new JLabel("学号:");
private JLabel jl2 = new JLabel("姓名:");
private JLabel jl3 = new JLabel("性别:");
private JLabel jl4 = new JLabel("所学专业:");
private JLabel jl5 = new JLabel("家庭住址:");
private JLabel jl6 = new JLabel("出生日期:");
JTextField jt1 = new JTextField(10);
JTextField jt2 = new JTextField(10);
JTextField jt3 = new JTextField(10);
JTextField jt4 = new JTextField(10);
private JRadioButton jrb1 = new JRadioButton("男", true);
private JRadioButton jrb2 = new JRadioButton("女");
private ButtonGroup bg = new ButtonGroup();
String[] zy = { "计科师范", "计科大类", "软工", "电商" };
private JComboBox xz = new JComboBox(zy);
private JButton jb1 = new JButton("插入");
private JButton jb2 = new JButton("重置");
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private JPanel jp3 = new JPanel();
private JPanel jp4 = new JPanel();
private JPanel jp5 = new JPanel(); public Mytext4() {
jp1.setLayout(new GridLayout(1, 2));
// jp1.add(jbr1);
// jp1.add(jbr2);
// bg.add(jbr1);
// bg.add(jbr2);
jp2.setLayout(new GridLayout(5, 1));
jl1.setHorizontalAlignment(SwingConstants.CENTER);
jl2.setHorizontalAlignment(SwingConstants.CENTER);
jl3.setHorizontalAlignment(SwingConstants.CENTER);
jl4.setHorizontalAlignment(SwingConstants.CENTER);
jl5.setHorizontalAlignment(SwingConstants.CENTER);
jl6.setHorizontalAlignment(SwingConstants.CENTER);
jp2.add(jl1);
jp2.add(jl2);
jp2.add(jl3);
jp2.add(jl4);
jp2.add(jl5);
jp2.add(jl6);
jp3.setLayout(new GridLayout(5, 1));
jp3.add(jt1);
jp3.add(jt2);
jp3.add(xz);
jp4.add(jt3);
jp3.add(jt4);
jp4.setLayout(new GridLayout(1, 2));
jp4.add(jp2);
jp4.add(jp3);
jp5.setLayout(new FlowLayout(FlowLayout.CENTER));
jp5.add(jb1);
jp5.add(jb2);
this.setLayout(new GridLayout(1, 2));
this.add(jp4);
this.add(jp5);
jb1.addActionListener(this);
jb2.addActionListener(this);
} public void actionPerformed(ActionEvent e) {
String Command = e.getActionCommand();
if (Command.equals("插入")) {
insertDB();
} else if (Command.equals("重置")) {
reset();
}
} private void reset() {
jt1.setText("");
jt2.setText("");
jt3.setText("");
jt4.setText("");
jrb1.setSelected(true);
xz.setSelectedIndex(0);
} private void insertDB() {
String number = jt1.getText();
String name = jt2.getText();
String add = jt3.getText();
String date = jt4.getText();
String sub = zy[xz.getSelectedIndex()];
String sex = null;
if (jrb1.isSelected()) {
sex = "男";
}
if (jrb2.isSelected()) {
sex = "女";
}
System.out.println("看看效果");
System.out.println("学号:" + number);
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("专业:" + sub);
System.out.println("住址:" + add);
System.out.println("出生日期:" + date);
}}
1. Mytext.java:42: 无法将 javax.swing.AbstractButton 中的addActionListener(java.awt.event.ActionListener)应用于 <Mytext3>
file_x.addAcionListener(this);(x=1,2,3,4,5)
2. .\mytext\mytext\Mytext4.java:78:找不到符号
符号:变量setText
位置:类javax.swing.JTextField
jtx.setText("");(x=1,2,3,4)
3. .\mytext\mytext\Mytext4.java:93:无法将 javax.swing.AbstractButton 中的 setSelected
(boolean)应用于()
if(jrbx.setSelected()) (x=1,2)