zoukankan      html  css  js  c++  java
  • 利用JPanel类 JFrame JButton 以及布局管理类实现的一个简单布局界面


    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JButton;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.FlowLayout;

    public class Outer extends JFrame implements ActionListener{
     /**
      *
      */
     private static final long serialVersionUID = 1L;
     public static final int WIDTH = 300;
     public static final int HEIGTH = 200;
     private JPanel redPanel;
     private JPanel whitePanel;
     private JPanel bluePanel;
     
     public static void main(String []args)
     { 
      Outer gui = new Outer();
      gui.setVisible(true);
     }
     public Outer()
     {
      super("Panel Demonstration");
      this.setSize(WIDTH,HEIGTH);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLayout(new BorderLayout());
      
      JPanel biggerPanel = new JPanel();
      biggerPanel.setLayout(new GridLayout(1,3));
      
      redPanel = new JPanel();
      redPanel.setBackground(Color.LIGHT_GRAY);
      biggerPanel.add(redPanel);
      
      whitePanel = new JPanel();
      whitePanel.setBackground(Color.LIGHT_GRAY);
      biggerPanel.add(whitePanel);
      
      bluePanel = new JPanel();
      bluePanel.setBackground(Color.LIGHT_GRAY);
      biggerPanel.add(bluePanel);
      
      this.add(biggerPanel,BorderLayout.CENTER);
      JPanel buttonPanel = new JPanel();
      buttonPanel.setBackground(Color.LIGHT_GRAY);
      buttonPanel.setLayout(new FlowLayout());
      
      JButton redButton = new JButton("Red");
      redButton.setBackground(Color.RED);
      redButton.addActionListener(this);
      buttonPanel.add(redButton);
      
      JButton whiteButton = new JButton("White");
      whiteButton.setBackground(Color.WHITE);
      whiteButton.addActionListener(this);
      buttonPanel.add(whiteButton);
      
      JButton blueButton = new JButton("Blue");
      blueButton.setBackground(Color.BLUE);
      blueButton.addActionListener(this);
      buttonPanel.add(blueButton);
      
      this.add(buttonPanel,BorderLayout.SOUTH);
     }
     public void actionPerformed(ActionEvent e)
     {
      String str = e.getActionCommand();
      
      if(str.equals("Red"))
       redPanel.setBackground(Color.RED);
      else if(str.equals("White"))
       whitePanel.setBackground(Color.WHITE);
      else if(str.equals("Blue"))
       bluePanel.setBackground(Color.BLUE);
     }
    }

  • 相关阅读:
    【转】最奇特的编程语言特征
    【原】mysql 视图
    自己动手写ORM框架(一):目标效果预览
    (二)Javascript面向对象:命名空间
    在JAVA中封装JSONUtils工具类及使用(一)
    .NET3.5中JSON用法以及封装JsonUtils工具类(二)
    (四)Javascript面向对象:继承
    自己动手写ORM框架(三):关系映射配置—Table属性
    (一)javascript面向对象:(2)类
    自己动手写ORM框架(二):AdoHelper支持多数据库操作的封装(1)
  • 原文地址:https://www.cnblogs.com/xuxu8511/p/2498175.html
Copyright © 2011-2022 走看看