Java 为两个JTextArea设置事件侦听器';s
我正在开发一个图像处理软件(只是为了好玩),它的一个功能是图像大小调整选项。基本上会弹出一个窗口,其中包含两个Java 为两个JTextArea设置事件侦听器';s,java,swing,jtextarea,event-listener,Java,Swing,Jtextarea,Event Listener,我正在开发一个图像处理软件(只是为了好玩),它的一个功能是图像大小调整选项。基本上会弹出一个窗口,其中包含两个JTextArea组件,以获得所需的图像宽度和高度,以便调整大小。如果用户需要,还可以使用JCheckBox来保持纵横比。问题是。当选中复选框并且用户假定首先输入宽度或高度时。我希望另一个文本区域在每次进行更改时都会相应地更新自己,以便保留AR。我已经开发了一些代码来处理这个问题,但它没有提供我真正想要的内容,因为我不了解应该将哪个侦听器分配给哪个组件 代码: String height
JTextArea
组件,以获得所需的图像宽度和高度,以便调整大小。如果用户需要,还可以使用JCheckBox
来保持纵横比。问题是。当选中复选框并且用户假定首先输入宽度或高度时。我希望另一个文本区域在每次进行更改时都会相应地更新自己,以便保留AR。我已经开发了一些代码来处理这个问题,但它没有提供我真正想要的内容,因为我不了解应该将哪个侦听器分配给哪个组件
代码:
String height, width;
if (checkBoxImage.isSelected()){
// aspect ratio = width / height
width = widthArea.getText();
height = heightArea.getText();
double aspectRatio = (double) images.get(tabbedPane.getSelectedIndex()).getWidth() / images.get(tabbedPane.getSelectedIndex()).getHeight();
/**
* to do, update width, height area
* to the closest user input
*/
if(heightArea.getText().length() != 0 && heightArea.getText().length() <= 5
&& heightArea.getText().charAt(0) != '0'){
//parsing string to integer
try{
int heightNum = Integer.parseInt(height);
int widthNum = (int) Math.round(aspectRatio * heightNum);
widthArea.setText(String.valueOf(widthNum) );
widthArea.updateUI();
frameimgSize.repaint();
}
catch(NumberFormatException e1){JOptionPane.showMessageDialog(error,e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);}
}
//width has been entered first
else if(widthArea.getText().length() != 0 && widthArea.getText().length() <= 5 &&
widthArea.getText().charAt(0) != '0'){
try{
int widthNum = Integer.parseInt(width);
int heightNum = (int) Math.round(aspectRatio * widthNum);
heightArea.setText(String.valueOf(heightNum) );
heightArea.updateUI();
frameimgSize.repaint();
}
catch(NumberFormatException e1){JOptionPane.showMessageDialog(error,e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);}
}
}
字符串高度、宽度;
if(checkBoxImage.isSelected()){
//纵横比=宽度/高度
width=widthArea.getText();
height=heightArea.getText();
double aspectRatio=(double)images.get(tabbedPane.getSelectedIndex()).getWidth()/images.get(tabbedPane.getSelectedIndex()).getHeight();
/**
*要执行此操作,请更新宽度、高度区域
*到最近的用户输入
*/
如果(heightArea.getText().length()!=0&&heightArea.getText().length()首先,我不会使用JTextArea
,它是用于自由形式的文本编辑(想想记事本)。相反,你至少应该使用JTextField
,但是JSpinner
实际上可能更好
请查看以了解更多详细信息
本质上,对于JTextField
,可以使用和/或来监视字段的更改
此侦听器往往会在事件发生后收到通知,也就是说,只有在用户完成编辑字段后才会收到通知。如果您想要实时反馈,您可以使用一个,它将在每次修改字段的基础文档时实时通知
JSpinner
稍微复杂一点,因为它是一个包含编辑器和控件的组件。您可以使用,它将在提交对字段模型的更改时发出通知。这将取代前面提到的ActionListener
和FocusListener
,因此您应该只需要一次侦听呃,但不会提供实时反馈(至少,不是没有更多的工作)首先,我不会使用JTextArea
,它是用于自由形式的文本编辑(想想记事本)。相反,你至少应该使用JTextField
,但是JSpinner
实际上可能更好
请查看以了解更多详细信息
本质上,对于JTextField
,可以使用和/或来监视字段的更改
此侦听器往往会在事件发生后收到通知,也就是说,只有在用户完成编辑字段后才会收到通知。如果您想要实时反馈,您可以使用一个,它将在每次修改字段的基础文档时实时通知
JSpinner
稍微复杂一点,因为它是一个包含编辑器和控件的组件。您可以使用,它将在提交对字段模型的更改时发出通知。这将取代前面提到的ActionListener
和FocusListener
,因此您应该只需要一次侦听呃,但不会提供实时反馈(至少,如果没有更多的工作就不会)在宽度和高度字段中使用非数字值是否有效
如果不是,则使用JSpinners
或JFormattedTextFields
而不是JTextFields
。如果是这样,(例如,您允许输入“单位”以及宽度和高度)您应该在JTextFields中附加一个DocumentListener
,以监视对底层文本文档内容的更改。以下是一个示例:
widthField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
update();
}
public void removeUpdate(DocumentEvent e) {
update();
}
public void insertUpdate(DocumentEvent e) {
update();
}
// your method that handles any Document change event
public void update() {
if( aspectCheckBox1.isSelected() ) {
// parse the width and height,
// constrain the height to the aspect ratio and update it here
}
}
});
然后将类似的DocumentListener添加到heightTextField
请注意,如果使用JTextFields,则需要解析其内容,在用户输入无效数值的情况下,读取单位(如果适用)并处理NumberFormatException
要回答有关在何处添加处理程序的问题
当高度GUI元素发生文档更改时,应更新宽度。同样,宽度GUI元素发生文档更改时,应更新高度
您需要优雅地处理被零除的错误(或将输入限制为始终大于0),使用double执行计算,最好使用Math.round()
来获得最佳整数值以保留纵横比
即:
最重要的是避免为作业使用错误的输入类型:
- 适用于多行文字
- 适用于单行文本
- 适用于约束为特定格式的文本
- 适合数字输入
希望对您有所帮助。在宽度和高度字段中使用非数字值是否有效
如果不是,则使用JSpinners
或JFormattedTextFields
而不是JTextFields
。如果是这样,(例如,您允许输入“单位”以及宽度和高度)您应该在JTextFields中附加一个DocumentListener
,以监视对底层文本文档内容的更改。以下是一个示例:
widthField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
update();
}
public void removeUpdate(DocumentEvent e) {
update();
}
public void insertUpdate(DocumentEvent e) {
update();
}
// your method that handles any Document change event
public void update() {
if( aspectCheckBox1.isSelected() ) {
// parse the width and height,
// constrain the height to the aspect ratio and update it here
}
}
});
然后将类似的DocumentListener添加到heightTextField
请注意,如果使用JTextFields,则需要解析其内容,在用户输入无效数值的情况下,读取单位(如果适用)并处理NumberFormatException
要回答有关在何处添加处理程序的问题
当高度GUI元素发生文档更改时,应更新宽度
private double aspect = 1.0;
aspectCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
preserveAspectActionPerformed(evt);
}
});
private void preserveAspectActionPerformed(java.awt.event.ActionEvent evt) {
try {
double w = Double.parseDouble(widthField.getText());
double h = Double.parseDouble(heightField.getText());
aspect = w / h;
}
catch(NumberFormatException ex) {
// ... error occurred due to non-numeric input
// (use a JSpinner or JFormattedTextField to avoid this)
}
}