Java GUI界面编程举例

下面是简单的Java图形界面编程示例,作为笔者的复习笔记吧~

Test1:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test {
	
	public static void main(String args[]) {
		final JFrame frame = new JFrame("InternalConfirmDialog Example");
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setBounds(200, 200, 400, 200);
		JButton b = new JButton();
		frame.getContentPane().add(b);
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (JOptionPane.showInternalConfirmDialog(frame.getContentPane(), "确认退出系统吗?", "退出",
						JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
					System.exit(0);
				}
			}
		});
		frame.setVisible(true);	
	}
}

Test2:

import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class Test extends JFrame{
	private static final long serialVersionUID = -2518477425676861150L;
	private JPanel  mPanel;
	private JTable  mTable;
	public static void main(String[] args){
		Test  test=new Test();
		test.setVisible(true); 
		System.out.println("mainTestNew");
	}
	public Test(){
		this.setTitle("MyTable");
		this.setBounds(400, 200, 470, 310);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JScrollPane scrollPane=new JScrollPane();
		mPanel=new JPanel();
        mPanel.setBorder(new EmptyBorder(5,5,3,3));	
        mPanel.setLayout(new BorderLayout(0,0));
        this.setContentPane(mPanel);
        mPanel.add(scrollPane,BorderLayout.CENTER);

        VectorcolumnNames=new Vector();
        columnNames.add("Age");
        columnNames.add("Height");
        columnNames.add("Weight");
        Vector>values=new Vector>();
        for(int row=1;row<8;row++){
        	Vector rowData=new Vector();
        	rowData.add("A"+row);
        	rowData.add("B"+row);
        	rowData.add("C"+row);
        	values.add(rowData);
        }
    	DefaultTableModel  tableModel=new DefaultTableModel(values,columnNames);
    	mTable=new JTable(tableModel);
    	tableModel.addTableModelListener(new TableModelListener(){
			public void tableChanged(TableModelEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println("Table Changed Listener!");	 
			}
    	});
    	JTableHeader  tableHeader=mTable.getTableHeader();
    	tableHeader.setReorderingAllowed(false);    	
    	scrollPane.setViewportView(mTable);
	}
}



你可能感兴趣的:(Java开发)