简体   繁体   English

JLabel 和 JButton 文本和图标大小在导出后发生变化

[英]JLabel and JButton text and icon sizes changes after export

I am just a beginner and am learning about JFrame , JLabel and JButton .我只是一个初学者,正在学习JFrameJLabelJButton 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.我写了一个简单的代码,按下按钮后你应该看到某个 label。当我在那里运行代码时,IntelliJ 或 Eclipse 一切正常,但在将其导出为 JAR 并打开它后,文本和图像大小发生了变化.

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

If all you want to do is change the size and style of the JLabel font, then replace this line of your code如果您只想更改JLabel字体的大小和样式,请替换这行代码

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

with this有了这个

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

Similarly for the JButton .对于JButton也是如此。

button.setFont(button.getFont().deriveFont(Font.PLAIN, 20.0f));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM