extend jframe
时间: 2023-08-13 13:11:50 浏览: 103
在Java中,可以通过继承JFrame类来创建GUI应用程序窗口。JFrame类是Java Swing库中的一个类,它提供了创建GUI应用程序窗口的各种方法和属性。
下面是一个简单的示例代码,演示如何通过继承JFrame类创建一个简单的GUI应用程序窗口:
```
import javax.swing.JFrame;
public class MyFrame extends JFrame {
public MyFrame() {
setTitle("My Frame");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
```
在上面的代码中,我们创建了一个名为MyFrame的类,并继承了JFrame类。在构造函数中,我们设置了窗口的标题、大小、关闭操作和可见性。在main方法中,我们创建了一个MyFrame对象,这将显示GUI应用程序窗口。
当你运行这个程序时,你应该能够看到一个简单的GUI应用程序窗口,具有一个标题栏和一个空白的客户区域。你可以在这个窗口中添加各种GUI组件,例如按钮、标签、文本框等。
相关问题
Modify the MyFrame class from the code above to extend View, w.In the constructor of MyFrame, the actionPerformed method of the ActionListener of the “Reset” button must call the resetClicked method of the controller c, and the actionPerformed method of the ActionListener of the “Undo” button must call the undoClicked method of the controller c. The controller c will then itself implement the actual meaning of the button clicks. Also move the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) method call from the constructor of MyFrame to the constructor of View.
Here is the modified code:
```java
import java.awt.BorderLayout;
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.JTextField;
public class MyFrame extends View {
private Controller c;
private JTextField textField;
public MyFrame(Controller controller) {
super();
this.c = controller;
textField = new JTextField(20);
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.resetClicked();
}
});
JButton undoButton = new JButton("Undo");
undoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.undoClicked();
}
});
JPanel panel = new JPanel();
panel.add(textField);
panel.add(resetButton);
panel.add(undoButton);
add(panel, BorderLayout.CENTER);
pack();
setVisible(true);
}
@Override
public void update(String text) {
textField.setText(text);
}
@Override
public void setDefaultCloseOperation() {
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
```
In this modified code, `MyFrame` extends `View` and implements the `update` method. The `Controller` instance is passed to the constructor of `MyFrame`. The `actionPerformed` method of the `ActionListener` of the "Reset" button calls the `resetClicked` method of the `Controller`, and the `actionPerformed` method of the `ActionListener` of the "Undo" button calls the `undoClicked` method of the `Controller`. The `setDefaultCloseOperation` method call has been moved to the constructor of `View`.
Modify the MyFrame class from the code above to extend View, with the following UML specification:In the constructor of MyFrame, the actionPerformed method of the ActionListener of the “Reset” button must call the resetClicked method of the controller c, and the actionPerformed method of the ActionListener of the “Undo” button must call the undoClicked method of the controller c. The controller c will then itself implement the actual meaning of the button clicks. Also move the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) method call from the constructor of MyFrame to the constructor of View. The update method must simply call Swing’s repaint method: when the model changes (because a new point has been added, for example), the model will notify all its listeners, which will call the update method of MyFrame, which will ask Swing to repaint everything; Swing will then automatically call the paintComponent method of the MyPanel (see below) which will then redraw everything (including the new point).写一个java文件
下面是一个符合要求的Java文件,其中包含了MyFrame和View两个类的实现:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends View implements ActionListener {
private JButton resetButton;
private JButton undoButton;
private Controller c;
private MyPanel myPanel;
public MyFrame(Controller c) {
super();
this.c = c;
this.resetButton = new JButton("Reset");
this.undoButton = new JButton("Undo");
this.resetButton.addActionListener(this);
this.undoButton.addActionListener(this);
this.myPanel = new MyPanel(c.getModel());
this.getContentPane().add(myPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.add(resetButton);
buttonPanel.add(undoButton);
this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == resetButton) {
c.resetClicked();
} else if (e.getSource() == undoButton) {
c.undoClicked();
}
}
}
class View extends JFrame {
public View() {
super();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setMinimumSize(new Dimension(400, 400));
this.setVisible(true);
}
public void update() {
this.repaint();
}
}
class MyPanel extends JPanel {
private Model model;
public MyPanel(Model model) {
super();
this.model = model;
this.setBackground(Color.WHITE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
ArrayList<Point> points = model.getPoints();
for (int i = 0; i < points.size(); i++) {
Point p = points.get(i);
g.fillOval(p.x - 5, p.y - 5, 10, 10);
}
}
}
```
其中,View类是一个抽象类,它包含了窗口的基本属性和方法,而MyFrame类则是继承自View类的具体窗口实现,它负责显示画板和按钮,并且处理按钮的点击事件。MyPanel类则是一个画板,负责绘制所有的点。
在构造函数中,MyFrame类会创建一个Controller对象c,并将其保存在成员变量中。同时,它会创建两个按钮resetButton和undoButton,分别用于重置和撤销操作,然后将这两个按钮添加到窗口底部的面板中。此外,它还会创建一个MyPanel对象,并将其添加到窗口的中央区域。
在MyFrame类中,实现了ActionListener接口,并覆盖了actionPerformed方法,以处理按钮的点击事件。当resetButton被点击时,它会调用Controller对象c的resetClicked方法;当undoButton被点击时,它会调用Controller对象c的undoClicked方法。
在View类中,实现了update方法,它会在模型发生变化时被调用,以便更新窗口的显示内容。在本例中,update方法只是简单地调用了Swing的repaint方法,以触发窗口的重绘操作。
在MyPanel类中,实现了paintComponent方法,它会在窗口需要重绘时被调用,以绘制所有的点。具体来说,它会从模型中获取所有的点,并将它们绘制成实心圆。
阅读全文