按钮超过面板(Java)的问题
我在Java试图做的布局有问题。我在800x600框架中有2个面板。第一个面板“ GamePanel”是(600x600),第二个“ Menupanel”是(200x600)。
在Menupanel中,我尝试使用Gridlayout(部分工作)作为4行的单列组织4个按钮。这些按钮似乎已经到位,但是当悬停在它们上时,它们会扩展占据另一个面板(GamePanel)。我尝试使用setbounds将它们放置,但它们直接消失了。
这是在悬停按钮之前的工作方式。 悬停在2个按钮后,但所有4个按钮都以相同的方式显示
这是代码:
public class Layout {
Point point = new Point();
public Layout() {
JFrame window = new JFrame();
ImageIcon icon = new ImageIcon("images/icon.jpg");
//JFRAME
window.setSize(800,600);
window.setLocationRelativeTo(null);
window.setTitle("Arkanoid");
window.setUndecorated(true);
window.setIconImage(icon.getImage());
//PANELS
JPanel gamePanel = new JPanel();
gamePanel.setBackground(Color.RED);
gamePanel.setBounds(0, 0, 600, 600);
JPanel menuPanel = new JPanel(new GridLayout(4,1));
menuPanel.setBackground(Color.BLACK);
menuPanel.setBounds(600,0,200,600);
menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
window.add(gamePanel);
window.add(menuPanel);
//Buttons
JButton closeButton = new JButton("Close Me");
closeButton.addActionListener(e -> System.exit(0));
menuPanel.add(closeButton);
JButton playButton = new JButton("Play");
menuPanel.add(playButton);
JButton Button1 = new JButton("Test1");
menuPanel.add(Button1);
JButton Button2 = new JButton("Test2");
menuPanel.add(Button2);
//Labels
//SHOW
window.setVisible(true);
}
}
I'm having a problem with a layout I'm trying to do in java. I have 2 panels in a 800x600 frame. The first panel "gamePanel" is (600x600) and the second "menuPanel" is (200x600).
In the menuPanel there are 4 buttons that I tried to organize as a single column of 4 rows using gridLayout(which partially worked). The buttons appear to be in place but when hovering on them they expand occupying the other panel (gamePanel). I tried placing them using setBounds but they directly disappear.
This is how it works before hovering the buttons.
After hovering 2 buttons, but all 4 are displayed the same way
Here is the code:
public class Layout {
Point point = new Point();
public Layout() {
JFrame window = new JFrame();
ImageIcon icon = new ImageIcon("images/icon.jpg");
//JFRAME
window.setSize(800,600);
window.setLocationRelativeTo(null);
window.setTitle("Arkanoid");
window.setUndecorated(true);
window.setIconImage(icon.getImage());
//PANELS
JPanel gamePanel = new JPanel();
gamePanel.setBackground(Color.RED);
gamePanel.setBounds(0, 0, 600, 600);
JPanel menuPanel = new JPanel(new GridLayout(4,1));
menuPanel.setBackground(Color.BLACK);
menuPanel.setBounds(600,0,200,600);
menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
window.add(gamePanel);
window.add(menuPanel);
//Buttons
JButton closeButton = new JButton("Close Me");
closeButton.addActionListener(e -> System.exit(0));
menuPanel.add(closeButton);
JButton playButton = new JButton("Play");
menuPanel.add(playButton);
JButton Button1 = new JButton("Test1");
menuPanel.add(Button1);
JButton Button2 = new JButton("Test2");
menuPanel.add(Button2);
//Labels
//SHOW
window.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Oracle有一个有用的教程,创建带有swing 的GUI。使用Netbeans IDE部分跳过学习秋千。密切注意在容器中放置组件部分。
jframe
具有默认的borderlayout
,我用来放置两个jpanels
。我添加了一个
main
方法,以便可以运行GUI。我评论了图标代码,这对于未装修jframe
没有任何意义。我将setLocationRelativeto
方法移至pack
方法之后,因此jframe
实际上是中心的。这是完整的可运行代码。
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Laying Out Components Within a Container section.
A
JFrame
has a defaultBorderLayout
, which I used to place the twoJPanels
.I added a
main
method so I could run the GUI. I commented out the icon code, which doesn't make any sense for an undecoratedJFrame
. I moved thesetLocationRelativeTo
method to after thepack
method, so theJFrame
is actually centered.Here's the complete runnable code.