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);
}
}
Java GUITest
最新推荐文章于 2021-05-19 10:44:12 发布