JLabel和JButton的文本和图标大小在导出后发生更改



我只是一个初学者,正在学习JFrameJLabelJButton。我写了一个简单的代码,按下按钮后,你应该看到某个标签。当我在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();
}
}
}

如果您只想更改JLabel字体的大小和样式,那么请替换代码的这一行

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

用这个

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

对于CCD_ 5。

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

最新更新