gpt4 book ai didi

java - 如何使 JPanel 居中(包含 JBox,但不一定)?

转载 作者:行者123 更新时间:2023-12-01 07:49:31 24 4
gpt4 key购买 nike

我已经搜索了大约 2 个小时,并阅读了方法和教程,但由于某种原因我的程序仍然没有按预期运行。

我想将包含一个包含一些按钮的框的面板放在屏幕中央。有一种方法可以使用 RigidBox 来做到这一点,但它被硬编码为我当前窗口/屏幕的大小,并且我希望无论从哪里打开它都可以居中。

    package layout;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.*;

public class Layout2 {

public static void main(String[] args) {

//The window that opens when you run the application
JFrame mainWindow = new JFrame("Solitare Anca Version 2016");
mainWindow.setSize(1000, 800);
mainWindow.setLocation(460, 140);

//The main menu bar, default in top left of the window it belongs to I think?
JMenuBar mainMenu = new JMenuBar();
//JMenu's=the menus in a bar
JMenu file = new JMenu("Home");
//Items in each menu
JMenuItem save=new JMenuItem("Save progress");
file.add(save);
JMenuItem load=new JMenuItem("Load game");
file.add(load);
JMenuItem savexit=new JMenuItem("Save & exit");
file.add(savexit);
JMenuItem close=new JMenuItem("Close game");
file.add(close);
JMenu options = new JMenu("Options");
JMenuItem changeback=new JMenuItem("Pick another card back");
options.add(changeback);
JMenuItem changecolor=new JMenuItem("Modify background color");
options.add(changecolor);
JMenuItem changelang=new JMenuItem("Change language");
options.add(changelang);
JMenuItem sound=new JMenuItem("Sound options");
options.add(sound);
JMenu help = new JMenu("Help");
JMenuItem rules=new JMenuItem("Rules");
help.add(rules);

//Ading the menus to the bar
mainMenu.add(file);
mainMenu.add(options);
mainMenu.add(help);

JPanel content=new JPanel();

JPanel butPan = new JPanel();
butPan.setBackground(Color.BLACK);

JButton okbut = new JButton("START THE GAME");
JButton exitbut = new JButton(" EXIT ");
Box butBox=Box.createVerticalBox();
butBox.add(okbut);
butBox.add(exitbut);
butPan.add(BorderLayout.SOUTH, butBox);


//Set bg of the main window to the very familiar default green
content.setBackground(Color.decode("#008001"));
content.add(BorderLayout.SOUTH,butPan);

mainWindow.setJMenuBar(mainMenu);
mainWindow.setContentPane(content);
mainWindow.setVisible(true);

// mainWindow.setContentPane(content);

}

}

在这里,我只是尝试不同的方法来移动那个该死的面板/盒子,但它就是不会移动......

最佳答案

以下是使组件居中的四种方法:

4 Centered Components

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class CenterComponent {

public static JLabel getLabel(String text) {
return getLabel(text, SwingConstants.LEFT);
}

public static JLabel getLabel(String text, int alignment) {
JLabel l = new JLabel(text, alignment);
l.setBorder(new LineBorder(Color.RED, 2));
return l;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel p = new JPanel(new GridLayout(2,2,4,4));
p.setBackground(Color.black);
p.setBorder(new EmptyBorder(4,4,4,4));

JPanel border = new JPanel(new BorderLayout());
border.add(getLabel(
"Border", SwingConstants.CENTER), BorderLayout.CENTER);
p.add(border);

JPanel gridbag = new JPanel(new GridBagLayout());
gridbag.add(getLabel("GridBag"));
p.add(gridbag);

JPanel grid = new JPanel(new GridLayout());
grid.add(getLabel("Grid", SwingConstants.CENTER));
p.add(grid);

// from @0verbose
JPanel box = new JPanel();
box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS ));

box.add(Box.createHorizontalGlue());
box.add(getLabel("Box"));
box.add(Box.createHorizontalGlue());
p.add(box);

JFrame f = new JFrame("Streeeetch me..");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}

关于java - 如何使 JPanel 居中(包含 JBox,但不一定)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41040655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com