java求助,编一个小程序
展开全部
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Student extends JFrame implements ActionListener{
JPanel panel1,panel2,numPanel,namePanel,sexPanel,addressPanel,
blankPanel,constPanel;
JTextField num,name,address;
JLabel numLabel,nameLabel,addressLabel,sexLabel;
JComboBox sex;
JButton ok;
JTextArea message=new JTextArea(15,30);
String[] sexes={"男","女"};
public Student(){
super();
this.start();
}
public void start(){
this.setTitle("学生信息管理");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(400,400));
panel1=new JPanel();
panel2=new JPanel();
numPanel=new JPanel();
namePanel=new JPanel();
sexPanel=new JPanel();
addressPanel=new JPanel();
blankPanel=new JPanel();
constPanel=new JPanel();
this.getContentPane().add(constPanel);
panel1.setLayout(new GridLayout(3,2));
numLabel=new JLabel("学号:");
nameLabel=new JLabel("姓名:");
sexLabel=new JLabel("性别:");
addressLabel=new JLabel("地址:");
ok=new JButton("增加");
ok.addActionListener(this);
num=new JTextField(10);
name=new JTextField(10);
address=new JTextField(10);
sex=new JComboBox(sexes);
sex.setPreferredSize(new Dimension(113,22));
constPanel.add(panel1);
constPanel.add(panel2);
numPanel.add(numLabel);
numPanel.add(num);
namePanel.add(nameLabel);
namePanel.add(name);
sexPanel.add(sexLabel);
sexPanel.add(sex);
addressPanel.add(addressLabel);
addressPanel.add(address);
panel1.add(numPanel);
panel1.add(namePanel);
panel1.add(sexPanel);
panel1.add(addressPanel);
panel1.add(blankPanel);
panel1.add(ok);
panel2.add(message);
panel2.setBorder(BorderFactory.createTitledBorder("学生信息列表"));
message.setEditable(false);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
message.setText("信息如下:" +"\n");
message.append("学号:"+num.getText()+"\n");
message.append("姓名:"+name.getText()+"\n");
message.append("性别:"+sex.getSelectedItem()+"\n");
message.append("地址:"+address.getText()+"\n");
}
public static void main(String[] args){
Student s=new Student();
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Student extends JFrame implements ActionListener{
JPanel panel1,panel2,numPanel,namePanel,sexPanel,addressPanel,
blankPanel,constPanel;
JTextField num,name,address;
JLabel numLabel,nameLabel,addressLabel,sexLabel;
JComboBox sex;
JButton ok;
JTextArea message=new JTextArea(15,30);
String[] sexes={"男","女"};
public Student(){
super();
this.start();
}
public void start(){
this.setTitle("学生信息管理");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(400,400));
panel1=new JPanel();
panel2=new JPanel();
numPanel=new JPanel();
namePanel=new JPanel();
sexPanel=new JPanel();
addressPanel=new JPanel();
blankPanel=new JPanel();
constPanel=new JPanel();
this.getContentPane().add(constPanel);
panel1.setLayout(new GridLayout(3,2));
numLabel=new JLabel("学号:");
nameLabel=new JLabel("姓名:");
sexLabel=new JLabel("性别:");
addressLabel=new JLabel("地址:");
ok=new JButton("增加");
ok.addActionListener(this);
num=new JTextField(10);
name=new JTextField(10);
address=new JTextField(10);
sex=new JComboBox(sexes);
sex.setPreferredSize(new Dimension(113,22));
constPanel.add(panel1);
constPanel.add(panel2);
numPanel.add(numLabel);
numPanel.add(num);
namePanel.add(nameLabel);
namePanel.add(name);
sexPanel.add(sexLabel);
sexPanel.add(sex);
addressPanel.add(addressLabel);
addressPanel.add(address);
panel1.add(numPanel);
panel1.add(namePanel);
panel1.add(sexPanel);
panel1.add(addressPanel);
panel1.add(blankPanel);
panel1.add(ok);
panel2.add(message);
panel2.setBorder(BorderFactory.createTitledBorder("学生信息列表"));
message.setEditable(false);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
message.setText("信息如下:" +"\n");
message.append("学号:"+num.getText()+"\n");
message.append("姓名:"+name.getText()+"\n");
message.append("性别:"+sex.getSelectedItem()+"\n");
message.append("地址:"+address.getText()+"\n");
}
public static void main(String[] args){
Student s=new Student();
}
}
展开全部
按照你的要求,写出的程序如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class StudentInfoManage {
JFrame jf;
JPanel jp,jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8;
JLabel jl1,jl2,jl3,jl4;
JComboBox jcb;
JTextField jtf1,jtf2,jtf3;
JButton jb,jb1;
JTable jt;
JScrollPane scrollPane;
DefaultTableModel dtm;
StudentInfoManage(){
String headName[] = { "学号", "姓名", "性别", "住址"};
String obj [][] = null;
jf=new JFrame("学生信息管理");
jp1=new JPanel();
jp1.setLayout(new GridLayout(3,2));
jp1.setBorder (BorderFactory.createTitledBorder (""));
jl1=new JLabel("学号");
jl2=new JLabel("姓名");
jl3=new JLabel("性别");
jl4=new JLabel("住址");
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jcb=new JComboBox();
jcb.addItem("男 ");
jcb.addItem("女 ");
jb=new JButton("增加");
jb1=new JButton("删除");
jb.addActionListener(new AddActionListener());
jb1.addActionListener(new AddActionListener());
dtm = new DefaultTableModel(obj,headName);
jt = new JTable(dtm);
jt.setPreferredScrollableViewportSize(new Dimension(300,120));
scrollPane = new JScrollPane(jt);
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp5=new JPanel();
jp6=new JPanel();
jp7=new JPanel();
jp8=new JPanel();
jp3.add(jl1);jp3.add(jtf1);jp4.add(jl2);jp4.add(jtf2);
jp5.add(jl3);jp5.add(jcb);jp6.add(jl4);jp6.add(jtf3);
jp7.add(jb);jp8.add(jb1);
jp1.add(jp3);jp1.add(jp4);jp1.add(jp5);jp1.add(jp6);
jp1.add(jp7);jp1.add(jp8);
jp2.add(scrollPane);
jp2.setBorder (BorderFactory.createTitledBorder ("学生信息列表"));
jp=new JPanel();
jp.setLayout(new GridLayout(2,1));
jp.add(jp1);
jp.add(jp2);
jf.add(jp,BorderLayout.CENTER);
jf.setSize(400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class AddActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb){
String tmp=(String) jcb.getSelectedItem();
tmp=tmp.trim();
String []rowData=new String[4];
rowData[0]=jtf1.getText();
rowData[1]=jtf2.getText();
rowData[2]=tmp;
rowData[3]=jtf3.getText();
dtm.addRow(rowData);
}else if(ae.getSource()==jb1){
int row= jt.getSelectedRow();
if(row!=-1)
dtm.removeRow(row);
}
}
}
public static void main(String[] args) {
new StudentInfoManage();
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class StudentInfoManage {
JFrame jf;
JPanel jp,jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8;
JLabel jl1,jl2,jl3,jl4;
JComboBox jcb;
JTextField jtf1,jtf2,jtf3;
JButton jb,jb1;
JTable jt;
JScrollPane scrollPane;
DefaultTableModel dtm;
StudentInfoManage(){
String headName[] = { "学号", "姓名", "性别", "住址"};
String obj [][] = null;
jf=new JFrame("学生信息管理");
jp1=new JPanel();
jp1.setLayout(new GridLayout(3,2));
jp1.setBorder (BorderFactory.createTitledBorder (""));
jl1=new JLabel("学号");
jl2=new JLabel("姓名");
jl3=new JLabel("性别");
jl4=new JLabel("住址");
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jcb=new JComboBox();
jcb.addItem("男 ");
jcb.addItem("女 ");
jb=new JButton("增加");
jb1=new JButton("删除");
jb.addActionListener(new AddActionListener());
jb1.addActionListener(new AddActionListener());
dtm = new DefaultTableModel(obj,headName);
jt = new JTable(dtm);
jt.setPreferredScrollableViewportSize(new Dimension(300,120));
scrollPane = new JScrollPane(jt);
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp5=new JPanel();
jp6=new JPanel();
jp7=new JPanel();
jp8=new JPanel();
jp3.add(jl1);jp3.add(jtf1);jp4.add(jl2);jp4.add(jtf2);
jp5.add(jl3);jp5.add(jcb);jp6.add(jl4);jp6.add(jtf3);
jp7.add(jb);jp8.add(jb1);
jp1.add(jp3);jp1.add(jp4);jp1.add(jp5);jp1.add(jp6);
jp1.add(jp7);jp1.add(jp8);
jp2.add(scrollPane);
jp2.setBorder (BorderFactory.createTitledBorder ("学生信息列表"));
jp=new JPanel();
jp.setLayout(new GridLayout(2,1));
jp.add(jp1);
jp.add(jp2);
jf.add(jp,BorderLayout.CENTER);
jf.setSize(400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class AddActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb){
String tmp=(String) jcb.getSelectedItem();
tmp=tmp.trim();
String []rowData=new String[4];
rowData[0]=jtf1.getText();
rowData[1]=jtf2.getText();
rowData[2]=tmp;
rowData[3]=jtf3.getText();
dtm.addRow(rowData);
}else if(ae.getSource()==jb1){
int row= jt.getSelectedRow();
if(row!=-1)
dtm.removeRow(row);
}
}
}
public static void main(String[] args) {
new StudentInfoManage();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你有没学过GUI啊 很简单啊 写几个JText 对BUTTON 加个ACTION 然后 getText() 就能得到数据啊
至于显示 你用JTarea 和 Jtable 都行啊 看书吧
至于显示 你用JTarea 和 Jtable 都行啊 看书吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
孩子 还是自己写吧 这个根本就没什么难度的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询