?? position.java
字號(hào):
///////////////////////////////////////////////////////////
// Name: Drawer //
// Author:Zhanghan //
// Date: 2005-9-10 //
// Email: zhang_han04@ncic.ac.cn //
///////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Container;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Position extends JPanel
implements ChangeListener {
private JSpinner xSpinner = null;
private JSpinner ySpinner = null;
private JSpinner dSpinner = null;
private DrawingBoard board = null;
private Shape currentShape = null;
//public Shape
public Position(DrawingBoard board) {
super(new SpringLayout());
this.board = board;
currentShape = board.currentShape;
JFormattedTextField ftf = null;
SpinnerModel xModel = new SpinnerNumberModel(currentShape.currentX, //initial value
0, //min
786, //max
1); //step
SpinnerModel yModel = new SpinnerNumberModel(currentShape.currentY, //initial value
0, //min
578, //max
1); //step
SpinnerModel dModel = new SpinnerNumberModel(currentShape.currentD, //initial value
-800, //min
800, //max
1); //step
this.setLayout(new GridLayout(3, 1, 5, 5));
xSpinner = addLabeledSpinner(this, "X: ", xModel);
ySpinner = addLabeledSpinner(this, "Y: ", yModel);
dSpinner = addLabeledSpinner(this, "Diameter: ", dModel);
//Number Format.
xSpinner.setEditor(new JSpinner.NumberEditor(xSpinner, "#"));
ySpinner.setEditor(new JSpinner.NumberEditor(ySpinner, "#"));
dSpinner.setEditor(new JSpinner.NumberEditor(dSpinner, "#"));
//Tweak the spinner's formatted text field.
ftf = getTextField(xSpinner);
if (ftf != null ) {
ftf.setHorizontalAlignment(JTextField.RIGHT);
ftf.setBorder(BorderFactory.createEmptyBorder(1,1,1,3));
}
ftf = getTextField(ySpinner);
if (ftf != null ) {
ftf.setHorizontalAlignment(JTextField.RIGHT);
ftf.setBorder(BorderFactory.createEmptyBorder(1,1,1,3));
}
ftf = getTextField(dSpinner);
if (ftf != null ) {
ftf.setHorizontalAlignment(JTextField.RIGHT);
ftf.setBorder(BorderFactory.createEmptyBorder(1,1,1,3));
}
xSpinner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
ySpinner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
dSpinner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
//Listen for changes on the date spinner.
xSpinner.addChangeListener(this);
ySpinner.addChangeListener(this);
dSpinner.addChangeListener(this);
}
/**
* Return the formatted text field used by the editor, or
* null if the editor doesn't descend from JSpinner.DefaultEditor.
*/
public JFormattedTextField getTextField(JSpinner spinner) {
JComponent editor = spinner.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
return ((JSpinner.DefaultEditor)editor).getTextField();
} else {
System.err.println("Unexpected editor type: "
+ spinner.getEditor().getClass()
+ " isn't a descendant of DefaultEditor");
return null;
}
}
/**
* Required by the ChangeListener interface. Listens for
* changes in the spinners and does something silly in
* response.
*/
public void stateChanged(ChangeEvent e) {
SpinnerModel xModel = xSpinner.getModel();
SpinnerModel yModel = ySpinner.getModel();
SpinnerModel dModel = dSpinner.getModel();
Integer xValue = (Integer)xModel.getValue();
Integer yValue = (Integer)yModel.getValue();
Integer dValue = (Integer)dModel.getValue();
int x = xValue.intValue();
int y = yValue.intValue();
int d = dValue.intValue();
if (xModel instanceof SpinnerNumberModel) board.setCurrentX(x);
if (yModel instanceof SpinnerNumberModel) board.setCurrentY(y);
if (dModel instanceof SpinnerNumberModel) board.setCurrentD(d);
}
//Add Label For Spinner
static protected JSpinner addLabeledSpinner(Container c,
String label,
SpinnerModel model) {
JLabel l = new JLabel(label);
c.add(l);
JSpinner spinner = new JSpinner(model);
l.setLabelFor(spinner);
c.add(spinner);
return spinner;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -