我是GUI编程的新手,我不知道如何更新对象并在屏幕上显示更改。我有一个 Sudoku 类,它扩展了 JPanel 。我有一个JFrame,数独是它的一部分。我有一个按钮,它调用函数 openFile 。以下是它的名称:
private void openFileActionPerformed(java.awt.event.ActionEvent evt) {
sudoku = FileManagement.openFile(this, sudoku);
}
sudoku 对象有一个9x9网格,当我调试它时没关系,新值在网格中。但我不知道如何在屏幕上显示新值。我读了几条评论并尝试了sudoku.validate(),sudoku.repaint()或者只是再次打包JFrame,但它没有用。如果有人告诉我如何修复它,我将不胜感激。如果我的解释不充分或不清楚,请告诉我在说明中还要添加什么。谢谢!
编辑 - 添加代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class Sample extends javax.swing.JFrame {
public Sample() {
initComponents();
}
private void initComponents() {
sudoku = new sudoku.Sudoku();
openFile = new javax.swing.JButton();
openFile.setText("Open");
openFile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openFileActionPerformed(evt);
}
});
/*sorry for that part - automaticlly generated, don't know how to make it less*/
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sudoku)
.addGroup(layout.createSequentialGroup()
.addComponent(openFile)))));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(sudoku)
.addComponent(openFile)));
pack();
}
private void openFileActionPerformed(java.awt.event.ActionEvent evt) {
sudoku = openFile(this, sudoku);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sample().setVisible(true);
}
});
}
private javax.swing.JButton openFile;
private sudoku.Sudoku sudoku;
public static Sudoku openFile(JFrame frame, Sudoku sudoku) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.CANCEL_OPTION) {
return sudoku;
}
File fileName = fileChooser.getSelectedFile();
try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(fileName))) {
sudoku = (Sudoku) input.readObject();
} catch (IOException | ClassNotFoundException ioException) {
System.out.println(ioException);
}
return sudoku;
}
}
答案 0 :(得分:1)
问题的快速解决方法是从JPanel
中移除JFrame
,更改其值,然后将JPanel
重新添加到JFrame
。这可以在下面的例子中看到。
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.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class PanelFix extends JFrame {
JFrame frame = this;
private JPanel view = a();
PanelFix() {
super("Panel Fix");
setSize(400, 400);
setLocationRelativeTo(null);
getContentPane().add(view);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel a() {
JPanel panel = new JPanel();
JButton but = new JButton("Press Me");
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
getContentPane().remove(view);
view = b();
getContentPane().add(view);
repaint();
validate();
}
});
panel.add(but);
return panel;
}
private JPanel b() {
JPanel panel = new JPanel();
panel.add(new JLabel("Second Panel"));
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new PanelFix());
}
}
如果这不能解决问题,例如Andrew Thompson,请添加MCVE。这应该可以帮助我们解决您遇到的确切问题