我有這段代碼,它使用 JTextField 物件的陣列來處理 25 個元素,我希望在更改或添加文本后,任何元素都將由 KeyListener (keyPressed) 以不同方式處理。
那么如何將更改欄位的索引獲取到偵聽器中呢?
對于存在,第 10 個元素將在用戶添加或更改文本后將欄位的背景更改為綠色。
public JTextField field[];
public void TextFilling(){
field = new JTextField[25]; Font font = new Font("Times new Roman", Font.BOLD ,30);
textChangedListener listener = new textChangedListener();
//There are the process of formating size, location and other settings of the elements:
for (int i = 0, j=0, k=0; i< field.length; i , k ) {field[i] = new JTextField();
if (k==j) field[i].setText("1");
if (i % 5 == 0 && i!=0) {j ; k = 0;}
field[i].setBounds(5 (100*(k 1)), 5 (100*(j 1)), 95, 95);
field[i].setHorizontalAlignment(JTextField.CENTER); field[i].setFont(font);
frame.add(field[i]);
field[i].addKeyListener(listener);
}
}
class textChangedListener implements KeyListener
{
public void keyPressed(KeyEvent e){
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
uj5u.com熱心網友回復:
不要KeyListener在文本組件上使用,它沒有幫助,您可能最終處于狀態突變的中間。
相反,使用 aDocumentListener來監視對文本欄位的更改,請參閱如何撰寫檔案偵聽器以獲取更多詳細資訊。
您還應該使用依賴注入,基本上將資訊傳遞給需要與您的程式互動的類。
看:
- 代碼示例的依賴注入
- 什么是依賴注入?
- 關于設計模式:依賴注入
- 依賴注入快速介紹:它是什么以及何時使用它
- 依賴注入
- 將資訊傳遞給方法或建構式
了解更多資訊。
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<JTextField> textFields;
public TestPane() {
setBorder(new EmptyBorder(64, 64, 64, 64));
setLayout(new GridBagLayout());
Font font = new Font("Times new Roman", Font.BOLD, 30);
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 75;
gbc.ipady = 75;
gbc.insets = new Insets(8, 8, 8, 8);
for (int row = 0; row < 5; row ) {
for (int col = 0; col < 5; col ) {
gbc.gridx = col;
gbc.gridy = row;
JTextField field = new JTextField(1);
field.setHorizontalAlignment(JTextField.CENTER);
field.setFont(font);
textFields.add(field);
if (row == col) {
field.setText("1");
}
int index = ((row * 5) col) 1;
if (index % 10 == 0) {
field.getDocument().addDocumentListener(new DocumentHighlighter(field));
}
add(field, gbc);
}
}
}
public class DocumentHighlighter implements DocumentListener {
private JTextField field;
private Color defaultColor;
public DocumentHighlighter(JTextField field) {
this.field = field;
defaultColor = field.getBackground();
}
protected void highlightFieldIfRequired() {
if (field.getText().isEmpty()) {
field.setBackground(defaultColor);
} else {
field.setBackground(Color.GREEN);
}
}
@Override
public void insertUpdate(DocumentEvent e) {
highlightFieldIfRequired();
}
@Override
public void removeUpdate(DocumentEvent e) {
highlightFieldIfRequired();
}
@Override
public void changedUpdate(DocumentEvent e) {
highlightFieldIfRequired();
}
}
}
}
我能看到的一件事會讓你大吃一驚,那就是你如何共享和管理資料。
One concept you want to spend some time looking into, is the way you decouple your responsibilities. One of the ways this can be done is via a "model". A model is responsible for managing the state and rules associated with the data. The view then becomes responsible for presenting the current state of the model and updating the model based on the user input.
See:
- Model–view–controller
- The Model View Controller Pattern – MVC Architecture and Frameworks Explained
- MVC: Model, View, Controller
One of the key elements to this concept is the use of the observer pattern. You've already seen this at work, as Swing listeners are observers.
Oh, and you really, really want to take the time and learn and understand the layout manager API. See Laying Out Components Within a Container for more details
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450063.html
上一篇:JavaSwing-定時器的運行速度不能超過15毫秒
下一篇:這個鍵系結我做錯了什么?
