awt和swing学习(一)
来源:互联网 发布:网络编辑是什么来的 编辑:程序博客网 时间:2024/05/21 18:44
- import java.util.*;
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class TestLayout {
- public static void main(String[] args) {
- MyFrame mf = new MyFrame();
- }
- }
- class MyFrame extends JFrame implements ActionListener {
- public MyFrame() {
- super();
- this.setTitle("TestMyFrame");
- this.setSize(800, 600);
- this.centerOnScreen();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container p = getContentPane();
- p.setLayout(new BorderLayout());
- JMenuBar jmb = new JMenuBar();
- JMenu[] jm = new JMenu[3];
- JMenuItem[][] jmi = { {new JMenuItem("打开"),new JMenuItem("保存")},
- {new JMenuItem("选择")},
- {new JMenuItem("新窗口")}
- };
- String[] mName = {"文件", "颜色", "窗口"};
- for(int i=0; i<jm.length; i++) {
- jm[i] = new JMenu(mName[i]);
- for(int j=0; j<jmi[i].length; j++) {
- jmi[i][j].addActionListener(this);
- jm[i].add(jmi[i][j]);
- }
- jmb.add(jm[i]);
- }
- this.setJMenuBar(jmb);
- setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand()=="打开") {
- JFileChooser chooseFile = new JFileChooser();
- int returnVal = chooseFile.showOpenDialog(null);
- if(returnVal == chooseFile.APPROVE_OPTION) {
- File f = chooseFile.getSelectedFile();
- JOptionPane.showConfirmDialog(this, "你选择的文件名是:"+chooseFile.getName(f),
- "确认",JOptionPane.ERROR_MESSAGE);
- }
- } else if(e.getActionCommand()=="保存") { //也可以用getsource()方法获得那个变量
- JFileChooser chooseFile = new JFileChooser();
- int returnVal = chooseFile.showSaveDialog(null);
- if(returnVal == chooseFile.APPROVE_OPTION) {
- File f = chooseFile.getSelectedFile();
- JOptionPane.showConfirmDialog(this,chooseFile.getName(f));
- }
- } else if(e.getActionCommand()=="选择") {
- Color tmpColor = JColorChooser.showDialog(this, "调色板", Color.red);
- } else if(e.getActionCommand()=="新窗口") {
- MySmallFrame msf = new MySmallFrame("Just for fun !");
- } else {
- }
- }
- public void centerOnScreen() {
- Dimension displaySize = getToolkit().getScreenSize();
- Dimension winSize = getSize();
- int x = (displaySize.width - winSize.width) / 2;
- int y = (displaySize.height - winSize.height) / 2;
- if(x < 0) {
- x = 0;
- }
- if(y < 0) {
- y = 0;
- }
- setLocation(x, y);
- }
- }
- class MyFlowLayout extends FlowLayout {
- }
- class MyGridLayout extends GridLayout {
- }
- class MyBorderLayout extends BorderLayout {
- }
- class MySmallFrame extends JFrame {
- public MySmallFrame(String s) {
- super();
- setTitle(s);
- setBounds(500,200,300,300);
- JLabel labe = new JLabel();
- labe.setBounds(20,20,200,500);
- labe.setForeground(Color.red);
- labe.setText("Just for fun !");
- labe.setFont(new Font("", Font.BOLD, 35));
- getContentPane().add(labe);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- }
- awt和swing学习(一)
- 学习AWT/Swing编程(一)解决Eclipse运行AWT/Swing项目和组件时中文乱码
- awt和swing学习(二)
- awt和swing学习(三)
- swing awt学习笔记
- AWT、SWT 和 Swing
- awt,swing和applet
- Swing和AWT比较
- Swing和awt
- java awt和swing
- AWT和Swing
- awt和Swing
- AWT和Swing
- AWT和Swing
- Java----AWT组件开发和Swing界面编程(一)
- Java----AWT组件开发和Swing界面编程(一)
- java学习篇(二)---卡片布局详解(Swing和awt)
- AWT和Swing中的绘画
- 存储过程加密
- 第九章输入输出流1-4
- 要学C!
- 工厂设计模式
- 读写文件
- awt和swing学习(一)
- 《疯狂程序员》精典摘录
- 统计一个表的行数的最快的方法
- 4.19
- 关于SQLserver 2000的安装问题
- 如何通过博客挣钱?
- C的阶段总结(一)
- 学习
- 赢在中国-敏捷之旅