import javax.swing.*;
import java.awt.FlowLayout;public class Test {
public static void main(String[] args){
JFrame frame = new JFrame("My Frame");
JButton OKButton = new JButton("OK");
JButton CancelButton = new JButton("Cancel");
FlowLayout layout= new FlowLayout(FlowLayout.CENTER);
frame.setLayout(layout);
System.out.println(frame.toString());
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(OKButton);
frame.add(CancelButton); //((FlowLayout) frame.getLayout()).setAlignment(FlowLayout.LEFT); //这里会有无法转换错误
//frame.doLayout(); frame.setVisible(true);
}
}为什么输出的结果是javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=My Frame,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]呢?难道setLayout没有把FlowLayout控件添加进去?
import java.awt.FlowLayout;public class Test {
public static void main(String[] args){
JFrame frame = new JFrame("My Frame");
JButton OKButton = new JButton("OK");
JButton CancelButton = new JButton("Cancel");
FlowLayout layout= new FlowLayout(FlowLayout.CENTER);
frame.setLayout(layout);
System.out.println(frame.toString());
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(OKButton);
frame.add(CancelButton); //((FlowLayout) frame.getLayout()).setAlignment(FlowLayout.LEFT); //这里会有无法转换错误
//frame.doLayout(); frame.setVisible(true);
}
}为什么输出的结果是javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=My Frame,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]呢?难道setLayout没有把FlowLayout控件添加进去?
解决方案 »
- 改变了字符编码方式为什么没变化?
- 外观感觉
- 推荐使用Flash Get下载,限制线程
- 帮我解释一下 很简单的问题??
- 求用java做的文本编辑器的源码
- 大家对Eclipse软件如何看?
- 我是菜鸟,向学习java,请各位大侠给我推荐几本好书!!
- 急:中午交卷(改错题),请各位兄弟帮帮忙?求各位了?一定高分相送!
- 载入不了图像,拜托进来看看,我郁闷死了
- 怎么彻底杀死一个线程?
- 一个java运算符的题,谁解答下,谢谢
- JFileChooser保存文件时,输入中文文件名,就会出现保存文件的对话框失去焦点的情况,好像是调用JFIleChooser本身的输入法对话框的,请教大侠这个怎么规避,在线等,谢谢!~
public static void main(String[] args) { JFrame frame = new JFrame("My Frame"); JButton OKButton = new JButton("OK");
JButton CancelButton = new JButton("Cancel"); FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
frame.setLayout(layout); //
//System.out.println(frame.toString());你这里加的这句干什么用啊 frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.add(OKButton);
frame.add(CancelButton);
//((FlowLayout) frame.getLayout()).setAlignment(FlowLayout.LEFT);//有些东西如果之间没有某种特定的关联那么强制转换也不行啊
layout.setAlignment(FlowLayout.LEFT);//把上面换成这个
frame.doLayout(); frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
按钮控件最好还是加到模板Panel上吧,然后再将模板Panel加到框架Frame上