MyJFrame(文本)界面的建立
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class MyJFrame extends JFrame implements ActionListener,MouseListener{
private JTextField tfdname;
private JTextField tfdpwd;
JButton btn1,btn2,btn3,btn4,btn5,btn6;
public MyJFrame(String str){
setTitle( str);
setLocation(300,200);
setSize(400, 370);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(Color.red);
getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT,5,20));
JLabel lb1=new JLabel("姓名");
tfdname= new JTextField(22);
add(lb1);
add(tfdname);
JLabel lb2=new JLabel("密码");
tfdpwd =new JTextField(22);
add(lb2);
add(tfdpwd);
btn1=new JButton("MOVE");
btn2=new JButton("退出");
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3=new JButton("JOptionPane");
add(btn3);
btn3.addActionListener(this);
btn4=new JButton("JOptionPane2");
add(btn4);
btn4.addActionListener(this);
btn5=new JButton("JOptionPane3");
add(btn5);
btn5.addActionListener(this);
btn6=new JButton("JColorChoose");
add(btn6);
btn6.addActionListener(this);
addMenu();
addPopMenu();
String datas[][]={{"70","65","80","90"},
{"69","45","90","80"},
{"89","78","56","67"},
{"89","58","50","40"}
};
String titles[]={"math","language","art","programe"};
JTable table=new JTable(datas,titles);
table.setSize(300,200);
this.getContentPane().add(new JScrollPane(table));
setVisible(true);
}
private JPopupMenu pMenu=null;
private void addPopMenu() {
pMenu =new JPopupMenu();
JMenuItem miCopy=new JMenuItem("复制");
JMenuItem miCut=new JMenuItem("剪切");
JMenuItem miPaste=new JMenuItem("复制");
pMenu.add(miCopy);
pMenu.add(miCut);
pMenu.add(miPaste);
tfdname.add(pMenu);
tfdname. addMouseListener(this);
miCopy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
tfdname.copy();
}
});
miCut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfdname.cut();
}
});
}
private void addMenu() {
JMenuBar bar = new JMenuBar();
this.setJMenuBar(bar);
JMenu menuFile=new JMenu("文件");
bar.add(menuFile );
JMenuItem menuNew=new JMenuItem("新建");
menuFile.add(menuNew);
JMenuItem menuOpen=new JMenuItem("打开");
menuFile.add(menuOpen);
menuFile.addSeparator();
JMenuItem menuExit=new JMenuItem("退出");
menuExit.setActionCommand("e");
menuFile.add(menuExit);
JMenu menuEdit=new JMenu("编辑");
bar.add(menuEdit);
JMenuItem miCopy=new JMenuItem("复制");
menuEdit.add(miCopy);
JMenuItem miCut=new JMenuItem("剪切");
menuEdit.add(miCut);
JMenuItem miPaste=new JMenuItem("粘贴");
menuEdit.add(miPaste);
JCheckBoxMenuItem miSel=new JCheckBoxMenuItem("选择",true);
menuEdit.add(miSel);
AbstractButton miExit;
menuExit.addActionListener(this);
}
public static void main(String args[])
{
new MyJFrame("湖南城市学院");
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1){
String text=tfdname.getText();
tfdpwd.setText(text);
tfdname.setText("");
}
if(e.getSource()==btn2){
System.exit(0);
}
if(e.getSource()==btn3){
int i=JOptionPane.showConfirmDialog(this ,"请确认--城院");
if(i==JOptionPane.CANCEL_OPTION){
System.out.println("取消");
}
else if(i==JOptionPane.NO_OPTION){
System.out.println("否");
}
else if(i==JOptionPane.OK_OPTION){
System.out.println("是");
}
}
if(e.getSource()==btn4){
JOptionPane.showMessageDialog(this, "我的提示信息--城院");
}
if(e.getSource()==btn5){
String str=JOptionPane.showInputDialog("请输入你的姓名:" );
System.out.println("你输入的是:"+str);
}
if(e.getSource()==btn6){
Color c=JColorChooser.showDialog( this, "请选择颜色",Color.CYAN);
this.getContentPane().setBackground(c);
}
if(e.getActionCommand().equals( "e")){
System.exit(0);
}
}
public void mouseClicked(MouseEvent e) {
if(e.getModifiers()==MouseEvent.BUTTON2_MASK){
pMenu.show(tfdname,e.getX(),e.getY());
}
}
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}