- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的第一个数据库模拟器 GUI,我很难使用 TAB 浏览用户输入文本字段。我的愿望是按以下顺序导航:从姓名到第一个姓氏,到第二个姓氏,到年龄,到电话号码,到地址,最后到电子邮件。老实说,我只是复制了自定义焦点遍历策略的代码,并希望它能起作用,但事实并非如此。我不知道我是否应该在 Listener 类上调用它,还是这是多余的?
这是代码,很抱歉它太长了=S(省略了库以符合帖子最大长度):
公共(public)类 UserFormNew {
private JTextField idtf;
private JTextField nametf;
private JTextField ln1tf;
private JTextField ln2tf;
private JTextField agetf;
private JTextField pntf;
private JTextField addresstf;
private JTextField emtf;
private JTable t;
private DefaultTableModel dtm;
private JTableHeader header;
private int idCount = 0;
private int rowSelected;
private boolean ed = false;
private String[] selRowInfoGotten;
private List<Component> compList = new ArrayList<Component>();
private JPanel dataIn;
public UserFormNew() {
// Containers
JFrame mainF = new JFrame();
JPanel mainP = new JPanel(new BorderLayout(10, 5));
dataIn = new JPanel(new GridBagLayout());
JPanel table = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
JPanel buttons = new JPanel(new BorderLayout(20, 10));
// Fonts
Font plain = new Font("Tahoma", Font.PLAIN, 12);
Font plaint = new Font("Tahoma", Font.PLAIN, 11);
Font bold12 = new Font("Tahoma", Font.BOLD, 12);
// Data-In
GridBagConstraints gbc = new GridBagConstraints();
Insets topLeftLabel = new Insets(20, 20, 2, 2);
Insets midLeftLabel = new Insets(2, 20, 2, 2);
Insets botLeftLabel = new Insets(2, 20, 20, 2);
Insets topMidTF = new Insets(20, 2, 2, 20);
Insets midMidTF = new Insets(2, 2, 2, 20);
Insets botMidTF = new Insets(2, 2, 20, 20);
Insets topMidLabel = new Insets(20, 30, 2, 2);
Insets midMidLabel = new Insets(2, 30, 2, 2);
Insets botMidLabel = new Insets(2, 30, 20, 2);
Insets topRightInsets = new Insets(20, 2, 2, 20);
Insets midRightInsets = new Insets(2, 2, 2, 20);
Insets botRightInsets = new Insets(2, 2, 10, 20);
JLabel id = new JLabel("Id");
id.setFont(bold12);
gbc.insets = topLeftLabel;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(id, gbc);
JLabel name = new JLabel("Name");
name.setFont(bold12);
gbc.insets = midLeftLabel;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(name, gbc);
JLabel ln1 = new JLabel("1\u00B0 Lastname");
ln1.setFont(bold12);
gbc.insets = midLeftLabel;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(ln1, gbc);
JLabel ln2 = new JLabel("2\u00B0 Lastname");
ln2.setFont(bold12);
gbc.insets = botLeftLabel;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(ln2, gbc);
JLabel age = new JLabel("Age");
age.setFont(bold12);
gbc.insets = topMidLabel;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(age, gbc);
JLabel pn = new JLabel("Phone Number");
pn.setFont(bold12);
gbc.insets = midMidLabel;
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(pn, gbc);
JLabel address = new JLabel("Address");
address.setFont(bold12);
gbc.insets = midMidLabel;
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(address, gbc);
JLabel em = new JLabel("E-Mail");
em.setFont(bold12);
gbc.insets = botMidLabel;
gbc.gridx = 2;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(em, gbc);
JLabel emptyTop = new JLabel(" ");
gbc.insets = topRightInsets;
gbc.gridx = 5;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emptyTop, gbc);
JLabel emptyMid1 = new JLabel(" ");
gbc.insets = midRightInsets;
gbc.gridx = 5;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emptyMid1, gbc);
JLabel emptyMid2 = new JLabel(" ");
gbc.insets = midRightInsets;
gbc.gridx = 5;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emptyMid2, gbc);
JLabel emptyBot = new JLabel(" ");
gbc.insets = botRightInsets;
gbc.gridx = 5;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emptyBot, gbc);
idtf = new JTextField("");
idtf.setFont(plain);
idtf.setEditable(false);
idtf.setHorizontalAlignment(JTextField.RIGHT);
idtf.addKeyListener(new NumericList());
gbc.insets = topMidTF;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.4;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(idtf, gbc);
nametf = new JTextField("");
nametf.setFont(plain);
nametf.setEditable(true);
nametf.addKeyListener(new AlphabeticList());
gbc.insets = midMidTF;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.4;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(nametf, gbc);
ln1tf = new JTextField("");
ln1tf.setFont(plain);
ln1tf.setEditable(true);
ln1tf.addKeyListener(new AlphabeticList());
gbc.insets = midMidTF;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.weightx = 0.4;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(ln1tf, gbc);
ln2tf = new JTextField("");
ln2tf.setFont(plain);
ln2tf.setEditable(true);
ln2tf.addKeyListener(new AlphabeticList());
gbc.insets = botMidTF;
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0.4;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(ln2tf, gbc);
agetf = new JTextField("");
agetf.setFont(plain);
agetf.setEditable(true);
agetf.addKeyListener(new NumericList());
gbc.insets = topMidTF;
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(agetf, gbc);
pntf = new JTextField("");
pntf.setFont(plain);
pntf.setEditable(true);
pntf.addKeyListener(new PhoneList());
gbc.insets = midMidTF;
gbc.gridx = 3;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(pntf, gbc);
addresstf = new JTextField("");
addresstf.setFont(plain);
addresstf.setEditable(true);
addresstf.addKeyListener(new AddressList());
gbc.insets = midMidTF;
gbc.gridx = 3;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(addresstf, gbc);
emtf = new JTextField("");
emtf.setFont(plain);
emtf.setEditable(true);
emtf.addKeyListener(new EmailList());
gbc.insets = botMidTF;
gbc.gridx = 3;
gbc.gridy = 3;
gbc.gridwidth = 3;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emtf, gbc);
// My Traversal Policy
compList.add(nametf);
compList.add(ln1tf);
compList.add(ln2tf);
compList.add(agetf);
compList.add(pntf);
compList.add(addresstf);
compList.add(emtf);
dataIn.setFocusTraversalPolicy(new MyFocusTraversalPolicy());
// Table
String[][] data = {};
String[] headers = { "Id", "Name", "First Lastname", "Second Lastname", "Age", "Phone Number", "Address",
"E-Mail" };
dtm = new DefaultTableModel(data, headers);
t = new JTable(dtm);
t.setFont(plaint);
header = t.getTableHeader();
header.setFont(bold12);
// Table Design
int[] columnsWidth = { 65, 120, 120, 120, 40, 100, 300, 220 };
int i = 0;
for (int width : columnsWidth) {
TableColumn column = t.getColumnModel().getColumn(i++);
column.setMinWidth(width);
column.setMaxWidth(width);
column.setPreferredWidth(width);
}
// Columns Text Alignment
DefaultTableCellRenderer rendererLeft = new DefaultTableCellRenderer();
DefaultTableCellRenderer rendererCenter = new DefaultTableCellRenderer();
TableColumnModel colModel = t.getColumnModel();
TableColumn[] col = new TableColumn[t.getColumnCount()];
for (int j = 0; j < t.getColumnCount(); j++) {
col[j] = colModel.getColumn(j);
switch (j) {
case 0: {
rendererCenter.setHorizontalAlignment(JLabel.CENTER);
col[j].setCellRenderer(rendererCenter);
}
break;
case 1: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
case 2: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
case 3: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
case 4: {
rendererCenter.setHorizontalAlignment(JLabel.CENTER);
col[j].setCellRenderer(rendererCenter);
}
break;
case 5: {
rendererCenter.setHorizontalAlignment(JLabel.CENTER);
col[j].setCellRenderer(rendererCenter);
}
break;
case 6: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
case 7: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
}
}
// Insert table into ScrollPane
JScrollPane sp = new JScrollPane(t);
table.add(sp);
t.setPreferredScrollableViewportSize(new Dimension(1085, 230));
// Buttons
JPanel eastP = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
JButton save = new JButton("Save");
JButton del = new JButton("Delete");
JButton edit = new JButton("Edit");
JButton clr = new JButton("Clear");
save.setFont(bold12);
del.setFont(bold12);
edit.setFont(bold12);
clr.setFont(bold12);
save.addActionListener(new Save());
del.addActionListener(new Delete());
edit.addActionListener(new Edit());
clr.addActionListener(new Clear());
t.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
rowSelected = t.getSelectedRow();
}
});
eastP.add(save);
eastP.add(del);
eastP.add(edit);
eastP.add(clr);
buttons.add(eastP, BorderLayout.EAST);
// Add to main
mainP.add(dataIn, BorderLayout.NORTH);
mainP.add(table, BorderLayout.CENTER);
mainP.add(buttons, BorderLayout.SOUTH);
mainF.setContentPane(mainP);
mainF.pack();
mainF.setVisible(true);
mainF.setResizable(false);
mainF.setLocationRelativeTo(null);
mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserFormNew();
}
});
}
//Focus Traversal Policy Custom Class
private class MyFocusTraversalPolicy extends FocusTraversalPolicy {
public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {
int currentPosition = compList.indexOf(aComponent);
currentPosition = (currentPosition + 1) % compList.size();
return (Component) compList.get(currentPosition);
}
public Component getComponentBefore(Container focusCycleRoot, Component aComponent) {
int currentPosition = compList.indexOf(aComponent);
currentPosition = (compList.size() + currentPosition - 1) % compList.size();
return (Component) compList.get(currentPosition);
}
public Component getFirstComponent(Container cntnr) {
return (Component) compList.get(0);
}
public Component getLastComponent(Container cntnr) {
return (Component) compList.get(compList.size() - 1);
}
public Component getDefaultComponent(Container cntnr) {
return (Component) compList.get(0);
}
}
// Listener Classes
public class Save implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String name = nametf.getText();
String ln1 = ln1tf.getText();
String ln2 = ln2tf.getText();
String age = agetf.getText();
String pn = pntf.getText();
String address = addresstf.getText();
String em = emtf.getText();
// Autogenerate id
idCount++;
// Data Into Table
if (idCount == 0) {
t.setValueAt(Integer.toString(idCount), 0, 0);
t.setValueAt(name, 0, 1);
t.setValueAt(ln1, 0, 2);
t.setValueAt(ln2, 0, 3);
t.setValueAt(age, 0, 4);
t.setValueAt(pn, 0, 5);
t.setValueAt(address, 0, 6);
t.setValueAt(em, 0, 7);
} else if (ed == true) {
t.setValueAt(name, rowSelected, 1);
t.setValueAt(ln1, rowSelected, 2);
t.setValueAt(ln2, rowSelected, 3);
t.setValueAt(age, rowSelected, 4);
t.setValueAt(pn, rowSelected, 5);
t.setValueAt(address, rowSelected, 6);
t.setValueAt(em, rowSelected, 7);
ed = false;
} else {
String[] newRow = { Integer.toString(idCount), name, ln1, ln2, age, pn, address, em };
dtm.addRow(newRow);
}
idtf.setText("");
nametf.setText("");
ln1tf.setText("");
ln2tf.setText("");
agetf.setText("");
pntf.setText("");
addresstf.setText("");
emtf.setText("");
}
}
public class Delete implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
dtm.removeRow(rowSelected);
}
}
public class Edit implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
ed = true;
selRowInfoGotten = new String[t.getColumnCount()];
for (int i = 0; i < t.getColumnCount(); i++) {
selRowInfoGotten[i] = (String) t.getValueAt(rowSelected, i);
}
idtf.setText(selRowInfoGotten[0]);
nametf.setText(selRowInfoGotten[1]);
ln1tf.setText(selRowInfoGotten[2]);
ln2tf.setText(selRowInfoGotten[3]);
agetf.setText(selRowInfoGotten[4]);
pntf.setText(selRowInfoGotten[5]);
addresstf.setText(selRowInfoGotten[6]);
emtf.setText(selRowInfoGotten[7]);
}
}
public class Clear implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (ed == false) {
idtf.setText("");
nametf.setText("");
ln1tf.setText("");
ln2tf.setText("");
agetf.setText("");
pntf.setText("");
addresstf.setText("");
emtf.setText("");
} else {
nametf.setText("");
ln1tf.setText("");
ln2tf.setText("");
agetf.setText("");
pntf.setText("");
addresstf.setText("");
emtf.setText("");
}
}
}
public class AlphabeticList implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
// Letters only
char testChar = e.getKeyChar();
if (!Character.isAlphabetic(testChar)) {
e.consume();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
nametf.getFocusTraversalPolicy();
ln1tf.getFocusTraversalPolicy();
ln2tf.getFocusTraversalPolicy();
}
}
@Override
public void keyReleased(KeyEvent e) { }
}
public class NumericList implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
// Numbers only
char testChar = e.getKeyChar();
if (!Character.isDigit(testChar)) {
e.consume();
}
// 3-digit ages max
String ageText = agetf.getText();
int ageLength = ageText.length();
if (ageLength >= 3) {
e.consume();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
agetf.getFocusTraversalPolicy();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
public class AddressList implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
addresstf.getFocusTraversalPolicy();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
public class PhoneList implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
// Numbers only
char testChar = e.getKeyChar();
if (!Character.isDigit(testChar)) {
e.consume();
}
String pnText = pntf.getText();
int pnLength = pnText.length();
// Add hyphens
if (pnLength == 3 || pnLength == 7) {
pntf.setText(pnText + "-");
}
// 10-digit numbers
if (pnLength >= 12) {
e.consume();
}
// Erase even hyphens
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
String pnTxt = pntf.getText();
int start = 0;
int end = pnLength - 2;
String pnErased = pnTxt.substring(start, end);
System.out.println(
"PNLength: " + pnLength + ", Start: " + start + ", End: " + end + ", PNErased: " + pnErased);
pntf.setText(pnErased);
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
pntf.getFocusTraversalPolicy();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
public class EmailList implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
emtf.getFocusTraversalPolicy();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
}
最佳答案
幸运的是,我有一个小得多的示例用户表单,我在其中添加了焦点遍历策略。
该代码有两部分。当我构建 GUI 时,我设置了焦点遍历策略。我按照希望遍历的顺序将所有 JTextField 放入一个列表中。 JPanel 中的每个可编辑字段都必须包含在此列表中。
我还编写了一个扩展 FocusTraversalPolicy 的类。我将其设为私有(private)并将其包含在 View 类中。这似乎是一个适合类的地方。如果需要,您可以将其放入单独的文件中。
这是代码。您可以编译它,运行它,然后看到在各列后面按 Tab 键浏览表单。这就是我们所说的 minimal, reproducible example .
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FocusTraversalPolicy;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class UserForm {
// top, left, bottom, right
private static final Insets topInsets = new Insets(10, 10, 10, 10);
private static final Insets topCenterInsets = new Insets(10, 0, 10, 10);
private static final Insets middleInsets = new Insets(0, 10, 10, 10);
private static final Insets middleCenterInsets = new Insets(0, 0, 10, 10);
public UserForm() {
JFrame frame = new JFrame("User Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Miscelaneous
Font plain = new Font("Tahoma", Font.PLAIN, 12);
Font bold11 = new Font("Tahoma", Font.BOLD, 11);
Font bold12 = new Font("Tahoma", Font.BOLD, 12);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
int gridy = 0;
// Data-In Form Components
JLabel idl = new JLabel("Id:");
idl.setFont(bold12);
addComponent(mainPanel, idl, 0, gridy, 1, 1, topInsets, GridBagConstraints.LINE_START, GridBagConstraints.NONE);
JTextField idtf = new JTextField(40);
idtf.setFont(plain);
addComponent(mainPanel, idtf, 1, gridy, 1, 1, topCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel agl = new JLabel("Age:");
agl.setFont(bold12);
addComponent(mainPanel, agl, 2, gridy, 1, 1, topCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField agtf = new JTextField(40);
agtf.setFont(plain);
addComponent(mainPanel, agtf, 3, gridy++, 1, 1, topCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel namel = new JLabel("Name:");
namel.setFont(bold12);
addComponent(mainPanel, namel, 0, gridy, 1, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField nametf = new JTextField(40);
nametf.setFont(plain);
addComponent(mainPanel, nametf, 1, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel adl = new JLabel("Address:");
adl.setFont(bold12);
addComponent(mainPanel, adl, 2, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField adtf = new JTextField(40);
adtf.setFont(plain);
addComponent(mainPanel, adtf, 3, gridy++, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel pnl = new JLabel("Phone Number:");
pnl.setFont(bold12);
addComponent(mainPanel, pnl, 0, gridy, 1, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField pntf = new JTextField(40);
pntf.setFont(plain);
addComponent(mainPanel, pntf, 1, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel ln1l = new JLabel("First Lastname:");
ln1l.setFont(bold12);
addComponent(mainPanel, ln1l, 2, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField ln1tf = new JTextField(40);
ln1tf.setFont(plain);
addComponent(mainPanel, ln1tf, 3, gridy++, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel ln2l = new JLabel("Second Lastname:");
ln2l.setFont(bold12);
addComponent(mainPanel, ln2l, 0, gridy, 1, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField ln2tf = new JTextField(40);
ln2tf.setFont(plain);
addComponent(mainPanel, ln2tf, 1, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel eml = new JLabel("E-Mail:");
eml.setFont(bold12);
addComponent(mainPanel, eml, 2, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField emtf = new JTextField(40);
emtf.setFont(plain);
addComponent(mainPanel, emtf, 3, gridy++, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
List<Component> order = new ArrayList<>(8);
order.add(idtf);
order.add(nametf);
order.add(pntf);
order.add(ln2tf);
order.add(agtf);
order.add(adtf);
order.add(ln1tf);
order.add(emtf);
MyOwnFocusTraversalPolicy newPolicy = new MyOwnFocusTraversalPolicy(order);
frame.setFocusTraversalPolicy(newPolicy);
// JTable Creation
String[] headline = { "Id", "Name", "First Lastname", "Second Lastname", "Age", "Address", "Phone Number",
"E-Mail" };
String[][] data = { { "", "", "", "", "", "", "", "" }, { "", "", "", "", "", "", "", "" } };
JTable dataShow = new JTable(data, headline);
dataShow.setPreferredScrollableViewportSize(new Dimension(900, 300));
dataShow.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(dataShow);
addComponent(mainPanel, scrollPane, 0, gridy++, 4, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
// Table Personalization
dataShow.getTableHeader().setFont(bold11);
new HeaderRenderer(dataShow);
// buttons Components
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
JButton save = new JButton("Save");
save.setFont(bold12);
addComponent(buttonPanel, save, 0, 0, 1, 1, topInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JButton del = new JButton("Delete");
del.setFont(bold12);
addComponent(buttonPanel, del, 1, 0, 1, 1, topInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
JButton edit = new JButton("Edit");
edit.setFont(bold12);
addComponent(buttonPanel, edit, 2, 0, 1, 1, topInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
addComponent(mainPanel, buttonPanel, 0, gridy++, 4, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
// set content pane
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
// For future table filling
/*
* String ids = idtf.getText(); String names = nametf.getText(); String ln1s =
* ln1tf.getText(); String ln2s = ln2tf.getText(); String ags = agtf.getText();
* String ads = adtf.getText(); String pns = pntf.getText(); String ems =
* emtf.getText();
*/
}
private void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth,
int gridheight, Insets insets, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill,
insets, 0, 0);
container.add(component, gbc);
}
public static void main(String[] args) {
// invoke runnable for thread safety
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserForm();
}
});
}
// LEFT alignment renderer
private class HeaderRenderer implements TableCellRenderer {
DefaultTableCellRenderer renderer;
public HeaderRenderer(JTable table) {
renderer = (DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(JLabel.LEFT);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int col) {
return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
}
}
private class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy {
private List<Component> componentList;
public MyOwnFocusTraversalPolicy(List<Component> componentList) {
this.componentList = componentList;
}
@Override
public Component getComponentAfter(Container aContainer, Component aComponent) {
int index = getComponentIndex(aComponent);
index++;
index = (index >= componentList.size()) ? 0 : index;
return componentList.get(index);
}
@Override
public Component getComponentBefore(Container aContainer, Component aComponent) {
int index = getComponentIndex(aComponent);
index--;
index = (index < 0) ? index + componentList.size() : index;
return componentList.get(index);
}
@Override
public Component getFirstComponent(Container aContainer) {
return componentList.get(0);
}
@Override
public Component getLastComponent(Container aContainer) {
return componentList.get(componentList.size() - 1);
}
@Override
public Component getDefaultComponent(Container aContainer) {
return componentList.get(0);
}
private int getComponentIndex(Component test) {
for (int i = 0; i < componentList.size(); i++) {
Component component = componentList.get(i);
if (component.equals(test)) {
return i;
}
}
return -1;
}
}
}
关于java - 尝试使用 Tab 在 JTextField 之间垂直跳转(使用 Custom FocusTraversalPolicy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60984530/
我想对齐输入,它有两个标签和一个没有任何标签的按钮,以便它们都在同一级别。 这是期望的效果: 这是我到目前为止得到的: 我通过在按钮前添加带有空白空间的标签,设法让它看起来像我想要的样子,但这并不理想
您好,我有一个关于 JavaFX 的问题。我担心答案是剥皮,我对此一无所知,但这里是。 我想在 JavaFX 中制作 Accordion/TabPane 交叉。我将尝试用文字解释自己,但在下面我已经包
这可能被认为是一个低俗的问题。然而,我还没有找到任何代码,也没有找到任何讨论如何用我的编程语言 C 来解决这个问题的论坛。已经进行了大量的尝试,并且最终都以实际上“硬编码”新数组而告终。 我正在尝试垂
我有以下代码: public Frame() { super(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT
是否可以将 UIModalTransitionStyleFlipHorizontal 修改为垂直而不是水平? 我通常会使用 CATransition 来执行此操作,但该方法需要将一个 UIView
我有一个 ios5 导航的想法,我正在一个应用程序上做,我认为从 SOF 那里得到一些关于我的想法的建设性批评是明智的。 p> 想法: UIView 包含 6 个左右垂直堆叠的按钮 UIButtons
我正在努力进行一些 iOS 开发,我希望有人能够帮助我提出建议。我想实现一堆看起来像一副纸牌的 UIView。用户应该能够通过触摸刷出“卡片”。我想到了带有 pagingEnabled 的 UIScr
我想创建一个包含四张图片的入口页面作为指向其他页面的链接。但我希望这些图片显示在显示器的中间(垂直和水平)。我已经尝试了很多事情,但我做对了。我什至无法以任何方式垂直对齐图片(例如底部,您将在代码中看
我有代码在 div 的底部创建一个元素作为向下的“箭头/指针”。 我需要这样写,以便“箭头/指针”指向左侧 当前代码 - HTML 掩码 css 在 fiddle 中 https://j
下面的代码将焦点放在中间的 div 上,但该 div 出现在窗口的底部。如何使该 div 居中,使其显示在窗口的中心[垂直],而不设置固定位置。 $(function(){ $("#focus
这个问题在这里已经有了答案: Vertical Align Center in Bootstrap 4 [duplicate] (20 个答案) 关闭 4 年前。 我创建了一个由两列组成的结构,第一
演示:http://jsfiddle.net/vpH8r/2/ 我在上面创建了一个 jfiddle,试图让垂直滑动正常工作。 当前的问题是 slider 标签不会移动,并且不会出现从顶部到底部的干净滑
我正在尝试在 javascript 中创建一个垂直 slider 小部件。我不是在寻找任何插件或库,我正在尝试查看如何使用纯 javascript 完成它。我想我大概明白了。 请在此处查看到目前为止已
我正在尝试创建一个响应式导航列表,并将图像置于页面列表的中心 我已经设法接近那个,但是图像比列表更靠前。 我想像这样得到它,但似乎无法弄清楚。 任何帮助将不胜感激! 代码:https://jsfidd
有人可以帮我将导航栏居中,使其在页面上垂直居中吗? 我希望导航栏中央的链接位于顶部,而不是像现在这样向左浮动。 提前致谢 Toggle navigation
我想垂直、水平转换图像,并使用 javascript 缩放图像。目前我设法使调整大小的图像起作用,但它看起来不正确。我正在尝试使用类似于 Transform image 的东西.调整垂直移动条时,图像
这可能是一个问题,但要求太多,但我已经达到了我的极限。 我有这段 ( http://jsfiddle.net/3whTa/) CSS,它创建了一个箭头水平面包屑导航。 我想做的是将其转换为垂直。就像箭
我正在使用 opencv 进行非常简单的操作,但我无法理解为什么会出现此错误/问题。图像被操作到图像的一半(垂直)。 Mat img = imread("/Users/tanmoy/Documents
我想知道是否有人有任何技术来定位 css 生成的内容。例如: .block { height: 150px; width: 150px; border: 1px solid black;
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭3年前。 Improve this que
我是一名优秀的程序员,十分优秀!