里面应该有个菜单栏的,实际上就一个JFrame框架public class RootFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel centerPanel;
@SuppressWarnings("unchecked")
public RootFrame(String title) throws IOException{
super();
// setIconImages(new ICOFile(SourceLoader.AbsolutePath("files/images/menu/application.ico")).getImages());
setTitle(title);
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
menuBar();
toolBar();
center();
statusBar();
setVisible(true);
}
public void menuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu config = new JMenu();
config.setText("配置(Q)");
config.setMnemonic('Q');
JMenuItem switch_db = new JMenuItem();
switch_db.setText("切换数据库(S)");
switch_db.setMnemonic('S');
// Image image = Toolkit.getDefaultToolkit().getImage(
// getClass().getResource("/images/menu/搜狗浏览器截图.jpg"));
// ImageIcon icon = new ImageIcon(image);
// switch_db.setIcon(icon);
switch_db.addActionListener(new Action());
config.add(switch_db);
menuBar.add(config);
setJMenuBar(menuBar);
}
public void center() {
// this.centerPanel = new JPanel();
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
// System.out.println("worec.form.RootFrame..center");
}
// System.out.println("worec.form.RootFrame..center");
this.centerPanel.setLayout(new BorderLayout());
JButton b = new JButton("1432");
this.centerPanel.add(b);
// this.centerPanel.setVisible(true);
getContentPane().add(this.centerPanel, BorderLayout.CENTER);
}
public void toolBar() {
}
public void statusBar() {
}
public Component add(Component comp) {
this.centerPanel.add(comp, BorderLayout.CENTER);
// System.out.println("worec.form.RootFrame..add");
// this.centerPanel.invalidate();
return this.centerPanel;
}
public Component add(Component comp, int position) {
this.centerPanel.add(comp, position);
// this.centerPanel.invalidate();
return this.centerPanel;
}
public void setLayout(LayoutManager manager) {
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
}
this.centerPanel.setLayout(manager);
}
}import java.io.IOException;import worec.form.RootFrame;public class RootFrameTest { public RootFrameTest() {
}
public void init() throws IOException {
RootFrame r = new RootFrame("RootFrameTest");
// JButton b = new JButton("普通啊");
// r.add(b);
// r.validate();
r.setVisible(true);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
RootFrameTest rft = new RootFrameTest();
rft.init();
}
}
private static final long serialVersionUID = 1L;
private JPanel centerPanel;
@SuppressWarnings("unchecked")
public RootFrame(String title) throws IOException{
super();
// setIconImages(new ICOFile(SourceLoader.AbsolutePath("files/images/menu/application.ico")).getImages());
setTitle(title);
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
menuBar();
toolBar();
center();
statusBar();
setVisible(true);
}
public void menuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu config = new JMenu();
config.setText("配置(Q)");
config.setMnemonic('Q');
JMenuItem switch_db = new JMenuItem();
switch_db.setText("切换数据库(S)");
switch_db.setMnemonic('S');
// Image image = Toolkit.getDefaultToolkit().getImage(
// getClass().getResource("/images/menu/搜狗浏览器截图.jpg"));
// ImageIcon icon = new ImageIcon(image);
// switch_db.setIcon(icon);
switch_db.addActionListener(new Action());
config.add(switch_db);
menuBar.add(config);
setJMenuBar(menuBar);
}
public void center() {
// this.centerPanel = new JPanel();
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
// System.out.println("worec.form.RootFrame..center");
}
// System.out.println("worec.form.RootFrame..center");
this.centerPanel.setLayout(new BorderLayout());
JButton b = new JButton("1432");
this.centerPanel.add(b);
// this.centerPanel.setVisible(true);
getContentPane().add(this.centerPanel, BorderLayout.CENTER);
}
public void toolBar() {
}
public void statusBar() {
}
public Component add(Component comp) {
this.centerPanel.add(comp, BorderLayout.CENTER);
// System.out.println("worec.form.RootFrame..add");
// this.centerPanel.invalidate();
return this.centerPanel;
}
public Component add(Component comp, int position) {
this.centerPanel.add(comp, position);
// this.centerPanel.invalidate();
return this.centerPanel;
}
public void setLayout(LayoutManager manager) {
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
}
this.centerPanel.setLayout(manager);
}
}import java.io.IOException;import worec.form.RootFrame;public class RootFrameTest { public RootFrameTest() {
}
public void init() throws IOException {
RootFrame r = new RootFrame("RootFrameTest");
// JButton b = new JButton("普通啊");
// r.add(b);
// r.validate();
r.setVisible(true);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
RootFrameTest rft = new RootFrameTest();
rft.init();
}
}
public void menuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu config = new JMenu();
config.setText("配置(Q)");
config.setMnemonic('Q');
JMenuItem switch_db = new JMenuItem();
switch_db.setText("切换数据库(S)");
switch_db.setMnemonic('S');
// Image image = Toolkit.getDefaultToolkit().getImage(
// getClass().getResource("/images/menu/搜狗浏览器截图.jpg"));
// ImageIcon icon = new ImageIcon(image);
// switch_db.setIcon(icon);
switch_db.addActionListener(new Action());
config.add(switch_db);
menuBar.add(config);
setJMenuBar(menuBar);
}
public void setLayout(LayoutManager manager) {
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
}
this.centerPanel.setLayout(manager);
}
在Swing组件中JFrame已经有这个方法了,即使你不调用它,他也会自动执行。改一下名字就好。看我的截图;
根本原因就是在
public void center() {
// this.centerPanel = new JPanel();
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
// System.out.println("worec.form.RootFrame..center");
}
// System.out.println("worec.form.RootFrame..center");
this.centerPanel.setLayout(new BorderLayout());
JButton b = new JButton("1432");
this.centerPanel.add(b);
// this.centerPanel.setVisible(true);
getContentPane().add(this.centerPanel, BorderLayout.CENTER);
}
中调用了
this.centerPanel.setLayout(new BorderLayout());
再加上重写后的setLayout代码是个残废
public void setLayout(LayoutManager manager) {
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
}
this.centerPanel.setLayout(manager);
}
导致每次都是新的布局覆盖了旧的布局比较笨的解决办法是修改setLayout方法,硬编码写入
public void setLayout(LayoutManager manager) {
if(this.borderLayout == null) {
this.borderLayout = new BorderLayout();
}
super.setLayout(this.borderLayout);
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
}
this.centerPanel.setLayout(manager);
}
问题就解决了。虽然如此,还是谢谢您了
根本原因就是在
public void center() {
// this.centerPanel = new JPanel();
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
// System.out.println("worec.form.RootFrame..center");
}
// System.out.println("worec.form.RootFrame..center");
this.centerPanel.setLayout(new BorderLayout());
JButton b = new JButton("1432");
this.centerPanel.add(b);
// this.centerPanel.setVisible(true);
getContentPane().add(this.centerPanel, BorderLayout.CENTER);
}
中调用了
this.centerPanel.setLayout(new BorderLayout());
再加上重写后的setLayout代码是个残废
public void setLayout(LayoutManager manager) {
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
}
this.centerPanel.setLayout(manager);
}
导致每次都是新的布局覆盖了旧的布局比较笨的解决办法是修改setLayout方法,硬编码写入
public void setLayout(LayoutManager manager) {
if(this.borderLayout == null) {
this.borderLayout = new BorderLayout();
}
super.setLayout(this.borderLayout);
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
}
this.centerPanel.setLayout(manager);
}
问题就解决了。虽然如此,还是谢谢您了饿,这个也不对在代码
public void setLayout(LayoutManager manager) {
if(this.centerPanel == null) {
this.centerPanel = new JPanel();
}
this.centerPanel.setLayout(manager);
}
加入
super.setLayout(this.borderLayout);才是解决问题的关键