0

I am just a beginner and am learning about JFrame, JLabel and JButton. I wrote a simple code where after pressing the button you are supposed to see a certain label. Everything works fine inside IntelliJ or Eclipse when I run the code in there, but after exporting it as JAR and opening it, text and image sizes are changed.

public class MyFrame extends JFrame implements ActionListener {
    File file;
    AudioInputStream audioStream;
    Clip clip;
    JButton button;
    JLabel label;

    MyFrame() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
        file = new File("C:/Users/uchak/IdeaProjects/New folder/fail.wav");
        audioStream = AudioSystem.getAudioInputStream(file);
        clip = AudioSystem.getClip();
        clip.open(audioStream);

        ImageIcon icon = new ImageIcon("gilaki.png");
        ImageIcon icon2 = new ImageIcon("nini.png");

        label = new JLabel();
        label.setText("nini debili xar :)");
        label.setBackground(new Color(150, 10, 10));
        label.setForeground(new Color(245, 215, 66));
        label.setBounds(450, 75, 600, 600);
        label.setVisible(false);
        label.setIcon(icon2);
        label.setHorizontalTextPosition(JLabel.CENTER);
        label.setVerticalTextPosition(JLabel.TOP);
        label.setFont(new Font("", Font.PLAIN, 50));

        button = new JButton();

        button.setText("daachire da moxdeba saocreba");
        button.setBounds(500, 150, 400, 300);
        button.setVisible(true);
        button.addActionListener(this);
        button.setFocusable(false);
        button.setIcon(icon);
        button.setHorizontalTextPosition(JButton.CENTER);
        button.setVerticalTextPosition(JButton.TOP);
        button.setBackground(Color.BLACK);
        button.setForeground(Color.red);
        button.setFont(new Font("", Font.PLAIN, 20));

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(1920, 1080);
        this.setLayout(null);
        this.add(button);
        this.add(label);
        this.getContentPane().setBackground(new Color(100, 20, 200));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            label.setVisible(true);
            button.setVisible(false);
            this.getContentPane().setBackground(Color.BLACK);
            clip.start();
        }
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
Ucha
  • 11
  • 1
  • 1
    *text and image sizes are changed.* - how? Are they larger? Maybe when you run the code as a jar you are getting scaling based on the properties of your desktop. Maybe you can prevent scaling using one of the suggestions from here: https://stackoverflow.com/a/64600797/131872 – camickr Feb 24 '22 at 16:04
  • In my opinion, the [sound](https://docs.oracle.com/javase/tutorial/sound/index.html) code has nothing to do with your problem since you claim that the problem is with the `JLabel` font. Hence in order for the code in your question to be a proper [mcve], I suggest that you remove the _sound_ code. – Abra Feb 24 '22 at 16:06
  • 1
    `this.setLayout(null);` ← That’s most likely your problem. [Use layouts](https://docs.oracle.com/javase/tutorial/uiswing/layout/) so your window will look correct no matter what fonts or resolution are imposed by the system. – VGR Feb 25 '22 at 01:40

1 Answers1

0

If all you want to do is change the size and style of the JLabel font, then replace this line of your code

label.setFont(new Font("",Font.PLAIN,50));

with this

label.setFont(label.getFont().deriveFont(Font.PLAIN, 50.0f));

Similarly for the JButton.

button.setFont(button.getFont().deriveFont(Font.PLAIN, 20.0f));
Abra
  • 19,142
  • 7
  • 29
  • 41
  • No, I do not want to change the size of the font. I want it to stay same after exporting. After exporting as .jar and running it, size of the button and the label are changed by themselves. Only after exporting. When I run the code inside the intellije or eclipse, everything is fine. – Ucha Feb 24 '22 at 15:49
  • @Ucha you can't lose anything by giving it a try. You only need to change two lines of your code and that's not a lot of work. If it doesn't work it will be easy to change the code back to the original. Not setting an actual font name in the `Font` constructor is probably causing the problem. I assume you don't provide a font name since you only want to change the font size and style, right? I mean that you want to change the `JLabel` font and not use the default font, right? That's what this line of your code does: `label.setFont(new Font("",Font.PLAIN,50));` – Abra Feb 24 '22 at 15:54
  • I tried it and unfortunately it did not work. – Ucha Feb 24 '22 at 17:07