awt和swing学习(一)

来源:互联网 发布:网络编辑是什么来的 编辑:程序博客网 时间:2024/05/21 18:44
Code:
  1. import java.util.*;   
  2. import java.io.*;   
  3. import java.awt.*;   
  4. import java.awt.event.*;   
  5. import javax.swing.*;   
  6.   
  7. public class TestLayout {   
  8.     public static void main(String[] args) {   
  9.         MyFrame mf = new MyFrame();   
  10.     }   
  11. }   
  12.   
  13. class MyFrame extends JFrame implements ActionListener {   
  14.        
  15.     public MyFrame() {   
  16.         super();   
  17.         this.setTitle("TestMyFrame");   
  18.         this.setSize(800600);   
  19.         this.centerOnScreen();   
  20.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  21.         Container p = getContentPane();   
  22.         p.setLayout(new BorderLayout());   
  23.            
  24.         JMenuBar jmb = new JMenuBar();   
  25.         JMenu[] jm = new JMenu[3];   
  26.         JMenuItem[][] jmi = { {new JMenuItem("打开"),new JMenuItem("保存")},   
  27.                               {new JMenuItem("选择")},   
  28.                               {new JMenuItem("新窗口")}   
  29.                             };   
  30.         String[] mName = {"文件""颜色""窗口"};   
  31.         for(int i=0; i<jm.length; i++) {   
  32.             jm[i] = new JMenu(mName[i]);   
  33.             for(int j=0; j<jmi[i].length; j++) {   
  34.               jmi[i][j].addActionListener(this);   
  35.                 jm[i].add(jmi[i][j]);   
  36.             }   
  37.             jmb.add(jm[i]);   
  38.       }   
  39.         this.setJMenuBar(jmb);   
  40.            
  41.         setVisible(true);      
  42.     }   
  43.        
  44.     public void actionPerformed(ActionEvent e) {   
  45.         if(e.getActionCommand()=="打开") {   
  46.             JFileChooser chooseFile = new JFileChooser();   
  47.             int returnVal = chooseFile.showOpenDialog(null);   
  48.       if(returnVal == chooseFile.APPROVE_OPTION) {   
  49.         File f = chooseFile.getSelectedFile();   
  50.         JOptionPane.showConfirmDialog(this"你选择的文件名是:"+chooseFile.getName(f),   
  51.                                       "确认",JOptionPane.ERROR_MESSAGE);   
  52.         }   
  53.         } else if(e.getActionCommand()=="保存") { //也可以用getsource()方法获得那个变量   
  54.             JFileChooser chooseFile = new JFileChooser();   
  55.             int returnVal = chooseFile.showSaveDialog(null);   
  56.       if(returnVal == chooseFile.APPROVE_OPTION) {   
  57.         File f = chooseFile.getSelectedFile();   
  58.        JOptionPane.showConfirmDialog(this,chooseFile.getName(f));   
  59.       }   
  60.         } else if(e.getActionCommand()=="选择") {   
  61.             Color tmpColor = JColorChooser.showDialog(this"调色板", Color.red);   
  62.         } else if(e.getActionCommand()=="新窗口") {   
  63.             MySmallFrame msf = new MySmallFrame("Just for fun !");   
  64.         } else {   
  65.         }   
  66.     }   
  67.        
  68.     public void centerOnScreen() {   
  69.         Dimension displaySize = getToolkit().getScreenSize();   
  70.         Dimension winSize = getSize();   
  71.         int x = (displaySize.width - winSize.width) / 2;   
  72.         int y = (displaySize.height - winSize.height) / 2;   
  73.         if(x < 0) {   
  74.             x = 0;   
  75.         }   
  76.         if(y < 0) {   
  77.             y = 0;   
  78.         }   
  79.         setLocation(x, y);   
  80.     }   
  81. }   
  82.   
  83. class MyFlowLayout extends FlowLayout {   
  84. }   
  85. class MyGridLayout extends GridLayout {   
  86. }   
  87. class MyBorderLayout extends BorderLayout {   
  88. }   
  89.   
  90. class MySmallFrame extends JFrame {   
  91.     public MySmallFrame(String s) {   
  92.         super();   
  93.         setTitle(s);   
  94.         setBounds(500,200,300,300);   
  95.         JLabel labe = new JLabel();   
  96.         labe.setBounds(20,20,200,500);   
  97.         labe.setForeground(Color.red);   
  98.         labe.setText("Just for fun !");   
  99.         labe.setFont(new Font("", Font.BOLD, 35));   
  100.         getContentPane().add(labe);   
  101.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  102.         setVisible(true);   
  103.     }   
  104. }  

 

原创粉丝点击