java - 如何在 Gui 中添加图形作为按钮面板下方的面板?

标签 java swing user-interface

我一直在尝试使用一些 JLabel 和按钮来实现 JPanel 的简单布局,并在其下方显示一个图形。

我使用扩展了 JComponent 的类(其中包含 paintComponent)创建了图形,然后实例化该类并将其添加到第一个面板之后的框架中。仅显示图形。

我尝试了图形间距,但没有改变任何内容。我知道按钮确实有效,因为当我注释掉添加 Graphics 对象组件时,它只显示按钮。我该怎么办?

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

import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JColorChooser;

import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Frame
{
    public static void main(String[] args)
    {
        JFrame yipee = new JFrame();

        final JLabel title = new JLabel("Create your Character");
        title.setFont(new Font("Serif", Font.PLAIN, 40));
        final JLabel chooseCol = new JLabel("Choose Color");
        final JLabel chooseGen = new JLabel("Choose Gender");
        final JLabel ranWeap = new JLabel("Random Weapon");

        color = (Color.WHITE);

        isMale = true;

        ranWeapTriggered = false;

        JButton ranWeapBut = new JButton("Fancy");

        JRadioButton male = new JRadioButton("Male");
        male.setSelected(true);

        JRadioButton female = new JRadioButton("Female");

        ButtonGroup genBut = new ButtonGroup();
        genBut.add(male);
        genBut.add(female);

        JButton pickCol = new JButton("Click Me");

        KnightComponent kc = new KnightComponent();
        //call method if random weapon pressed
        if(ranWeapTriggered)
        {
            //character.trigger()
            ranWeapTriggered = false;
        }

        JPanel options = new JPanel();

        options.add(title);
        options.add(chooseCol);
        options.add(new JSeparator(SwingConstants.VERTICAL));
        options.add(chooseGen);
        options.add(new JSeparator(SwingConstants.VERTICAL));
        options.add(ranWeap);
        options.add(pickCol);
        options.add(male);
        options.add(female);
        options.add(ranWeapBut);
        //options.add(dis);



        yipee.add(options);
        yipee.add(kc);


        class PickColorListener implements ActionListener 
        {
            public void actionPerformed(ActionEvent e)
            {
                color = JColorChooser.showDialog(null, "Pick Color", color);
                if(color == null)
                    color = color.WHITE;
            }
        }

        class ChooseGenderListener implements ActionListener
        {
            public ChooseGenderListener(String gIn)
            {
                gender = gIn;
            }
            public void actionPerformed(ActionEvent e)
            {
                if(gender.equals("male"))

                    isMale = true;
                else
                    isMale = false;
            }
            private String gender;
        }

        class RandomWeaponListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                ranWeapTriggered = true;
            }
        }



        ActionListener l1 = new PickColorListener();
        pickCol.addActionListener(l1);

        ActionListener l2 = new ChooseGenderListener("male");
        male.addActionListener(l2);

        ActionListener l3 = new ChooseGenderListener("female");
        female.addActionListener(l3);

        yipee.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        yipee.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        yipee.setVisible(true);
        yipee.setResizable(false);
    }    

    private static final int FRAME_WIDTH = 400;
    private static final int FRAME_HEIGHT = 600;
    private static Color color;
    private static boolean isMale;
    private static boolean ranWeapTriggered;
}

.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;

import javax.swing.JComponent;

public class KnightComponent extends JComponent
{
    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        Knight k = new Knight(0,200);
        k.draw(g2);
    }
}

最佳答案

I'm not using a BorderLayout

实际上,JFrame默认使用BorderLayout,参见Laying Out Components Within a Container了解更多详情。

直接的解决方案可能是做类似的事情......

yipee.add(options, BorderLayout.NORTH);
yipee.add(kc);

但是由于 KnightComponent 不提供任何大小调整提示供布局管理器使用,因此它的大小将自动调整为 0x0。您应该重写它的 getPreferredSize 方法并返回适当的默认大小

您还破坏了油漆链,这将导致一些严重的奇怪,请参阅Painting in AWT and SwingPerforming Custom Painting

关于java - 如何在 Gui 中添加图形作为按钮面板下方的面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27051937/

相关文章:

java - 在真实设备上调试 Android App 与 Servlet 的通信

java - 如何实现静态的项目范围偏好系统?

java - 具有自定义文档和编辑器的 JTextPane 无法正确显示图标

java - 单击按钮时 JFrame 未打开

java - 在 Ubuntu 中将 Matlab java 从 7 降级到 6 会返回错误

perl - 如何在 Perl 中制作富 Web 应用程序?

java - 用Java复制文件的标准简洁方法?

java - 等宽 Swing 文字

如果所有者窗口不可见,则 JavaFX 弹出窗口不会显示

java - 如何将有效负载发送到 selenium 中的文本区域