import javax.swing.*;
public class TextFlowLayout
{
public static void main(String[] args)
{
JFrame frame=new JFrame("qFlowLayout");
JButton button1=new JButton("ok");
JButton button2=new JButton("open");
JButton button3=new JButton("Exit");
frame. setLayout(new FlowLayout());//这个地方怎么说setLayout对FlowLayout不适用.?
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setSize(100,100);
frame.setVisible(true);
}}
public class TextFlowLayout
{
public static void main(String[] args)
{
JFrame frame=new JFrame("qFlowLayout");
JButton button1=new JButton("ok");
JButton button2=new JButton("open");
JButton button3=new JButton("Exit");
frame. setLayout(new FlowLayout());//这个地方怎么说setLayout对FlowLayout不适用.?
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setSize(100,100);
frame.setVisible(true);
}}
//你没有导入包:FlowLayout 在java.awt下.//import java.awt.FlowLayout; //加上这句就OK了//import javax.swing.*;
import javax.swing.JFrame;public class TextFlowLayout { public static void main(String[] args) {
JFrame frame = new JFrame("qFlowLayout");
JButton button1 = new JButton("ok");
JButton button2 = new JButton("open");
JButton button3 = new JButton("Exit");
frame.getContentPane().setLayout(new FlowLayout());// 这个地方怎么说setLayout对FlowLayout不适用.?
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.getContentPane().add(button3);
frame.setSize(100, 100);
frame.setVisible(true); }}
我将你代码修改了下,我像应该是 JDK版本问题,我以前也遇到过这种情况的,主要是你要把组件放在容器里。所以你必须先用frame.getContentPane()得到他的Container 然后才可以add。
我这里有段话,是人家那里摘抄下来,发出来给你看看
Do not use javax.swing.JFrame.setLay
out() use javax.swing.JFrame.getContentPane().setLayout() instead
不要使用.swing.JFrame.setLayout() 用javax.swing.JFrame.getContentPane().setLayout() 替换在过去的J2SE版本中,部件(components)不能直接添加到最高级(top level)的Swing容器,这些容器包括:JFrame, JDialog, JWindow, JApplet和JInternalFrame. 例如现在有一个JFrame的instance命名为aFrame。我们要添加一个命名为aComponent的部件(比如一个JPanel)到JFrame里。以往我们需要调用JFrame的 getContentPane()办法。如下:
aFrame.getContentPane().add(aComponent)在J2SE5.0中,我们可以直接添加部件到最高级的Swing容器,也可以直接在容器内设置布局管理(Layout Manager)和删除部件。这个变化允许我们可以省去调用getContentPane()而直接在容器内应用add(),setLayout()和 remove()。然而,我们还是不能忽略了ContentPane,即使我们可能将不会再使用ContentPane来添加部件到容器。现在用SWING来构造界面是不允许把组件直接放到顶级窗口类的
而要放到顶级窗口的内容窗格上
所以,你把东西放到顶级窗口类上要先得到顶级窗口类的内容窗格,叫getContentPane()
这个方法返回一个Container对象的引用,然后你就可以用这个引用去添加组件了轻重量级组件,需要在JFrame里的ContentPane对象里放置重量级的容器