Java Swing设置JPanel大小
有谁能指出我在使用java swing gui代码时哪里出了问题。我试图在JPanel中添加两个按钮,然后在设置大小后将其添加到一个框架中,但它似乎没有响应传递给它的Java Swing设置JPanel大小,java,swing,layout,layout-manager,Java,Swing,Layout,Layout Manager,有谁能指出我在使用java swing gui代码时哪里出了问题。我试图在JPanel中添加两个按钮,然后在设置大小后将其添加到一个框架中,但它似乎没有响应传递给它的setSize值 public Test() { GridLayout layout = new GridLayout(1, 2); //this.setLayout(layout); this.setSize(700, 700); this.setDefaultCloseOperation(JFra
setSize
值
public Test() {
GridLayout layout = new GridLayout(1, 2);
//this.setLayout(layout);
this.setSize(700, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel = new JPanel();
buttonPanel.setSize(new Dimension(30, 100));
JButton rectButton = new JButton("Rectangle");
JButton ovalButton = new JButton("Oval");
buttonPanel.add(rectButton);
buttonPanel.add(ovalButton);
this.add(buttonPanel);
this.add(new PaintSurface());
this.setVisible(true);
}
这可能无法回答您当前的问题…但是
GridLayout layout = new GridLayout(1, 2);
this.setLayout(layout);
// You're original code...
// Why are you using `BorderLayout.CENTER` on a `GridLayout`
this.add(new PaintSurface(), BorderLayout.CENTER);
您将布局设置为GridLayout
,但您正在使用BorderLayout
约束来应用其中一个组件
另外,确保代码中的其他位置没有调用Test\pack
,因为这将覆盖setSize
更新(从更改为问题)
请记住,JFrame
的默认布局管理器是BorderLayout
,因此即使您正在调用buttonPanel.setSize
,布局管理器仍可能会覆盖它
我会通读一遍,找到一个最符合您要求的布局管理器
如果找不到一个,请考虑使用不同布局管理器的复合组件,使布局更接近您想要实现的。
< P>根据您的更新答案,您没有在任何东西上设置布局。 无论如何,如果您使用LayoutManager(您应该这样做),那么调用setSize()/setBounds()/setLocation()
是毫无意义的,因为它将被LayoutManager覆盖(这实际上是它的工作)
通过调用this.add(buttonPanel),猜测您的Test
类扩展了JFrame
;添加(新漆面())
您正在向内容窗格添加具有相同约束的两个组件(BorderLayout.CENTER
,因为BorderLayout
是JFrame
的内容窗格的默认布局管理器)
考虑一下阅读这本书
仅供参考,尽管还远远不够完美,但这表明有些东西“起作用”:
好的,我给你一个解决方案:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Cobie extends JFrame{
JButton rectButton = new JButton("Rectangle");
JButton ovalButton = new JButton("Oval");
JPanel buttonPanel = new JPanel();
JPanel paintSurface = new JPanel();
public Cobie(){
setLayout(new GridLayout(2,1));
buttonPanel.setBackground(Color.RED);
paintSurface.setBackground(Color.BLUE);
buttonPanel.add(rectButton);
buttonPanel.add(ovalButton);
add(buttonPanel);
add(paintSurface);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
Cobie c = new Cobie();
c.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
c.setSize(600,400); //Avoid using this method
c.setVisible(true);
}
});
}
}
你的代码没有多大意义。您正在使用GridLayout,但正在向BorderLayout.CENTER添加一个漆面。你不应该设定尺寸。让布局根据其布局的组件的大小来决定适当的大小。这种编辑也不好。
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Cobie extends JFrame{
JButton rectButton = new JButton("Rectangle");
JButton ovalButton = new JButton("Oval");
JPanel buttonPanel = new JPanel();
JPanel paintSurface = new JPanel();
public Cobie(){
setLayout(new GridLayout(2,1));
buttonPanel.setBackground(Color.RED);
paintSurface.setBackground(Color.BLUE);
buttonPanel.add(rectButton);
buttonPanel.add(ovalButton);
add(buttonPanel);
add(paintSurface);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
Cobie c = new Cobie();
c.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
c.setSize(600,400); //Avoid using this method
c.setVisible(true);
}
});
}
}