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