总结一下我在java swing 编程实践中的一些经验以及要注意的问题
(1)如何在启动swing界面时指定组件(文本框)聚焦呢?
范例:
CreateMD5App frame = new CreateMD5App();
frame.setVisible(true);
frame.pathTF.requestFocus();
注意:requestFocus();必须放在this.setVisible(true);之后
(2)如何响应文本框的输入事件(有输入操作时触发的事件)
titleTF的类型是JTextArea
titleTF.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
// System.out.println("remove");
try {
checkTitle(titleTF);
} catch (Exception e1) {
e1.printStackTrace();
}
}
@Override
public void insertUpdate(DocumentEvent e) {
// System.out.println("insert");
try {
checkTitle(titleTF);
} catch (Exception e1) {
e1.printStackTrace();
}
}
@Override
public void changedUpdate(DocumentEvent e) {
// System.out.println("change");
}
});
(3)panel增加组件时,出现水平滚动条,而不是竖直滚动条。要求出现竖直滚动条,怎么办?
要求的效果:
实现方法:
otherRequestArgPane = new JPanel();// must be in front of
// getAddNextButton() method
otherRequestArgPane.setAutoscrolls(true);
// 让panel 只显示竖直滚动条
otherRequestArgPane.setLayout(new ModifiedFlowLayout());
otherRequestArgJsPane = new JScrollPane(otherRequestArgPane);
otherRequestArgJsPane.getHorizontalScrollBar().setAutoscrolls(false);
otherRequestArgJsPane.getVerticalScrollBar().setAutoscrolls(true);
说明:把JPanel放在JScrollPane中。
ModifiedFlowLayout 是自定义的布局管理器,代码如下:
package com.swing.component;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
public class ModifiedFlowLayout extends FlowLayout {
private static final long serialVersionUID = 5426056229476897767L;
public ModifiedFlowLayout() {
super();
}
public ModifiedFlowLayout(int align) {
super(align);
}
public ModifiedFlowLayout(int align, int hgap, int vgap) {
super(align, hgap, vgap);
}
public Dimension minimumLayoutSize(Container target) {
// Size of largest component, so we can resize it in
// either direction with something like a split-pane.
return computeMinSize(target);
}
public Dimension preferredLayoutSize(Container target) {
return computeSize(target);
}
private Dimension computeSize(Container target) {
synchronized (target.getTreeLock()) {
int hgap = getHgap();
int vgap = getVgap();
int w = target.getWidth();
// Let this behave like a regular FlowLayout (single row)
// if the container hasn't been assigned any size yet
if (w == 0) {
w = Integer.MAX_VALUE;
}
Insets insets = target.getInsets();
if (insets == null) {
insets = new Insets(0, 0, 0, 0);
}
int reqdWidth = 0;
int maxwidth = w - (insets.left + insets.right + hgap * 2);
int n = target.getComponentCount();
int x = 0;
int y = insets.top + vgap;// FlowLayout starts by adding vgap, so do
// that here too.
int rowHeight = 0;
for (int i = 0; i < n; i++) {
Component c = target.getComponent(i);
if (c.isVisible()) {
Dimension d = c.getPreferredSize();
if ((x == 0) || ((x + d.width) <= maxwidth)) {
// fits in current row.
if (x > 0) {
x += hgap;
}
x += d.width;
rowHeight = Math.max(rowHeight, d.height);
} else {
// Start of new row
x = d.width;
y += vgap + rowHeight;
rowHeight = d.height;
}
reqdWidth = Math.max(reqdWidth, x);
}
}
y += rowHeight;
y += insets.bottom;
return new Dimension(reqdWidth + insets.left + insets.right, y);
}
}
private Dimension computeMinSize(Container target) {
synchronized (target.getTreeLock()) {
int minx = Integer.MAX_VALUE;
int miny = Integer.MIN_VALUE;
boolean found_one = false;
int n = target.getComponentCount();
for (int i = 0; i < n; i++) {
Component c = target.getComponent(i);
if (c.isVisible()) {
found_one = true;
Dimension d = c.getPreferredSize();
minx = Math.min(minx, d.width);
miny = Math.min(miny, d.height);
}
}
if (found_one) {
return new Dimension(minx, miny);
}
return new Dimension(0, 0);
}
}
}
(4)如何获取下拉框中选中的内容(不是index)
下拉框类型是JComboBox,初始化下拉框:
encodingComboBox = new JComboBox();
// 设置下拉框中的选项
for (int i = 0; i < SystemUtil.CHARSET_ARRAY.length; i++) {
String charSet = SystemUtil.CHARSET_ARRAY[i];
encodingComboBox.addItem(charSet);
}
获取下拉框选中的内容:
String charset = (String) encodingComboBox.getSelectedItem();
界面如下: