📄 textdialog.java
字号:
/**
* $Id:TextDialog.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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JLabel;
import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.jfdraw.gui.GUIConst;
import com.jfimagine.jfdraw.gui.GUIUtils;
/**
* TextDialog class. A class used to get a new text input in gui by user.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class TextDialog extends JDialog
implements ActionListener {
private static TextDialog m_dialog;
private static String m_textValue = "";
private JTextArea m_textArea;
/**
* modify an object's label
* @param refComp A reference component that used to show a modifier window
* @param initialValue initial text for modify
* @return Valid string if user enter/modified text, empty string if user canceled.
*/
public static String getNewText(Component refComp,String initialValue){
Frame f =GUIUtils.getFrame(refComp);
String newText =getNewText(f,initialValue);
if (newText==null)
return initialValue;
else{
return newText;
}
}
/**
* Open a text modifier 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 text for modify
* @return Valid string if user enter/modified text, empty string if user canceled.
*/
public static String getNewText(Frame modalFrame,String initialValue) {
return getNewText(modalFrame,null,initialValue);
}
/**
* Open a text modifier 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 text for modify
* @return Valid string if user enter/modified text, empty string if user canceled.
*/
public static String getNewText(Frame modalFrame,Component refComp,String initialValue) {
return showDialog(modalFrame,refComp,initialValue);
}
/**
* 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 String showDialog(Component frameComp,
Component locationComp,
String initialValue) {
Frame frame = JOptionPane.getFrameForComponent(frameComp);
m_dialog = new TextDialog(frame,
locationComp,
CADResource.getString("label.text.enterText"),
CADResource.getString("dialog.text"),
initialValue);
m_dialog.setVisible(true);
return m_textValue;
}
private TextDialog(Frame frame,
Component locationComp,
String aLabelText,
String title,
String initialValue) {
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);
//main part of the dialog
m_textArea =new JTextArea(10,10);
m_textArea.setLineWrap(true);
JScrollPane textScroller = new JScrollPane(m_textArea);
textScroller.setPreferredSize(new Dimension(250, 120));
textScroller.setAlignmentX(LEFT_ALIGNMENT);
//Create a container so that we can add a title around
//the scroll pane. Can't add a title directly to the
//scroll pane because its background would be white.
//Lay out the aLabel and scroll pane from top to bottom.
JPanel textPane = new JPanel();
textPane.setLayout(new BoxLayout(textPane, BoxLayout.Y_AXIS));
JLabel aLabel = new JLabel(aLabelText);
aLabel.setFont(GUIConst.FONT_LABEL);
textPane.add(aLabel);
textPane.add(Box.createRigidArea(new Dimension(0,5)));
textPane.add(textScroller);
textPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//Lay out the buttons from left to right.
JPanel buttonPane = new JPanel();
//buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
buttonPane.add(Box.createHorizontalGlue());
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(textPane, BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.SOUTH);
//Initialize values.
m_textArea.setText(initialValue);
pack();
setLocationRelativeTo(locationComp);
}
//Handle clicks on the Set and Cancel buttons.
public void actionPerformed(ActionEvent e) {
if ("Confirm".equals(e.getActionCommand())) {
m_textValue = (String)(m_textArea.getText());
if (m_textValue==null || m_textValue.length()==0){
int n = JOptionPane.showConfirmDialog(
TextDialog.this,
CADResource.getString("dialog.text.emptyText"),
CADResource.getString("sys.warn"),
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
m_textValue ="";
} else{
m_textArea.requestFocus();
return;
}
}
}else{
m_textValue =null;
}
m_dialog.setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -