我正在尝试制作一个具有由 JSlider 直接修改的 JLabel 的 Java 程序。我已经完成了所有的漂亮,但我无法让字体在 JLabel 上工作。下面是我的代码。
int PIX_INIT = 24;
int PIX_MIN = 0;
int PIX_MAX = 100;
JCheckBox text;
JCheckBox slide;
JTextField size;
JSlider pixel;
JLabel label1;
JLabel label2;
//Adds header
JPanel header = new JPanel(new BorderLayout());
header.setBackground(Color.CYAN);
header.setSize(400, 50);
header.setVisible(true);
//Adds content to header
JPanel content = new JPanel();
content.setBackground(Color.CYAN);
label1 = new JLabel("Marker Size: ");
content.add(label1);
size = new JTextField(5);
content.add(size);
label2 = new JLabel(" pixels");
content.add(label2);
header.add(content, BorderLayout.SOUTH);
this.add(header, BorderLayout.CENTER);
//Adds body
JPanel body = new JPanel(new BorderLayout());
body.setBackground(Color.WHITE);
body.setSize(400, 325);
body.setVisible(true);
this.add(body, BorderLayout.CENTER);
//End body
//Creates slider
//Adds slider
pixel = new JSlider(SwingConstants.HORIZONTAL, PIX_MIN, PIX_MAX, PIX_INIT);
pixel.setMajorTickSpacing( 10 );
pixel.setPaintTicks(true);
this.add(pixel, BorderLayout.SOUTH);
//End slider
private class SliderHandler implements ChangeListener {
public void stateChanged(ChangeEvent e) {
pixel = (JSlider)e.getSource();
int scale = pixel.getValue();
Font font = new Font("Serif", Font.PLAIN, scale);
label1.setFont(font);
System.out.println(scale);
}
}