JButton改变了大小

时间:2014-02-04 23:31:22

标签: java swing user-interface jframe

我有JFrame,每当我使用JFrame切换JButton时,它就会正常启动,但每当我创建第一个JFrame的新实例时,JButton位置不正确且尺寸错误。

启动时的示例on startup

when instanced a second time

并在创建另一个时 代码:

public class Menu extends JFrame implements Runnable {
    private static final long serialVersionUID = 1L;

    public static int Number_of_Participants = 0;
    protected JPanel window = new JPanel();
    double p;
    private JButton Participants;
    private Rectangle rParticipants;

    protected int Button_width = 240;
    protected int Button_height = 48;
    boolean running = false;
    Thread thread;

    JFrame frame = new JFrame();

    public Menu() {
        window.setBackground(Color.BLUE);

        frame.setSize(new Dimension(800, 600));
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.getContentPane().add(window);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Image image = null;
        try {
            image = ImageIO.read(new File("res/BG.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        generateFiles();
        drawButtons();

        startMenu();
        frame.repaint();
    }

    public void drawButtons() {
        rParticipants = new Rectangle(520, 12, Button_width, Button_height);
        Participants = new JButton("A");
        Participants.setBounds(rParticipants);
        window.add(Participants);
        Participants.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Participant(Number_of_Participants);
            }

        });
    }
}

Participant.java 扩展 Menu.java

int Participant_ID;

public Participant(int Participant_ID) {
    super();
    this.Participant_ID = Participant_ID;
}

创建一个返回Menu.java的JButton

1 个答案:

答案 0 :(得分:0)

正如评论中所述,您的问题很可能与调用setVisible(true)有关。这应该始终是构造函数中的最后一次调用。特别是,只有在将所有组件添加到框架后才应调用它。

除此之外,从您发布的代码中,您似乎想要切换一系列帧,从" main"开始。菜单,然后为每个"参与者"进行一个框架。这个意图可能已经被认为是有问题的,因为为了创建一个JFrame而关闭和处理JFrame似乎并不是很优雅。最有可能的是,使用CardLayouthttp://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

可以实现更优雅的解决方案

然而,一些一般提示:

  • 在事件派发线程上创建GUI
  • 不要扩展JFrame。而是创建一个JFrame并根据需要填充它
  • 不要使用您的顶级课程
  • 实施Runnable
  • 服从standardJavaNamingConventions!
  • 请勿尝试使用setBounds进行手动布局

这段代码仍然不是很漂亮",但至少说明如何实现切换几帧的目标,同时考虑到这些要点

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MenuExample
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JPanel mainMenuPanel = new MainMenuPanel();
                createAndShowFrame(mainMenuPanel);
            }
        });
    }

    static void createAndShowFrame(JPanel panel)
    {
        JFrame frame = new JFrame();
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(800, 600));
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static JButton createNextParticipantButton(
        final JComponent container, final int nextID)
    {
        JButton nextParticipantButton = new JButton("New Participant");
        nextParticipantButton.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                Window window = 
                    SwingUtilities.getWindowAncestor(container);
                window.dispose();

                ParticipantPanel participantPanel = 
                    new ParticipantPanel(nextID);
                createAndShowFrame(participantPanel);
            }
        });
        return nextParticipantButton;
    }

}

class MainMenuPanel extends JPanel
{
    public MainMenuPanel() 
    {
        setBackground(Color.BLUE);
        add(MenuExample.createNextParticipantButton(this, 0));
    }
}

class ParticipantPanel extends JPanel
{
    private final int participantID;

    public ParticipantPanel(int participantID) 
    {
        this.participantID = participantID;

        add(new JLabel("Add the contents for participant "+participantID));
        add(MenuExample.createNextParticipantButton(this, participantID+1));
    }
}