【发布时间】:2015-01-27 01:36:28
【问题描述】:
在我的新 Java 项目中,我有一个 JFrame,其中有一个带有 BorderLayout 的 JLabel 设置为 North,下面是一个图像。图像非常适合 JFrame,但 JLabel 会切断它的顶部。如何调整此 JLabel 的大小?我试过setPreferredSize,但没有用。帮助将不胜感激。
代码:
package counter.main;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class FoodCounter {
public static JLabel greet4 = new JLabel("",SwingConstants.CENTER);
public static JLabel message4 = new JLabel();
public static JLabel lclicks4 = new JLabel();
public static JButton buttonClick4 = new JButton("+ Food");
public static int clicks4 = 0;
public static URL food = Main.class.getResource("/counter/main/FoodEating.wav");
public static JButton back = new JButton("Back");
public static JLabel bread;
static JFrame frame = new JFrame("Food Counter"); {
createView();
frame.setSize(500, 100);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.revalidate();
frame.repaint();
}
private void createView() {
final JPanel panelc = new JPanel();
frame.getContentPane().add(panelc);
panelc.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 11));
greet4.setFont(new Font( "Dialog", Font.PLAIN, 18));
greet4.setPreferredSize(new Dimension(40, 20));
panelc.add(message4);
frame.add(back, BorderLayout.WEST);
frame.add(greet4, BorderLayout.NORTH);
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
SelectionFrame.frame1.setVisible(true);
}
});
panelc.add(buttonClick4);
panelc.add(lclicks4);
updateCounter();
bread = new JLabel("");
bread.setPreferredSize(new Dimension(64, 64));
Image img = new ImageIcon(this.getClass().getResource("/counter/main/SlicedBread64.png")).getImage();
bread.setIcon(new ImageIcon(img));
frame.getContentPane().add(bread, BorderLayout.EAST);
buttonClick4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clicks4++;
updateCounter();
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(food);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (IOException | UnsupportedAudioFileException | LineUnavailableException x) {
x.printStackTrace();
}
}
});
};
private void updateCounter() {
lclicks4.setText(clicks4 + "/100 Food ");
if (clicks4 < 1) {
message4.setText("Click to Begin! -->");
}
if (clicks4 >= 1 && clicks4 < 10) {
message4.setText("Keep Going!");
}
if (clicks4 >= 10 && clicks4 < 50) {
message4.setText("Keep 'em Comin'!");
}
if (clicks4 >= 50 && clicks4 < 70) {
message4.setText("Don't Stop!");
}
if (clicks4 >= 70 && clicks4 < 80) {
message4.setText("Almost!");
}
if (clicks4 >= 90 && clicks4 < 100) {
message4.setText("Finish Strong!");
}
if (clicks4 >= 100) {
try {
Thread.sleep(3000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.exit(0);
}
}
}
【问题讨论】:
-
当心
setResizable,这会改变框架边框的大小,从而改变内容可用空间的大小。不要使用setSize,而是使用pack,但在将框架设置为不可调整大小后调用它
标签: java swing resize icons jlabel