我有一个使用 aJTable
来显示一些数据的 Java 应用程序。我想从JDialog
.
我面临的问题是主窗口 GUI 在打开后将不再刷新JDialog
。我试图将其更改为 aJFrame
并且我得到相同的行为。如果我JTable
用JPanel
. 一切正常,直到在主窗口顶部打开一个窗口。此后,GUI 元素不会刷新,但按钮仍会对点击做出反应。
有人可以帮我理解什么是错的。
抱歉没有分享任何代码。早上 5 点,我完全忘记了。我有一个带有 aJTable
和 a的框架JButton
。按钮打开JDialog
. JDialog
有一个嵌有 aJTextField
和 a的面板JButton
。当我按下按钮时,JDialog
它会将数据发送到主框架。在那里,我想用收到的数据填充表格。我可以在调试器中看到数据。问题是在我重新启动应用程序之前它不会显示在表格中。我还在与表格相同的框架中添加了一个按钮来测试数据插入,它工作得很好。
public class GUIRezervari {
private static JButton butonNew;
private static JTable tabelRezervari;
public static DefaultTableModel dtm;
private static JScrollPane scrollPaneTabel;
private static JPanel panelRezervari;
private GUIRezervari(){
}
public static JPanel getGui(){
setPanelRezervari();
return panelRezervari;
}
private static void setPanelRezervari(){
panelRezervari=new JPanel();
panelRezervari.setLayout(new BoxLayout(panelRezervari, 0));
Box hBox=Box.createHorizontalBox();
hBox.add(Box.createHorizontalGlue());
hBox.add(boxRezervari());
hBox.add(Box.createHorizontalGlue());
panelRezervari.add(hBox);
}
private static Box boxRezervari(){
Box vBox=Box.createVerticalBox();
vBox.add(Box.createVerticalStrut(20));
vBox.add(boxButoane());
vBox.add(Box.createHorizontalStrut(10));
vBox.add(boxTabel());
vBox.add(Box.createVerticalStrut(20));
return vBox;
}
private static Box boxButoane(){
Box hBox=Box.createHorizontalBox();
butonNew=new JButton("New");
butonNew.addActionListener(new RezervareNouaAL());
hBox.add(Box.createHorizontalStrut(10));
hBox.add(butonNew);
hBox.add(Box.createHorizontalGlue());
hBox.add(Box.createHorizontalStrut(10));
return hBox;
}
private static Box boxTabel(){
Box hBox=Box.createHorizontalBox();
dtm=new DefaultTableModel();
dtm.addColumn("Date");
dtm.addColumn("Name");
tabelRezervari=new JTable(dtm);
scrollPaneTabel=new JScrollPane(tabelRezervari);
hBox.add(scrollPaneTabel);
return hBox;
}
}
public class RezervareNouaAL implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
GUIRezervare.getFrame((JFrame)GUIRezervari.getGui().getParent());
}
}
public class GUIRezervare {
private static JLabel labelName;
private static JLabel labelDate;
private static JTextField fieldName;
private static JTextField fieldDate;
private static JButton butonSalveaza;
private static JButton butonAnuleaza;
private static JPanel panelRezervare;
private static JDialog frame;
private GUIRezervare(){
}
public static void getFrame(JFrame panel) {
setGui();
frame = new JDialog(panel, "Rezervare", true);
frame.add(panelRezervare);
frame.pack();
frame.setVisible(true);
frame.setTitle("Adauga rezervare");
}
public static JPanel getGui(){
setGui();
return panelRezervare;
}
private static void setGui(){
panelRezervare=new JPanel();
panelRezervare.setLayout(new BoxLayout(panelRezervare, 0));
panelRezervare.setBorder(BorderFactory.createTitledBorder("Rezervare"));
panelRezervare.setBackground(new Color(242,197,61));
Box vBox=Box.createVerticalBox();
vBox.add(Box.createVerticalStrut(10));
vBox.add(panelRezervare());
vBox.add(Box.createVerticalStrut(10));
vBox.add(boxButoane());
vBox.add(Box.createVerticalStrut(10));
panelRezervare.add(vBox);
}
private static JPanel panelRezervare(){
JPanel panelRezervareT=new JPanel();
panelRezervareT.setBorder(BorderFactory.createTitledBorder("Date rezervare"));
panelRezervareT.add(boxRezervare());
panelRezervareT.setBackground(new Color(169,217,190));
return panelRezervareT;
}
private static Box boxRezervare(){
Box hBox=Box.createHorizontalBox();
hBox.add(Box.createHorizontalGlue());
hBox.add(boxStanga());
hBox.add(Box.createHorizontalStrut(10));
hBox.add(Box.createHorizontalGlue());
return hBox;
}
private static Box boxStanga(){
Box vBox=Box.createVerticalBox();
vBox.add(boxName());
vBox.add(Box.createVerticalStrut(5));
vBox.add(boxDate());
vBox.add(Box.createVerticalStrut(5));
return vBox;
}
private static Box boxDate(){
Box hBox=Box.createHorizontalBox();
labelDate=new JLabel("Date :");
fieldDate=new JTextField();
hBox.add(labelDate);
hBox.add(Box.createHorizontalStrut(10));
hBox.add(Box.createHorizontalGlue());
hBox.add(fieldDate);
return hBox;
}
private static Box boxName(){
Box hBox=Box.createHorizontalBox();
labelName=new JLabel("Name :");
fieldName=new JTextField();
hBox.add(labelName);
hBox.add(Box.createHorizontalStrut(10));
hBox.add(Box.createHorizontalGlue());
hBox.add(fieldName);
return hBox;
}
private static Box boxButoane(){
Box hBox=Box.createHorizontalBox();
butonSalveaza=new JButton("Salveaza");
butonSalveaza.addActionListener(new SalveazaClientAL());
butonAnuleaza=new JButton("Anulaeza");
butonAnuleaza.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
frame.dispose();
}
});
hBox.add(butonSalveaza);
hBox.add(Box.createHorizontalStrut(10));
hBox.add(butonAnuleaza);
return hBox;
}
public static String getFieldName() {
return fieldName.getText();
}
public static String getFieldDate() {
return fieldDate.getText();
}
public static void inchideFrame(){
frame.dispose();
}
}
public class SalveazaClientAL implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
GUIRezervari.dtm.addRow(new Object[]{GUIRezervare.getFieldName(),GUIRezervare.getFieldDate()});
GUIRezervare.inchideFrame();
}
}
这是代码。谢谢您的帮助!