Java GUITest

package qs.javase.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyGUI extends JPanel implements ActionListener {
  public MyGUI() {
    buildGUI();
    buildLayout();
    drawButton.addActionListener(this);
  }

  // build all the objects
  private void buildGUI() {
    
    canvas = new ACanvas();
    canvas.setBackground(Color.white);
    canvas.setPreferredSize(new Dimension(800, 800));

    shapeBox = new JComboBox(new String[] { "Circle", "Square" });
    colorBox = new JComboBox(new String[] { "red", "blue", "green" });
    
    xCoor = new JTextField(3);
    yCoor = new JTextField(3);

    ButtonGroup buttonGroup = new ButtonGroup();
    smallPic = new JCheckBox("Small", false);
    mediumPic = new JCheckBox("Medium", true);
    largePic = new JCheckBox("Large", false);
    buttonGroup.add(smallPic);
    buttonGroup.add(mediumPic);
    buttonGroup.add(largePic);

    fillBox = new JCheckBox("Fill");
    fillBox.setSelected(false);

    drawButton = new JButton("Draw");

    message = new JTextField(25);
    message.setEditable(false);
  }

  // Layout all the objects
  private void buildLayout() {
	  
	  JPanel topHalf    = new JPanel( );
	  JPanel bottomHalf = new JPanel( );
    // Layout the top half
    topHalf.setLayout(new FlowLayout());
    topHalf.add(canvas);
    // Layout the bottom half
    bottomHalf.setLayout(new FlowLayout());
    bottomHalf.add(new JLabel("Shape"));
    bottomHalf.add(shapeBox);
    bottomHalf.add(new JLabel("Color"));
    bottomHalf.add(colorBox);
    bottomHalf.add(new JLabel("X"));
    bottomHalf.add(xCoor);
    bottomHalf.add(new JLabel("Y"));
    bottomHalf.add(yCoor);
    bottomHalf.add(smallPic);
    bottomHalf.add(mediumPic);
    bottomHalf.add(largePic);
    bottomHalf.add(fillBox);
    bottomHalf.add(drawButton);
    bottomHalf.add(message);

    // Now layout GUI
    setLayout( new BorderLayout( ) );
    add( topHalf, "North" );
    add( bottomHalf, "South" );
  }

  public void actionPerformed(ActionEvent event) {
    try {
      canvas.setParams(
          (String)shapeBox.getSelectedItem(),
          (String)colorBox.getSelectedItem(),
          Integer.parseInt(xCoor.getText()), Integer.parseInt(yCoor.getText()),
          smallPic.isSelected() ? 0 : mediumPic.isSelected() ? 1 : 2,
          fillBox.isSelected());
      message.setText("");
    } catch (NumberFormatException e) {
      message.setText("Incomplete input");
    }
  }

  private ACanvas canvas;
  private JComboBox shapeBox;
  private JComboBox colorBox;
//  private JList colorList;
  private JTextField xCoor;
  private JTextField yCoor;
  private JCheckBox smallPic;
  private JCheckBox mediumPic;
  private JCheckBox largePic;
  private JCheckBox fillBox;
  private JButton drawButton;
  private JTextField message;

  class ACanvas extends JPanel {

    private String shape = "";
    private String color = "";
    private int xcoor;
    private int ycoor;
    private int size; // 0 = small, 1 = med, 2 = large
    private boolean fillOn;
    private int width;

    public void setParams(String aShape, String aColor, int x, int y,
        int canvasSize, boolean fill) {
      this.shape = aShape;
      this.color = aColor;
      xcoor = x;
      ycoor = y;
      size = canvasSize;
      fillOn = fill;
      repaint();
    }

    public void update(Graphics g) {
      System.out.println("Update called");
    }

    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (color.equals("red"))
        g.setColor(Color.red);
      else if (color.equals("blue"))
        g.setColor(Color.blue);
      else if (color.equals("green"))
        g.setColor(Color.green);

      width = 200 * (size + 1);

      if (shape.equals("Square"))
        if (fillOn)
          g.fillRect(xcoor, ycoor, width, width);
        else
          g.drawRect(xcoor, ycoor, width, width);

      else if (shape.equals("Circle"))
        if (fillOn)
          g.fillOval(xcoor, ycoor, width, width);
        else
          g.drawOval(xcoor, ycoor, width, width);
    }
  }
}

public class Test extends JFrame {

  public Test() {
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent event) {
        System.exit(0);
      }

    });
  }

  public static void main(String[] args) {
    JFrame frame = new Test();
    frame.setTitle("GUITest");

    Container contentPane = frame.getContentPane();

    contentPane.add(new MyGUI());

    frame.pack();
    frame.setVisible(true);
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值