JLabel没有显示在屏幕的中央

问题描述 投票:-1回答:1

我正在编写“问题类”,因此我更容易向grame / game添加问题,无论出于何种原因我的JFrame看起来如何:

https://imgur.com/JdrCSXm

package project.school.code;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Question extends JPanel{

    private String canswer;
    private String wanswer;
    private JButton correct;
    private JButton wrong;
    private JLabel question;
    private String qask;

    //Setter and getter for qask and canswer and wanswer

    public Dimension getPrefrredSize() {
        return new Dimension(1920,1920);
    } 

    public Question(String correctAnswer,String NotCorrect,String Ask,Font qfont) {
        this.qask = Ask;
        this.canswer = correctAnswer;
        this.wanswer = NotCorrect;
        this.correct = new JButton(canswer);
        this.wrong = new JButton(wanswer);
        this.question = new JLabel();
        this.question.setText(Ask);
        this.question.setFont(qfont);

        JPanel buttons = new JPanel();
        buttons.setPreferredSize(new Dimension(500,500));

        setLayout(new BorderLayout());
        this.correct.setPreferredSize(new Dimension(250,250));
        this.correct.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                Main.setCorrectans(Main.getCorrectans() + 1);

                JFrame GOODJOB = new JFrame("עבודה טובה!");
                Font tbf = new Font(null,Font.BOLD, 0);
                Font text = tbf.deriveFont(20f);
                JLabel answer = new JLabel("כל הכבד עניתה נכון!");
                answer.setFont(text);
                GOODJOB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GOODJOB.setLayout(new BorderLayout());
                GOODJOB.setPreferredSize(new Dimension(500,500));
                GOODJOB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GOODJOB.getContentPane().add(answer, BorderLayout.PAGE_START);
                GOODJOB.pack();
                GOODJOB.setVisible(true);
            }}
        );


        this.wrong.setPreferredSize(new Dimension(250,250));
        this.wrong.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFrame GOODJOB = new JFrame("עבודה גרועה!");
                Font tbf = new Font(null,Font.BOLD, 0);
                Font text = tbf.deriveFont(20f);
                JLabel answer = new JLabel("!עניתה לא נכון");
                answer.setFont(text);
                GOODJOB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GOODJOB.setLayout(new BorderLayout());
                GOODJOB.setPreferredSize(new Dimension(500,500));
                GOODJOB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GOODJOB.getContentPane().add(answer,BorderLayout.PAGE_START);
                GOODJOB.pack();
                GOODJOB.setVisible(true);
            }}
        );
        buttons.add(wrong,BorderLayout.EAST);
        buttons.add(correct,BorderLayout.WEST);
        add(question,BorderLayout.PAGE_START);
        add(buttons, BorderLayout.PAGE_END);
    }

}

我试着寻找这个奇怪的小故障JLabel的答案,但我似乎找不到答案,如果你知道我的代码有什么问题,请回答!

java swing jlabel layout-manager border-layout
1个回答
1
投票

由于answer标签从未添加到可见容器中(无论是创建时间)我都必须假设您指的是question标签。

add(question,BorderLayout.PAGE_START);

添加到PAGE_STARTBorderLayout约束的组件被赋予其优选高度,并被拉伸以适合整个可用宽度。

要使文本在JLabel中居中,请使用constructor指定其显示的文本的水平对齐方式。六个构造函数中的三个接受对齐。这种情况需要SwingConstants.CENTER

© www.soinside.com 2019 - 2024. All rights reserved.