我嘗試在 java 中進行的布局有問題。我在 800x600 框架中有 2 個面板。第一個面板“gamePanel”是(600x600),第二個“menuPanel”是(200x600)。
在 menuPanel 中有 4 個按鈕,我嘗試使用 gridLayout(部分作業)將它們組織為 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);
}
}
uj5u.com熱心網友回復:
Oracle 有一個有用的教程,使用 Swing 創建 GUI。跳過使用 NetBeans IDE 學習搖擺部分。密切注意在容器內布局組件部分。
AJFrame有一個默認值BorderLayout,我用來放置兩個JPanels。
我添加了一個main方法,以便我可以運行 GUI。我注釋掉了圖示代碼,這對于 undecorated 沒有任何意義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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/477987.html
