java 学习20181218

MyTable.java和C6.java,实现内容:

窗口显示,弹出对话框,输入框,选择,列表框,按钮事件,模式窗口

 

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;

public class MyTable extends JFrame
{

    private JTable table=null;
    private JButton myBtn1=null;
    private JButton myBtn2=null;
    private JButton addBtn=null;
    private DefaultTableModel model=null;

    public MyTable()
    {
        super("Mytable");
        String[][] datas={};
        String[] titles={"R1","R2"};
        model=new DefaultTableModel(datas,titles);
        table=new JTable(model);
        myBtn1=new JButton("myBtn1");
        myBtn2=new JButton("myBtn2");
        addBtn=new JButton("增加数据");
        
        addBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
               model.addRow(new String[] { getRdData(), getRdData() });
            }
         });

         myBtn1.addActionListener(new ActionListener(){
         
            @Override
            public void actionPerformed(ActionEvent e) {
               int n = JOptionPane.showConfirmDialog(null, "你高兴吗?", "标题",JOptionPane.YES_NO_OPTION);//返回的是按钮的index  i=0或者1  
               model.addRow(new String[] {"选择",String.valueOf(n)});
            }
         });
         myBtn2.addActionListener(new ActionListener(){
         
            @Override
            public void actionPerformed(ActionEvent e) {
              // new C6();
              C6 c6= new C6(MyTable.this);
              if (c6.checkSel()){
               model.addRow(new String[] {"输入:",c6.getMyStr()});
              }
            }
         });

         add(myBtn1,BorderLayout.EAST);
         add(myBtn2,BorderLayout.WEST);
         add(addBtn,BorderLayout.NORTH);
         add(new JScrollPane(table));
        setSize(500,500);
        setLocationRelativeTo(null);  //居中显示
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args)
    {
       new MyTable();
    }
 

    private String getRdData()
    {
       String source = "0123456789abcdefghijklmnopqrstuvwxyz";
       int len = source.length();
       Random random = new Random(System.currentTimeMillis());
       return MessageFormat.format("{0}{0}{0}", source.charAt(random.nextInt(len)));
    }
}

C6.java


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

class FreshJuice {
	enum FreshJuiceSize{ SMALL, MEDIUM , LARGE }
	FreshJuiceSize size;
 }
 
public class C6 extends JDialog  
{
	private boolean isYesOrNo;
	private String myString;
	public C6(Frame myparent) 
	{
		super(myparent, true);
		FreshJuice juice = new FreshJuice();
		juice.size = FreshJuice.FreshJuiceSize.MEDIUM  ;
		Label p1;
		TextField i1;
		Button btnYes=new Button("ok");
		Button btnNo=new Button("no");
		p1 = new Label("Enter integer or some words");
		i1 = new TextField(10);
		setLayout(new FlowLayout());

		
		btnNo.addActionListener(new ActionListener(){
		
			@Override
			public void actionPerformed(ActionEvent e) {
				isYesOrNo=false;
				setVisible(false);
			}
		});

		btnYes.addActionListener(new ActionListener(){
		
			@Override
			public void actionPerformed(ActionEvent e) {
				myString=i1.getText();
				isYesOrNo=true;
				setVisible(false);
			}
		});
		add(p1);
		add(i1);
		add(btnYes);
		add(btnNo);
		setBounds(100, 100, 220, 200*juice.size.ordinal());
		this.setLocationRelativeTo(null);
		setVisible(true);
		//this.addWindowListener(new WindowAdapter() {
		//	@Override
		//	public void windowClosing(WindowEvent e)
		//	{
		//	   System.exit(0);
		//	}
		//});
	}
	public boolean checkSel(){
		return isYesOrNo;
	}
	public String getMyStr(){
		return myString;
	}
//	public static void main(String[] args) 
//	{
//		 new C6();
//	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值