Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI:JButton几乎覆盖整个屏幕_Java_Swing_Jframe_Jbutton_Layout Manager - Fatal编程技术网

Java GUI:JButton几乎覆盖整个屏幕

Java GUI:JButton几乎覆盖整个屏幕,java,swing,jframe,jbutton,layout-manager,Java,Swing,Jframe,Jbutton,Layout Manager,我在使用JFrame和jbutton时遇到了一个问题。我试图将我的JButton居中,但是当我这样做时,它几乎覆盖了整个屏幕。知道是什么引起的吗 下面是一张关于它外观的图片: 这是我的代码: package character; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.io.IOException; /** * Created by Niknea on 6/28/

我在使用
JFrame
和jbutton时遇到了一个问题。我试图将我的
JButton
居中,但是当我这样做时,它几乎覆盖了整个屏幕。知道是什么引起的吗

下面是一张关于它外观的图片:

这是我的代码:

package character;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 * Created by Niknea on 6/28/14.
 */
public class characterSelector{


    JFrame  cselectorButtons, clogo;

    JLabel logo, characterName, label;

    JButton male, female;


    public characterSelector(){

        this.createCharacterSelector();

    }


    public void createCharacterSelector() {

        try {
            label = new JLabel(new ImageIcon(ImageIO.read(getClass()
                    .getResource("/resources/Grass_Background.jpg"))));
            cselectorButtons = new JFrame("SupremeSoccer");
            logo = new JLabel(new ImageIcon(ImageIO.read(this.getClass().getResource("/resources/Character_Selector_Image.png"))));
            characterName = new JLabel("<Character Name>");
            characterName.setFont(new Font(characterName.getFont().getName(),
                    Font.HANGING_BASELINE, 50));

            /*
            Buttons
             */

            male = new JButton("Male");


            ////******************////
            //// END OF BUTTONS  ////
            ////****************////
            cselectorButtons.add(logo);
            cselectorButtons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            cselectorButtons.setContentPane(label);
            cselectorButtons.setLayout(new BorderLayout());
            cselectorButtons.add(logo, BorderLayout.NORTH);
         cselectorButtons.add(male, BorderLayout.CENTER);
            cselectorButtons.pack();
            cselectorButtons.setLocationRelativeTo(null);
            cselectorButtons.setVisible(true);
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }
}
包字符;
导入javax.imageio.imageio;
导入javax.swing.*;
导入java.awt.*;
导入java.io.IOException;
/**
*Niknea于2014年6月28日创建。
*/
公共类字符选择器{
JFrame cselectorButtons,clogo;
JLabel徽标、字符名称、标签;
JButton雄性,雌性;
公共字符选择器(){
这是.createCharacterSelector();
}
public void createCharacterSelector(){
试一试{
label=newjlabel(newimageIcon(ImageIO.read)(getClass())
.getResource(“/resources/Grass_Background.jpg”);
cselectorButtons=新JFrame(“SupremoAccer”);
logo=newjlabel(newimageicon(ImageIO.read(this.getClass().getResource(“/resources/Character\u Selector\u Image.png”)));
characterName=新的JLabel(“”);
characterName.setFont(新字体(characterName.getFont().getName(),
Font.u(基线,50));
/*
按钮
*/
男性=新的JButton(“男性”);
////******************////
////按钮末端////
////****************////
cselectorButtons.add(徽标);
cselectorButtons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cselectorButtons.setContentPane(标签);
setLayout(新的BorderLayout());
cselectorButtons.add(logo,BorderLayout.NORTH);
cselectorButtons.add(男性,BorderLayout.CENTER);
cselectorButtons.pack();
cselectorButtons.setLocationRelativeTo(空);
cselectorButtons.setVisible(true);
}捕获(IOEXP异常){
exp.printStackTrace();
}
}
}
再次感谢

知道是什么引起的吗

这是
BorderLayout
的默认行为。当考虑到其他(
北部
南部
东部
西部
)位置时,
中部
位置的组件将占据父组件可用的最大空间量

根据您所要达到的目的,您可以考虑创建另一个<代码> jPoCTs/COD>(设置它的代码<不透明状态> <代码> false <代码>,这样它是透明的,并使用诸如<代码> GridLayout < /代码>或<代码> GridBagLayout <代码> > 看一看更多的想法

已更新

基于你的观点,我改变了

part2 = new JPanel();

而且

更新了其他示例

首先将您的需求分解为各个容器,并关注每个容器的布局需求,然后将它们全部构建回单个容器

这将使以后更改它们更容易,也使控制它们更容易

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ExampleLayout {

    public static void main(String[] args) {
        new ExampleLayout();
    }

    public ExampleLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private HeaderPane header;
        private ScorePane score;
        private CharacterSelectionPane characterSelection;

        public TestPane() {

            setLayout(new BorderLayout());

            JLabel background = new JLabel();

            try {
                BufferedImage img = ImageIO.read(getClass().getResource("/Grass.jpg"));
                background.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            add(background);

            background.setLayout(new GridBagLayout());

            header = new HeaderPane();
            score = new ScorePane();
            characterSelection = new CharacterSelectionPane();

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.BOTH;

            background.add(header, gbc);
            background.add(score, gbc);
            gbc.weighty = 1;
            background.add(characterSelection, gbc);
        }
    }

    public class HeaderPane extends JPanel {

        public HeaderPane() {
            setLayout(new BorderLayout());
            JLabel label = new JLabel("Character Selection");
            label.setForeground(Color.WHITE);
            label.setFont(label.getFont().deriveFont(Font.BOLD, 48f));
            label.setHorizontalAlignment(JLabel.CENTER);
            add(label);
            setOpaque(false);
        }

    }

    public class ScorePane extends JPanel {

        public ScorePane() {
            JLabel label = new JLabel("[-][-[]-][-]");
            label.setForeground(Color.YELLOW);
            add(label);
            setOpaque(false);
        }

    }

    public class CharacterSelectionPane extends JPanel {

        private JButton btMale;
        private JButton btFemale;
        private JTextField tfName;
        private JButton btContinue;

        public CharacterSelectionPane() {
            setOpaque(false);
            setLayout(new GridBagLayout());

            btMale = new JButton("Male");
            btFemale = new JButton("Female");
            btContinue = new JButton("Continue");
            tfName = new JTextField(10);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(btMale, gbc);
            gbc.gridx++;
            add(btFemale, gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            add(new JLabel("Name:"), gbc);
            gbc.gridx++;
            add(tfName, gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(btContinue, gbc);
        }

    }

}

知道是什么引起的吗

这是
BorderLayout
的默认行为。当考虑到其他(
北部
南部
东部
西部
)位置时,
中部
位置的组件将占据父组件可用的最大空间量

根据您所要达到的目的,您可以考虑创建另一个<代码> jPoCTs/COD>(设置它的代码<不透明状态> <代码> false <代码>,这样它是透明的,并使用诸如<代码> GridLayout < /代码>或<代码> GridBagLayout <代码> > 看一看更多的想法

已更新

基于你的观点,我改变了

part2 = new JPanel();

而且

更新了其他示例

首先将您的需求分解为各个容器,并关注每个容器的布局需求,然后将它们全部构建回单个容器

这将使以后更改它们更容易,也使控制它们更容易

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ExampleLayout {

    public static void main(String[] args) {
        new ExampleLayout();
    }

    public ExampleLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private HeaderPane header;
        private ScorePane score;
        private CharacterSelectionPane characterSelection;

        public TestPane() {

            setLayout(new BorderLayout());

            JLabel background = new JLabel();

            try {
                BufferedImage img = ImageIO.read(getClass().getResource("/Grass.jpg"));
                background.setIcon(new ImageIcon(img));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            add(background);

            background.setLayout(new GridBagLayout());

            header = new HeaderPane();
            score = new ScorePane();
            characterSelection = new CharacterSelectionPane();

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.BOTH;

            background.add(header, gbc);
            background.add(score, gbc);
            gbc.weighty = 1;
            background.add(characterSelection, gbc);
        }
    }

    public class HeaderPane extends JPanel {

        public HeaderPane() {
            setLayout(new BorderLayout());
            JLabel label = new JLabel("Character Selection");
            label.setForeground(Color.WHITE);
            label.setFont(label.getFont().deriveFont(Font.BOLD, 48f));
            label.setHorizontalAlignment(JLabel.CENTER);
            add(label);
            setOpaque(false);
        }

    }

    public class ScorePane extends JPanel {

        public ScorePane() {
            JLabel label = new JLabel("[-][-[]-][-]");
            label.setForeground(Color.YELLOW);
            add(label);
            setOpaque(false);
        }

    }

    public class CharacterSelectionPane extends JPanel {

        private JButton btMale;
        private JButton btFemale;
        private JTextField tfName;
        private JButton btContinue;

        public CharacterSelectionPane() {
            setOpaque(false);
            setLayout(new GridBagLayout());

            btMale = new JButton("Male");
            btFemale = new JButton("Female");
            btContinue = new JButton("Continue");
            tfName = new JTextField(10);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(btMale, gbc);
            gbc.gridx++;
            add(btFemale, gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            add(new JLabel("Name:"), gbc);
            gbc.gridx++;
            add(tfName, gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(btContinue, gbc);
        }

    }

}


在底部区域使用
BoxLayout
或(更简单的)
GridBagLayout
,如中所示


在底部区域使用
BoxLayout
或(更简单的)
GridBagLayout
,如中所示


我过去遇到过一些问题,涉及使用JPanel将对象居中。我会被要求这样做吗?还是把JPanel放在中间?另外,我应该创建一个像主框架一样的新框架,还是第一个可以作为“主”框架?再次感谢。使用
JPanel
而不是按钮,它将被添加到
JLabel
中心位置,但是,只要它是透明的,你甚至不知道它在那里。类似于
GridBagLayout
的内容将允许您添加将位于容器(面板)中心周围的组件。如果你需要在视图之间切换,你可能会考虑使用A/WORD你可能知道一个好的向导我可以检查一下所有这些吗?谢谢。@Niknea你已经有导游了。你这次真的要看它吗?我在过去遇到过一些问题,涉及到使用JPanel将一个对象居中。我会被要求这样做吗?还是把JPanel放在中间?另外,我应该创建一个像主框架一样的新框架,还是第一个可以作为“主”框架?再次感谢。使用
JPanel
而不是按钮,它将被添加到
JLabel
中心位置,但是,只要它是透明的,你甚至不知道它在那里。类似于
GridBagLayout
的东西将允许