按钮超过面板(Java)的问题

发布于 2025-01-29 17:29:35 字数 1912 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2025-02-05 17:29:35

Oracle有一个有用的教程,创建带有swing 的GUI。使用Netbeans IDE部分跳过学习秋千。密切注意在容器中放置组件部分。

jframe具有默认的borderlayout,我用来放置两个jpanels

我添加了一个main方法,以便可以运行GUI。我评论了图标代码,这对于未装修jframe没有任何意义。我将setLocationRelativeto方法移至pack方法之后,因此jframe实际上是中心的。

这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ExampleLayout {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ExampleLayout();
            }
        });
    }

    Point point = new Point();

    public ExampleLayout() {
        JFrame window = new JFrame();
//      ImageIcon icon = new ImageIcon("images/icon.jpg");

        // JFRAME
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Arkanoid");
        window.setUndecorated(true);
//      window.setIconImage(icon.getImage());

        // PANELS
        JPanel gamePanel = new JPanel();
        gamePanel.setBackground(Color.RED);
        gamePanel.setPreferredSize(new Dimension(600, 600));

        JPanel menuPanel = new JPanel(new GridLayout(0, 1));
        menuPanel.setBackground(Color.BLACK);
        menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        menuPanel.setPreferredSize(new Dimension(200, 600));

        window.add(gamePanel, BorderLayout.CENTER);
        window.add(menuPanel, BorderLayout.EAST);

        // 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.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

}

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 default BorderLayout, which I used to place the two JPanels.

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 undecorated JFrame. I moved the setLocationRelativeTo method to after the pack method, so the JFrame is actually centered.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ExampleLayout {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ExampleLayout();
            }
        });
    }

    Point point = new Point();

    public ExampleLayout() {
        JFrame window = new JFrame();
//      ImageIcon icon = new ImageIcon("images/icon.jpg");

        // JFRAME
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Arkanoid");
        window.setUndecorated(true);
//      window.setIconImage(icon.getImage());

        // PANELS
        JPanel gamePanel = new JPanel();
        gamePanel.setBackground(Color.RED);
        gamePanel.setPreferredSize(new Dimension(600, 600));

        JPanel menuPanel = new JPanel(new GridLayout(0, 1));
        menuPanel.setBackground(Color.BLACK);
        menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        menuPanel.setPreferredSize(new Dimension(200, 600));

        window.add(gamePanel, BorderLayout.CENTER);
        window.add(menuPanel, BorderLayout.EAST);

        // 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.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文