我在使用带有 RMI 的表时遇到了一个非常奇怪的问题。 该客户端是一个时段预订系统的实现,我已经 作为表实现。
我面临两个问题。 第一个是导致表在更改后更新。 解决方案似乎是
private void cleanUp() {
panel.removeAll();
panel.setVisible(false);
panel.revalidate();
showTable();
}
这似乎确实有效。 (或者可能导致我的问题,我不确定)
我现在遇到的问题与方法内的 JTextField 有关 这称为实际预订。
private JTextField txtClientname;
txtClientname = new JTextField();
txtClientname.setText("ClientName");
然后在确认按钮监听器中 -
callBookingSlot(buttonAction, txtClientname.getText());
真正奇怪的是,这最初有效,一次。通过工作 我的意思是将从 JTextField 中提取的正确值放入表中。 第一次,它将输入用户在字段中输入的值。 任何后续的操作都只会放入字符串“ClientName”
大家有什么想法吗?这个问题似乎与 RMI 无关,我已经 在没有 RMI 的情况下进行了尝试,并且从文本字段中获取的值仍然存在 行为方式相同。 我知道我可能应该看看 fireTableUpdated 等,我就是这样,但它会是 如果这是很容易解决的问题之一,那就太好了。
编辑 - 更多信息
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
public class StackOverFlowGUI {
private static JFrame frame;
private static JTable table;
private static JPanel panel;
private JTextField txtClientname;
private static JFrame bookingPopup = new JFrame();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
StackOverFlowGUI window = new StackOverFlowGUI();
window.frame.setVisible(true);
}
});
}
public StackOverFlowGUI() {
initialize();
}
private void initialize() {
panel = new JPanel();
frame = new JFrame();
frame.setBounds(100, 100, 700, 751);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
panel.setBounds(10, 11, 674, 576);
frame.getContentPane().add(panel);
showTable();
}
private void showTable() {
table = new JTable();
panel.add(table);
panel.setVisible(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setCellSelectionEnabled(true);
showBookingPopup(2, 2);
}
private void showBookingPopup(int row, int col) {
bookingPopup.setBounds(100, 100, 220, 185);
bookingPopup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bookingPopup.getContentPane().setLayout(null);
txtClientname = new JTextField();
txtClientname.setText("ClientName");
txtClientname.setBounds(10, 11, 184, 20);
bookingPopup.getContentPane().add(txtClientname);
txtClientname.setColumns(10);
bookingPopup.setVisible(true);
JPanel panel = new JPanel();
panel.setBounds(10, 65, 184, 33);
bookingPopup.getContentPane().add(panel);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Here - works first time
System.out.println(txtClientname.getText());
//Continues to work if I don't call cleanUp - but then main window will not update
cleanUp();
}
});
btnSubmit.setBounds(10, 113, 89, 23);
bookingPopup.getContentPane().add(btnSubmit);
}
private void cleanUp() {
panel.removeAll();
panel.setVisible(false);
panel.revalidate();
showTable();
}
}
最佳答案
您发布的代码有几个主要问题,包括:
- 在应该使用实例字段的地方使用静态字段
- 使用 null 布局和
setBounds(...)
并避免使用 Swing 布局管理器 - 在应该使用 JDialog 的地方使用多个 JFrame
- 但最重要的是,不必要地重新创建变量和组件。如果您需要清除字段,请清除字段,但不要简单地丢弃它们。
例如:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class DataIntoTable extends JPanel {
private static final String[] COL_NAMES = {"First Name", "LastName"};
private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0);
private JTable table = new JTable(model);
private JTextField firstNameField = new JTextField(10);
private JTextField lastNameField = new JTextField(10);
private JPanel dialogPanel = new JPanel();
public DataIntoTable(final JFrame frame) {
setLayout(new BorderLayout());
add(new JScrollPane(table));
dialogPanel.add(new JLabel("First Name:"));
dialogPanel.add(firstNameField);
dialogPanel.add(new JLabel("Second Name:"));
dialogPanel.add(lastNameField);
dialogPanel.add(new JButton(new AbstractAction("Submit") {
@Override
public void actionPerformed(ActionEvent arg0) {
String[] rowData = {firstNameField.getText(), lastNameField.getText()};
model.addRow(rowData);
firstNameField.setText("");
lastNameField.setText("");
}
}));
JDialog dialog = new JDialog(frame, "Enter Name", false);
dialog.getContentPane().add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Data Into Table");
DataIntoTable mainPanel = new DataIntoTable(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
关于java - JTextField 和 RMI Java 的 GUI 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15825120/