// 创建新的窗口,用于展示和修改数据 JFrame updateFrame = new JFrame("修改数据"); updateFrame.setSize(400, 300); updateFrame.setLayout(new GridLayout(6, 2)); 窗口的位置出现在电脑中间,要怎么设置
时间: 2024-02-14 18:16:33 浏览: 34
要将窗口设置在屏幕中央,可以使用以下代码:
```
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int x = (dim.width - updateFrame.getWidth()) / 2;
int y = (dim.height - updateFrame.getHeight()) / 2;
updateFrame.setLocation(x, y);
```
将这段代码添加到窗口初始化的代码块中,即可将窗口设置在屏幕中央。
相关问题
if (e.getSource() == updateButton) { // 获取当前行的数据,并在新的窗口中显示数据的详细信息 String id = tableModel.getValueAt(selectedRow, 0).toString(); String titleName = tableModel.getValueAt(selectedRow, 1).toString(); String status = tableModel.getValueAt(selectedRow, 2).toString(); String createTime = tableModel.getValueAt(selectedRow, 3).toString(); String updateTime = tableModel.getValueAt(selectedRow, 4).toString(); JOptionPane.showMessageDialog(this, "详细信息:\n" + "id:" + id + "\n材料类型:" + titleName + "\n状态:" + status + "\n创建时间:" + createTime + "\n更改时间:" + updateTime); } 将这段该成即可展示也可以输入信息修改的
代码,您的意思是将这段展示数据的代码改成既能展示数据,又能够修改数据的代码,对吗?
如果是这样的话,我们可以在展示数据的窗口中增加一些文本框和按钮,用于用户输入和保存修改后的数据。具体代码如下:
```
if (e.getSource() == updateButton) {
// 获取当前行的数据,并在新的窗口中显示数据的详细信息
String id = tableModel.getValueAt(selectedRow, 0).toString();
String titleName = tableModel.getValueAt(selectedRow, 1).toString();
String status = tableModel.getValueAt(selectedRow, 2).toString();
String createTime = tableModel.getValueAt(selectedRow, 3).toString();
String updateTime = tableModel.getValueAt(selectedRow, 4).toString();
// 创建新的窗口,用于展示和修改数据
JFrame updateFrame = new JFrame("修改数据");
updateFrame.setSize(400, 300);
updateFrame.setLayout(new GridLayout(6, 2));
// 在窗口中添加文本框和标签,用于展示和修改数据
JLabel idLabel = new JLabel("id:");
JTextField idField = new JTextField(id);
JLabel nameLabel = new JLabel("材料类型:");
JTextField nameField = new JTextField(titleName);
JLabel statusLabel = new JLabel("状态:");
JTextField statusField = new JTextField(status);
JLabel createTimeLabel = new JLabel("创建时间:");
JTextField createTimeField = new JTextField(createTime);
JLabel updateTimeLabel = new JLabel("更改时间:");
JTextField updateTimeField = new JTextField(updateTime);
// 在窗口中添加保存按钮,用于保存修改后的数据
JButton saveButton = new JButton("保存");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入的数据
String newId = idField.getText();
String newName = nameField.getText();
String newStatus = statusField.getText();
String newCreateTime = createTimeField.getText();
String newUpdateTime = updateTimeField.getText();
// 更新表格数据模型中的数据
tableModel.setValueAt(newId, selectedRow, 0);
tableModel.setValueAt(newName, selectedRow, 1);
tableModel.setValueAt(newStatus, selectedRow, 2);
tableModel.setValueAt(newCreateTime, selectedRow, 3);
tableModel.setValueAt(newUpdateTime, selectedRow, 4);
// 关闭窗口
updateFrame.dispose();
}
});
// 在窗口中添加取消按钮,用于关闭窗口
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateFrame.dispose();
}
});
// 将组件添加到窗口中
updateFrame.add(idLabel);
updateFrame.add(idField);
updateFrame.add(nameLabel);
updateFrame.add(nameField);
updateFrame.add(statusLabel);
updateFrame.add(statusField);
updateFrame.add(createTimeLabel);
updateFrame.add(createTimeField);
updateFrame.add(updateTimeLabel);
updateFrame.add(updateTimeField);
updateFrame.add(saveButton);
updateFrame.add(cancelButton);
// 显示窗口
updateFrame.setVisible(true);
}
```
这段代码中,我们创建了一个新的窗口,用于展示和修改数据。在窗口中添加了文本框和标签,用于展示和修改数据。还添加了保存按钮和取消按钮,用于保存修改后的数据和关闭窗口。当用户点击保存按钮时,我们获取用户输入的数据,更新表格数据模型中的数据,并关闭窗口。
帮我根据以下要求:Then modify the View superclass to: hide the frame when the user clicks on the “close” button; add a “window closing” event handler (use an anonymous window adapter) that calls the controller’s shutdown method.修改下述代码:import javax.swing.JFrame; public abstract class View<T extends Controller> extends JFrame implements ModelListener { protected Model m; protected T c; public View(Model m, T c) { this.m = m; this.c = c; m.addListener(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public abstract void update(); },public class MyFrame extends View<ControllerClicks> { public MyFrame(Model m, ControllerClicks c) { super(m, c); this.setTitle("MyFrame Title"); this.setSize(400, 300); this.setLocationRelativeTo(null); this.setLayout(new BorderLayout()); MyPanel centerPanel = new MyPanel(m, c); this.add(centerPanel, BorderLayout.CENTER); JPanel topPanel = new JPanel(); this.add(topPanel, BorderLayout.PAGE_START); topPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { c.resetClicked(); } }); topPanel.add(resetButton); JButton undoButton = new JButton("Undo"); undoButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { c.undoClicked(); } }); topPanel.add(undoButton); this.setVisible(true); } @Override public void update() { repaint(); // Makes Swing call MyPanel's paintComponent method. } } import javax.swing.JLabel; public class ViewNumber extends View<Controller> { private JLabel label; public ViewNumber(Model m, Controller c) { super(m, c); this.setTitle("View Number"); this.setSize(150, 150); label = new JLabel(); update(); // Initialize the label using the model. this.add(label); this.setVisible(true); } @Override public void update() { label.setText("Number of points is: " + m.numberOfPoints()); } }
修改后的代码如下:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public abstract class View<T extends Controller> extends JFrame implements ModelListener {
protected Model m;
protected T c;
public View(Model m, T c) {
this.m = m;
this.c = c;
m.addListener(this);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
c.shutdown();
}
});
}
@Override
public abstract void update();
}
public class MyFrame extends View<ControllerClicks> {
public MyFrame(Model m, ControllerClicks c) {
super(m, c);
this.setTitle("MyFrame Title");
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setLayout(new BorderLayout());
MyPanel centerPanel = new MyPanel(m, c);
this.add(centerPanel, BorderLayout.CENTER);
JPanel topPanel = new JPanel();
this.add(topPanel, BorderLayout.PAGE_START);
topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.resetClicked();
}
});
topPanel.add(resetButton);
JButton undoButton = new JButton("Undo");
undoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.undoClicked();
}
});
topPanel.add(undoButton);
this.setVisible(true);
}
@Override
public void update() {
repaint(); // Makes Swing call MyPanel's paintComponent method.
}
}
import javax.swing.JLabel;
public class ViewNumber extends View<Controller> {
private JLabel label;
public ViewNumber(Model m, Controller c) {
super(m, c);
this.setTitle("View Number");
this.setSize(150, 150);
label = new JLabel();
update(); // Initialize the label using the model.
this.add(label);
this.setVisible(true);
}
@Override
public void update() {
label.setText("Number of points is: " + m.numberOfPoints());
}
}