按钮超出面板的问题 (JAVA)
Problem with buttons exceeding the Panel (JAVA)
我在 java 中尝试做的布局有问题。我在一个 800x600 的框架中有 2 个面板。第一个面板“gamePanel”是 (600x600),第二个“menuPanel”是 (200x600)。
在 menuPanel 中有 4 个按钮,我尝试使用 gridLayout(部分工作)将它们组织为 4 行的单列。这些按钮似乎就位,但当悬停在它们上面时,它们会展开并占据另一个面板(游戏面板)。我尝试使用 setBounds 放置它们,但它们直接消失了。
This is how it works before hovering the buttons.
After hovering 2 buttons, but all 4 are displayed the same way
代码如下:
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);
}
}
Oracle 有一个有用的教程,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 部分。
一个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);
}
}
我在 java 中尝试做的布局有问题。我在一个 800x600 的框架中有 2 个面板。第一个面板“gamePanel”是 (600x600),第二个“menuPanel”是 (200x600)。
在 menuPanel 中有 4 个按钮,我尝试使用 gridLayout(部分工作)将它们组织为 4 行的单列。这些按钮似乎就位,但当悬停在它们上面时,它们会展开并占据另一个面板(游戏面板)。我尝试使用 setBounds 放置它们,但它们直接消失了。
This is how it works before hovering the buttons. After hovering 2 buttons, but all 4 are displayed the same way
代码如下:
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);
}
}
Oracle 有一个有用的教程,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 部分。
一个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);
}
}