JavaSwing开发实用技巧与案例
立即解锁
发布时间: 2025-08-18 02:24:37 阅读量: 1 订阅数: 13 

### Java Swing开发实用技巧与案例
在Java Swing开发中,有许多实用的技巧和案例可以帮助我们创建出功能丰富、界面美观的应用程序。下面将详细介绍一些常见问题的解决方案。
#### 1. 关闭JDialog对话框
在模拟密码更改的对话框中,当点击“更改密码”按钮时,需要关闭对话框。可以使用以下代码实现:
```java
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
```
上述代码中,通过设置对话框的可见性为`false`,并调用`dispose()`方法释放对话框资源,从而关闭对话框。
#### 2. 关联文档监听器实现自动完成功能
在开发字典应用时,为了实现自动完成功能,可以使用Java Swing的`DocumentListener`和`Document API`。以下是具体的实现步骤:
1. 创建一个实现`DocumentListener`接口的类`MyDocumentListener`,并实现`insertUpdate()`、`removeUpdate()`和`changedUpdate()`方法。
```java
class MyDocumentListener implements DocumentListener {
private static String[] dictionary = {"apress",
"caitlin", "car", "carl", "cat", "cathode",
"bat", "batter", "barney",
"fred", "fredrick",
"gillian", "goose",
"java", "javafx",
"swan", "swing"};
private JList listBox;
MyDocumentListener(JList listBox) {
this.listBox = listBox;
}
String newline = "\n";
public void insertUpdate(DocumentEvent e) {
searchDictionary(e);
}
public void removeUpdate(DocumentEvent e) {
searchDictionary(e);
}
public void changedUpdate(DocumentEvent e) {
System.out.println("change: " + e);
}
public void searchDictionary(DocumentEvent e) {
try {
Document doc = (Document) e.getDocument();
String text = doc.getText(0, doc.getLength());
DefaultListModel dlm = (DefaultListModel) listBox.getModel();
dlm.removeAllElements();
if (text != null && text.length() > 0) {
for (String word : dictionary) {
if (word.startsWith(text)) {
dlm.addElement(word);
}
}
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
```
2. 创建一个包含`JList`和`JTextField`的面板`AddingListenerToDocument`,并将`MyDocumentListener`添加到`JTextField`的文档监听器中。
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.java7recipes.chapter14.SimpleAppLauncher;
public class AddingListenerToDocument extends JPanel {
public AddingListenerToDocument() {
setLayout(new BorderLayout());
DefaultListModel<String> listModel = new DefaultListModel<>();
JList people = new JList(listModel);
add(people, BorderLayout.CENTER);
JTextField searchFld = new JTextField();
searchFld.getDocument().addDocumentListener(new MyDocumentListener(people));
add(searchFld, BorderLayout.NORTH);
}
public static void main(String[] args) {
final JPanel c = new AddingListenerToDocument();
c.setPreferredSize(new Dimension(379, 200));
SimpleAppLauncher.launch("Chapter 14-19 Adding Listener to Document", c);
}
}
```
通过以上步骤,当用户在`JTextField`中输入字符时,`MyDocumentListener`会监听文档事件,并根据输入的字符从字典中筛选出匹配的单词,显示在`JList`中。
#### 3. 使用HTML格式化GUI应用程序
在Swing的标签和按钮组件中,可以使用基本的HTML来格式化文本。以下是一个示例代码:
```java
import java.awt.*;
import javax.swing.*;
import org.java7recipes.chapter14.CellConstraint;
import org.java7recipes.chapter14.CustomGridLayout;
import org.java7recipes.chapter14.SimpleAppLauncher;
public class FormattingGuiWithHtml extends JPanel {
public FormattingGuiWithHtml() {
JLabel label1 = new JLabel("<html><center><b>Label 1</b><br>"
+ "<font color=#7f7fdd>Bold</font>");
JLabel label2 = new JLabel("<html><center><i>Label 2</i><br>"
+ "<font color=#7f7fdd>Italic</font>");
JLabel label3 = new JLabel("<html><center><font size=+4>Label 3</font><br>"
+ "<font color=#7f7fdd>Larger</font>");
JButton button1 = new JButton("<html><center><b><u>Button 1</u></b><br>"
+ "<font color=#7f7fdd>underline</font>");
JButton button2 = new JButton("<html><font color=blue>Button 2</font><br>"
+ "<font color=#7f7fdd>Blue Left</font>");
JButton button3 = new JButton("<html>Bu<sub>tt</sub>on 3<br>"
+ "<font co
```
0
0
复制全文