public class Myframe extends JFrame { public Myframe() { this.setSize(600,900); this.setLayout(new GridBagLayout()); this.setDefaultCloseOperation(3); //up up u=new up(); GridBagConstraints upc = new GridBagConstraints(); upc.gridx = 0; upc.gridy = -10; upc.fill=upc.BOTH; this.add(u,upc); //Middle Middle m=new Middle(); GridBagConstraints mdc = new GridBagConstraints(); mdc.gridx = 0; mdc.gridy = 10; this.add(m,mdc); //Buttom Middle b=new Middle(); GridBagConstraints bc = new GridBagConstraints(); bc.gridx = 0; bc.gridy = 20; this.add(b,bc);为什么frame里的组件都在中间
时间: 2023-06-19 20:09:56 浏览: 117
你的代码中,up 组件的 GridBagConstraints 中设置了 upc.gridy = -10,这会将 up 组件向上移动,可能会导致 up 组件不可见。同时,Middle b 组件的 GridBagConstraints 中设置了 bc.gridy = 20,这会将 b 组件放在中间下方,而不是底部。
至于为什么所有组件都在中间,可能是因为你的布局管理器是 GridBagLayout,它会根据组件的约束条件和空间来动态地调整组件的位置和大小,因此在没有设置好组件的约束条件时,所有组件可能会被放在中间。你可以根据自己的需要设置每个组件的约束条件,来调整它们的位置和大小。
相关问题
import java.awt.event.ActionListener; import javax.swing.JFrame; public class MyFrame extends JFrame implements ActionListener{ JButton b; JTextArea ar;报错
根据您提供的代码,报错可能是由于没有正确导入`JButton`和`JTextArea`类所致。请确保在文件的顶部添加以下导入语句:
```java
import javax.swing.JButton;
import javax.swing.JTextArea;
```
此外,您还需要实现`ActionListener`接口,因为您的类声明中指定了`implements ActionListener`。这意味着您的类需要提供一个实现`ActionListener`接口的方法。请确保在您的类中添加以下方法:
```java
@Override
public void actionPerformed(ActionEvent e) {
// 处理按钮点击事件的代码
}
```
请注意,这只是解释了您提供的代码中可能导致报错的问题。如果问题仍然存在,请提供更多的代码和错误信息,以便我能够更好地帮助您解决问题。
帮我根据以下要求: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());
}
}
阅读全文