gpt4 book ai didi

java - 无法将按钮置于右中位置

转载 作者:行者123 更新时间:2023-12-02 11:15:48 26 4
gpt4 key购买 nike

我正在尝试弄清楚如何将按钮放置在右中位置。我添加了到目前为止我所做的事情,并且我将添加一张我想要的图。

我试图了解如何在 Swing 中确定我想要的位置,但无法真正理解每种布局的优点。

到目前为止我的代码:

package Game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.Timer;


public class MainWindow extends JFrame implements ActionListener {

private JButton exit;
private JButton start_Game;
private ImageIcon puzzleBackground;
// private JLabel back_Label;
// private GridBagConstraints grid = new GridBagConstraints();
private JPanel menu;

public MainWindow()
{
super("Welcome");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(450,300);
setLocationRelativeTo(null);
this.setLayout(new FlowLayout(FlowLayout.RIGHT));
menu = new JPanel();
menu.setLayout(new BorderLayout());
//setResizable(false);

//===== Background =====
puzzleBackground = new ImageIcon("MyBackground.jpg");
setContentPane(new JLabel(puzzleBackground));

exit = new JButton("Exit");
menu.add(exit, BorderLayout.CENTER);
exit.addActionListener(this);

start_Game = new JButton("Start to play");
menu.add(start_Game, BorderLayout.SOUTH);
exit.addActionListener(this);
start_Game.addActionListener(this);

//
// back_Label = new JLabel(puzzleBackground);
// back_Label.setLayout(new BorderLayout());

//===== Buttons =====
// back_Label.add(exit,BorderLayout.CENTER);
//
// back_Label.add(start_Game,BorderLayout.EAST);
//
add(menu);
setVisible(true);
}


public static void main(String args[])
{
MainWindow a = new MainWindow();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exit)
{
System.exit(0);
}
else
{
//open start up window.
}

}

}

最佳答案

添加 BG 图像的更好方法是使用自定义绘制的 JPanel。然后设置面板的布局并向其中添加其他面板或组件。请注意,此处的按钮并未在很大程度上出现,因为它们被添加到JLabel中。

这里有一个替代方案,其工作原理相同,红色面板是自定义绘制背景图像的面板,而 menu 面板设置为透明(查找 opaque 方法)。

enter image description here

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

public class MainWindow extends JFrame {

private JPanel menu;
private JPanel contentPane = new JPanel(new BorderLayout(4,4));

public MainWindow() {
super("Welcome");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//setSize(450, 300); // use pack() instead
setContentPane(contentPane);
contentPane.setBorder(new EmptyBorder(8,8,8,8));
contentPane.setBackground(Color.RED);
contentPane.add(new JLabel(new ImageIcon(
new BufferedImage(400,200,BufferedImage.TYPE_INT_RGB))));

menu = new JPanel(new GridLayout(0,1,10,10));

menu.add(new JButton("Exit"), BorderLayout.CENTER);
menu.add(new JButton("Start to play"), BorderLayout.SOUTH);

JPanel menuCenterPanel = new JPanel(new GridBagLayout());
menuCenterPanel.add(menu);
add(menuCenterPanel, BorderLayout.LINE_END);

pack();
setLocationRelativeTo(null); // do AFTER pack()
setMinimumSize(getSize());
setVisible(true);
}

public static void main(String args[]) {
MainWindow a = new MainWindow();
}
}

关于java - 无法将按钮置于右中位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50290010/

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