-1

I was coding the "questions class" so it will be easier for me to add questions to the grame/game, for whatever reason this how my JFrame looks like:

https://i.stack.imgur.com/ZFDQ7.jpg

Question

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);
    }

}

I tried searching for answers for this weird glitchy JLabel but I seem to not find an answer, please answer if you know what's wrong with my code!

Community
  • 1
  • 1
Offlical
  • 25
  • 4
  • How about a [mcve]? – khelwood Dec 26 '17 at 10:30
  • Some other tips while preparing that MCVE suggested by @khelwood: 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 4) .. – Andrew Thompson Dec 26 '17 at 10:35
  • .. 4) `Font tbf = new Font(null,Font.BOLD, 0);` Seriously, what were you thinking by specifying `null`? – Andrew Thompson Dec 26 '17 at 10:36
  • *"this weird glitchy `JLabel`"* **Which** label, `question` or `answer`? And why is the `answer` label being created twice? – Andrew Thompson Dec 26 '17 at 10:41

1 Answers1

1

Since the answer label is never added to a visible container (either time it is created) I must presume you are referring to the question label.

add(question,BorderLayout.PAGE_START);

A component added to the PAGE_START constraint of a BorderLayout is given its preferred height, and stretched to fit the entire available width.

To center the text within the JLabel, use a constructor that specifies the horizontal alignment of the text it displays. Three of the six constructors accept an alignment. This case requires SwingConstants.CENTER.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433