java 我如何重新排列我的jbutton以使其在顶部的行中

72qzrwbm  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(122)

我的jbutton是在一个不对称的行。我希望他们是在2个对称的行在顶部,因为我认为这将看起来更好。我将如何做到这一点?这是我的代码。

package com.company;

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

class Fantasyrpglifesim {
    private  static int count = 1;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel mainPanel = new JPanel();
        JPanel westPanel = new JPanel();
        JPanel eastPanel = new JPanel();

        JButton age = new JButton("Age up");
        mainPanel.add(age);
        JButton character = new JButton("Character");
        mainPanel.add(character);
        JButton worldmap = new JButton("World map");
        mainPanel.add(worldmap);
        JButton setings = new JButton("Settings");
        mainPanel.add(setings);
        JButton jobs = new JButton("Jobs");
        westPanel.add(jobs);
        JButton manegement = new JButton("Management");
        westPanel.add(manegement);
        JButton family = new JButton("Relationships");
        westPanel.add(family);
        JButton cfafting = new JButton("Crafting");
        westPanel.add(cfafting);
        JButton newgame = new JButton("New game");
        eastPanel.add(newgame);
        JButton titorial = new JButton("tutorial");
        eastPanel.add(titorial);
        JButton log = new JButton("Log");
        eastPanel.add(log);
        JButton acheiments = new JButton("Achievements");
        eastPanel.add(acheiments);
        for (int x=0; x<14;x++){

        }
        westPanel.setVisible(true);
        eastPanel.setVisible(true);
        frame.getContentPane().add(BorderLayout.NORTH,mainPanel);
        frame.getContentPane().add(BorderLayout.WEST,westPanel);
        frame.getContentPane().add(BorderLayout.EAST,eastPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1285, 678);
        frame.setVisible(true);

    }
}

我试过重新排列jpanel,但没有成功。我只想让它们排成行。

dly7yett

dly7yett1#

您应该使用GridLayout在矩形网格中排列组件。
下面的例子有一列两行的GridLayout。它只是在一行中布局按钮,因为FlowLayout是JPanel的默认布局管理器。

JPanel row1Panel = new JPanel();
    JPanel row2Panel = new JPanel();
    mainPanel.setLayout(new GridLayout(2,1));
    mainPanel.add(row1Panel);
    mainPanel.add(row2Panel);

    JButton age = new JButton("Age up");
    row1Panel.add(age);
    JButton character = new JButton("Character");
    row1Panel.add(character);

    JButton jobs = new JButton("Jobs");
    row2Panel.add(jobs);
    JButton manegement = new JButton("Management");
    row2Panel.add(manegement);
myzjeezk

myzjeezk2#

Oracle有一个有用的教程Creating a GUI With Swing。跳过使用NetBeans IDE学习Swing部分。特别注意Laying Out Components Within a Container部分。
我重新排列了您的代码并创建了以下GUI。

我使用了一个GridLayout来创建JButtonJPanel
我将JFrame的创建与JPanel的创建分开,这使得代码更易于阅读和理解,并允许我一次专注于GUI的一个部分。
下面是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.GridLayout;

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

public class Fantasyrpglifesim implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Fantasyrpglifesim());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createButtonPanel(), BorderLayout.NORTH);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 6, 5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JButton age = new JButton("Age up");
        panel.add(age);
        JButton character = new JButton("Character");
        panel.add(character);
        JButton worldmap = new JButton("World map");
        panel.add(worldmap);
        JButton setings = new JButton("Settings");
        panel.add(setings);
        JButton jobs = new JButton("Jobs");
        panel.add(jobs);
        JButton manegement = new JButton("Management");
        panel.add(manegement);
        JButton family = new JButton("Relationships");
        panel.add(family);
        JButton cfafting = new JButton("Crafting");
        panel.add(cfafting);
        JButton newgame = new JButton("New game");
        panel.add(newgame);
        JButton titorial = new JButton("tutorial");
        panel.add(titorial);
        JButton log = new JButton("Log");
        panel.add(log);
        JButton acheiments = new JButton("Achievements");
        panel.add(acheiments);

        return panel;
    }

}

相关问题