📄 inputboxdialog.java
字号:
/**
* $Id:InputBoxDialog.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfdraw.gui.dialog;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.utils.commonutil.CommonUtil;
import com.jfimagine.jfdraw.gui.GUIConst;
/**
* InputBoxDialog class. A class used to get a new text or number input.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class InputBoxDialog extends JDialog
implements ActionListener {
private static InputBoxDialog m_dialog;
private static String m_value="";
private static JTextField m_inputTextField;
private static boolean m_textChanged=false;
/**
* After a valid calling of inputText method, just call this method
* to get a string input.
*/
public static String getString(){
return m_value;
}
/**
* After a valid calling of inputText method, just call this method
* to get a integer input.
*/
public static int getIntValue(){
return CommonUtil.s2i(m_value);
}
/**
* After a valid calling of inputText method, just call this method
* to get a double input.
*/
public static double getDoubleValue(){
return CommonUtil.s2f(m_value);
}
/**
* Open a window, to input/modify a new text
*
* @param modalFrame Determines which frame the dialog depends on; it should be
* a component in the dialog's controlling frame.
*
* @param initialValue initial value for modify
* @return A new zoom scale.
*/
public static boolean inputText(Frame modalFrame,String initialValue) {
return inputText(modalFrame,null,initialValue);
}
/**
* Open a window, to input/modify a new text
*
* @param modalFrame Determines which frame the dialog depends on; it should be
* a component in the dialog's controlling frame.
*
* @param initialValue initial value for modify
* @return A new zoom scale.
*/
public static boolean inputText(Frame modalFrame,String initialValue,String title,String iLabel) {
return showDialog(modalFrame,null,initialValue,title,iLabel);
}
/**
* Open a window, to input/modify a new text
*
* @param modalFrame Determines which frame the dialog depends on; it should be
* a component in the dialog's controlling frame.
*
* @param refComp This should be null if you want the dialog
* to come up with its left corner in the center of the screen;
* otherwise, it should be the component on top of which the
* dialog should appear.
*
* @param initialValue initial value for modify
* @return Valid string if user enter/modified text, empty string if user canceled.
*/
public static boolean inputText(Frame modalFrame,Component refComp,String initialValue) {
return showDialog(modalFrame,refComp,initialValue,CADResource.getString("label.text.inputtext"),CADResource.getString("label.text.entertexthere"));
}
/**
* Set up and show the dialog. The first Component argument
* determines which frame the dialog depends on; it should be
* a component in the dialog's controlling frame. The second
* Component argument should be null if you want the dialog
* to come up with its left corner in the center of the screen;
* otherwise, it should be the component on top of which the
* dialog should appear.
*/
private static boolean showDialog(Component frameComp,
Component locationComp,
String initialValue,
String title,
String iLabel
) {
Frame frame = JOptionPane.getFrameForComponent(frameComp);
m_dialog = new InputBoxDialog(frame,
locationComp,
initialValue,
title,
iLabel);
m_dialog.setVisible(true);
return m_textChanged;
}
private InputBoxDialog(Frame frame,
Component locationComp,
String initialValue,
String title,
String iLabel) {
super(frame, title, true);
setResizable(false);
//Create and initialize the buttons.
JButton cancelButton = new JButton(CADResource.getString("button.cancel"));
cancelButton.setFont(GUIConst.FONT_BUTTON);
cancelButton.addActionListener(this);
//
final JButton confirmButton = new JButton(CADResource.getString("button.confirm"));
confirmButton.setFont(GUIConst.FONT_BUTTON);
confirmButton.setActionCommand("Confirm");
confirmButton.addActionListener(this);
getRootPane().setDefaultButton(confirmButton);
m_value =initialValue;
JLabel textLabel =new JLabel(iLabel);
textLabel.setFont(GUIConst.FONT_LABEL);
textLabel.setPreferredSize(new Dimension(100,25));
m_inputTextField =new JTextField();
m_inputTextField.setFont(GUIConst.FONT_LABEL);
m_inputTextField.setText(initialValue);
m_inputTextField.setPreferredSize(new Dimension(50, 25));
//a top panel for placing zoom label, zoom list and zoom percent label.
JPanel textPanel = new JPanel();
textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
textPanel.add(textLabel);
textPanel.add(Box.createRigidArea(new Dimension(0,10)));
textPanel.add(m_inputTextField);
textPanel.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));
//Lay out the buttons from left to right.
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
buttonPane.add(Box.createRigidArea(new Dimension(20, 0)));
buttonPane.add(confirmButton);
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPane.add(cancelButton);
//Put everything together, using the content pane's BorderLayout.
Container contentPane = getContentPane();
contentPane.add(textPanel, BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(locationComp);
}
//Handle clicks on the Set and Cancel buttons.
public void actionPerformed(ActionEvent e) {
if ("Confirm".equals(e.getActionCommand())) {
m_value =m_inputTextField.getText();
if (m_value==null || m_value.equals("")){
JOptionPane.showMessageDialog(null, CADResource.getString("group.invalidShape"), CADResource.getString("dialog.text.emptyText.refused"), JOptionPane.ERROR_MESSAGE);
return;
}
m_textChanged =true;
}else{
m_textChanged =false;
}
m_dialog.setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -