gpt4 book ai didi

java - 事件发生时在 JFrame 中动态创建 JTextField

转载 作者:行者123 更新时间:2023-12-01 21:08:22 25 4
gpt4 key购买 nike

我正在根据“+”按钮上的点击事件动态创建JTextField。以下是屏幕截图。

enter image description here

问题是当我单击“+”按钮时,字段已创建,但未显示在 JFrame 上。当我将光标放在“项目名称”下的下一行时,文本字段变得可见。

问题出在哪里?

enter image description here

下面是我的代码。

CreateBill()
{

jf = new JFrame("Create Bill");
jf.getContentPane().setLayout(null);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setBounds(0, 0, d1.width, d1.height);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



createRow(); // This will create first row by default.
jf.pack();
jf.setVisible(true);

}


private void createRow() {

textField = new JTextField();
textField.setToolTipText("item name");
textField.setBounds(143, 46+j, 288, 20);
textField.setColumns(10);
textField.getDocument().addDocumentListener(new DocumentListener()
{

@Override
public void insertUpdate(DocumentEvent e) {
updatePrice();

}

@Override
public void removeUpdate(DocumentEvent e) {


}

@Override
public void changedUpdate(DocumentEvent e) {
updatePrice();

}

});

AutoCompleteDecorator.decorate(textField, names, true);
jf.getContentPane().add(comboComplete);
jf.getContentPane().add(textField);
comboComplete.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e)
{
String ItemSel = textField.getText().trim();
for(Item s:items)
{
if(s.getItemName().equals(ItemSel))
{
textField_1.setText(String.valueOf(s.getUnitPrice()));
}
}


}
});


textFields.add(textField);
textField_1 = new JTextField();
textField_1.setEditable(false);
textField_1.setBounds(639, 46+j, 175, 20);
jf.getContentPane().add(textField_1);
textField_1.setColumns(10);



qty = new JTextField();
qty.setBounds(455, 46+j, 156, 20);
jf.getContentPane().add(qty);
qty.setColumns(10);

qty.getDocument().addDocumentListener(new DocumentListener()
{

@Override
public void insertUpdate(DocumentEvent e) {
getTotal();

}

@Override
public void removeUpdate(DocumentEvent e) {


}

@Override
public void changedUpdate(DocumentEvent e) {
getTotal();

}

});

textFields.add(qty);
textFields.add(textField_1);


textField_3 = new JTextField();
textField_3.setEditable(false);
textField_3.setBounds(1038, 46+j, 156, 20);
jf.getContentPane().add(textField_3);

textField_3.setColumns(10);



textFields.add(textField_3);

JButton button = new JButton("+");
button.setBounds(1235, 45+j, 89, 23);
jf.getContentPane().add(button);
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("value of J"+j);
createRow(); // after pressing '+' button i am calling same method again. by changing value of j.

}
});

j=j+22;

jf.setVisible(true);

}

我希望我的所有 4 个文本字段同时显示。

最佳答案

向容器添加组件后,您需要在容器上调用repaint()。在调用重绘之前,您还应该调用revalidate(),因为这会告诉布局管理器布局新组件,但您使用的是空布局,真的很想避免做。

所以我的建议是 1) 使用带有适当布局管理器的嵌套 JPanel,并在添加或删除组件后在容器上调用 revalidaterepaint,或者 2)是的,正如 Andrew Thompson 精明地建议的那样,使用 Cardlayout 来交换 View 。您可以让第二个 JPanel 有一个 JTextField,它使用与前一个 JPanel 相同的 Document,因此看起来两者都使用相同的 JTextField(与顶部 JTextField 一样)。

在进一步查看您的图像时,我想知道 JTable 是否可能是一个更好的解决方案。是的,在开始使用布局管理器之后,请在添加所有组件之后并将其设置为可见之前,在顶级窗口上调用 pack()

有关 JTable 实现的示例,大致如下...

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

@SuppressWarnings("serial")
public class CreateRowGui extends JPanel {
private static final Item[] ITEMS = {
new Item("Light Bulb", 2.00),
new Item("Toilet Paper", 3.00),
new Item("Toothpaste", 1.50),
new Item("Aspirin", 3.75) };
private ItemTableModel tableModel = new ItemTableModel();
private JTable table = new JTable(tableModel);
private AddRowAction addRowAction = new AddRowAction("Add Row", KeyEvent.VK_A);

public CreateRowGui() {
TableCellRenderer moneyRenderer = new DefaultTableCellRenderer() {
private NumberFormat currFormat = NumberFormat.getCurrencyInstance();

@Override
protected void setValue(Object value) {
if (value != null) {
value = currFormat.format(value);
}
super.setValue(value);
}
};
table.getColumnModel().getColumn(2).setCellRenderer(moneyRenderer);
table.getColumnModel().getColumn(3).setCellRenderer(moneyRenderer);

JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(addRowAction));
btnPanel.add(new JButton("Remove Row")); // TODO: need Action for this

setLayout(new BorderLayout());
add(new JScrollPane(table));
add(btnPanel, BorderLayout.PAGE_END);
}

class AddRowAction extends AbstractAction {
private NewRowPanel newRowPanel = new NewRowPanel(ITEMS);

public AddRowAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}

@Override
public void actionPerformed(ActionEvent e) {
newRowPanel.reset();
int reply = JOptionPane.showConfirmDialog(table,
newRowPanel.getMainPanel(),
"Select Item and Quantity",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (reply == JOptionPane.OK_OPTION) {
Item item = newRowPanel.getSelectedItem();
int quantity = newRowPanel.getQuantity();
tableModel.addRow(item, quantity);
}
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame("CreateRowGui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CreateRowGui());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

class NewRowPanel {
private JPanel mainPanel = new JPanel();
private JComboBox<Item> itemsCombo;
private JSpinner quantitySpinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1));

@SuppressWarnings("serial")
public NewRowPanel(Item[] items) {
itemsCombo = new JComboBox<>(items);
itemsCombo.setRenderer(new DefaultListCellRenderer(){
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if (value != null) {
value = ((Item) value).getName();
} else {
value = "";
}
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
});
mainPanel.add(new JLabel("Item:"));
mainPanel.add(itemsCombo);
mainPanel.add(Box.createHorizontalStrut(15));
mainPanel.add(new JLabel("Quantity"));
mainPanel.add(quantitySpinner);
}

public void reset() {
itemsCombo.setSelectedIndex(-1);
quantitySpinner.setValue(0);
}

public JPanel getMainPanel() {
return mainPanel;
}

public Item getSelectedItem() {
return (Item) itemsCombo.getSelectedItem();
}

public int getQuantity() {
return (int) quantitySpinner.getValue();
}
}

class ItemTableModel extends AbstractTableModel {
private static final String[] COL_NAMES = { "Item Name", "Quantity", "Unit Price", "Total" };
private static final long serialVersionUID = 1L;
private List<ItemWithCount> itemsWithCount = new ArrayList<>();

@Override
public int getColumnCount() {
return 4;
}

@Override
public int getRowCount() {
return itemsWithCount.size();
}

@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return super.getColumnClass(columnIndex);
case 1:
return Integer.class;
case 2:
case 3:
return Double.class;
}
return super.getColumnClass(columnIndex);
}

@Override
public String getColumnName(int column) {
return COL_NAMES[column];
}

@Override
public Object getValueAt(int row, int column) {
ItemWithCount itemWithCount = itemsWithCount.get(row);
switch (column) {
case 0:
return itemWithCount.getItem().getName();
case 1:
return itemWithCount.getCount();
case 2:
return itemWithCount.getItem().getUnitPrice();
case 3:
return itemWithCount.getCount() * itemWithCount.getItem().getUnitPrice();
}
return null;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
ItemWithCount itemWithCount = itemsWithCount.get(rowIndex);
switch (columnIndex) {
case 1:
itemWithCount.setCount((int) aValue);
fireTableRowsUpdated(rowIndex, rowIndex);
break;

default:
break;
}
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0 || columnIndex == 1;
}

public void addRow(Item item, int quantity) {
ItemWithCount itemWithCount = new ItemWithCount(item, quantity);
itemsWithCount.add(itemWithCount);
int row = itemsWithCount.size() - 1;
fireTableRowsInserted(row, row);
}

private class ItemWithCount {
private Item item;
private int count;

public ItemWithCount(Item item, int count) {
this.item = item;
this.count = count;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public Item getItem() {
return item;
}

}
}

class Item {
private String name;
private double unitPrice;

public Item(String name, double unitPrice) {
this.name = name;
this.unitPrice = unitPrice;
}

public String getName() {
return name;
}

public double getUnitPrice() {
return unitPrice;
}

public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}

@Override
public String toString() {
return "Item [name=" + name + ", unitPrice=" + unitPrice + "]";
}

}

关于java - 事件发生时在 JFrame 中动态创建 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41912924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com