我想使用 swing 在 jframe 中添加一个 jbutton。
JFrame jframe=new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setPreferredSize(new Dimension(200,200));
JButton jbutton=new JButton("a");
jframe.getContentPane().add(jbutton);
jframe.pack();
jframe.setVisible(true);
问题是 jbutton 覆盖了整个 jframe。我只想在内容 Pane 中显示默认大小的 jbutton。
最佳答案
使用 BorderLayout
允许将任何内容添加到 BorderLayout.WEST
、BorderLayout.EAST
、BorderLayout.NORTH
、BorderLayout.SOUTH
位置仅占用所需的空间,并且添加到 BorderLayout.CENTER
的任何内容都会占用所有剩余空间。
试试这个...
JFrame jframe=new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setPreferredSize(new Dimension(200,200));
Container content = jframe.getContentPane(); // Get the content pane
content.setLayout(new BorderLayout());
JButton jbutton=new JButton("a");
content.add(jbutton, BorderLayout.WEST);
jframe.pack();
jframe.setVisible(true);
关于java - JButton 覆盖整个内容 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33361773/