繁体   English   中英

有什么方法可以显示JLabel并使布局保持为空?

[英]Is there any way to display JLabel while keeping layout at null?

我试图使两个按钮之间相互之间留一点点空间。 如果尝试使用BorderLayout,则防御按钮会占据整个屏幕。 我尝试了所有其他布局,但是没有运气。 我正在尝试打印JLabel,但根据互联网,您必须添加一个布局才能显示它。 无论如何,同时保持布局为空?

这是代码:

package jframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class klose extends JFrame implements ActionListener{

    private static final long serialVersionUID = 1L;



    public static void main(String[] args){


        JButton sword = new JButton("Sword");
        JButton axe = new JButton("Axe");

        JLabel bob = new JLabel("Choose a weapon...");

        JFrame frame = new JFrame("Combat Demo");
        frame.setSize(600, 600);
        frame.setVisible(true);

        JButton attack = new JButton("Attack!");
        JButton defend = new JButton("Defend!");
        frame.setLayout(null);
        attack.setSize(100, 50);
        defend.setSize(100, 50);
        attack.setLocation(400, 400);
        defend.setLocation(100, 400);
        frame.add(attack);
        frame.add(defend);

        attack.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {

                attack.setVisible(false);
                defend.setVisible(false);

                bob.setText("Choose a weapon...");

                bob.setLocation(0, 0);
                bob.setVisible(true);

                sword.setSize(100, 50);
                axe.setSize(100, 50);

                sword.setLocation(400, 400);
                axe.setLocation(100, 400);

                sword.setVisible(true);
                axe.setVisible(true);

                frame.add(sword);
                frame.add(axe);
                frame.add(bob);



            }
        });


    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }

    }

您可以通过调用此方法setLayout(null)来进行空布局,但是您将需要从DimensionInsets获取值以用作setBounds(x, y, width, height)方法的参数。

您的代码已在我端编译并执行。 在功能和可读性方面,我不得不对您的代码进行一些改进。 片段如下。

package app;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

//Please follow standard naming convention for java classes
public class GUIApp extends JFrame {

    private static final long serialVersionUID = 1L;

    private static JButton attack = new JButton("Attack!");
    private static JButton axe = new JButton("Axe");
    private static JLabel bob = new JLabel("Choose a weapon...");
    private static JButton defend = new JButton("Defend!");
    private static Insets insets = null;
    private static Dimension size = null;
    private static JButton sword = new JButton("Sword");

    public GUIApp() {
        //Notice the layout is null
        setLayout(null);

        //Insets need to be referenced
        insets = getContentPane().getInsets();

        //Layout stuffs
        setTitle("Combat Demo");
        setSize(600,600);
        attack.setSize(100, 50);
        axe.setSize(100, 50);
        defend.setSize(100, 50);
        sword.setSize(100, 50);

        //Toggle visibility
        attack.setVisible(true);
        defend.setVisible(true);
        sword.setVisible(false);
        axe.setVisible(false);
        bob.setVisible(false);

        //Add components
        add(attack);
        add(defend);
        add(sword);
        add(axe);
        add(bob);
    }

    public static void main(String[] args) {
        //Instantiate the object
        GUIApp app = new GUIApp();
        app.setVisible(true);
        app.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //You could obtain dimension and set bounds in the main method, or anywhere else.
        size = attack.getPreferredSize();
        attack.setBounds(400 + insets.left, 400 + insets.top, size.width, size.height);

        size = defend.getPreferredSize();
        defend.setBounds(100 + insets.left, 400 + insets.top, size.width, size.height);

        //Action listener for attack button
        attack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                attack.setVisible(false);
                defend.setVisible(false);
                bob.setVisible(true);
                sword.setVisible(true);
                axe.setVisible(true);

                //You could obtain dimension and set bounds in the actionPerformed method.
                size = bob.getPreferredSize();
                bob.setBounds(0 + insets.left, 0 + insets.top, size.width, size.height);

                size = sword.getPreferredSize();
                sword.setBounds(400 + insets.left, 400 + insets.top, size.width, size.height);

                size = axe.getPreferredSize();
                axe.setBounds(100 + insets.left, 400 + insets.top, size.width, size.height);
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM