JFrame 的边框布局
Border Layout with JFrame
我正在尝试使用带有一些 JLabel 的滑块来制作一个简单的程序,该程序从滑块获取半径并输出以标记周长和直径。我的问题是我似乎无法弄清楚如何使用边框布局来布置标签。滑块将显示得很好,但我希望能够在一排标签的左侧将标签排成一行。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class Slider extends JFrame {
private final JSlider radiusJSlider;
private final JLabel radius;
private final JLabel diameter;
public Slider() {
super("Slider Demo");
BorderLayout layout = new BorderLayout();
radius = new JLabel("radius");
diameter = new JLabel("diameter");
radiusJSlider
= new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
radiusJSlider.setMajorTickSpacing(10); // create tick every 10
radiusJSlider.setPaintTicks(true);
radiusJSlider.setPaintLabels(true);// paint ticks on slider
radiusJSlider.setLabelTable(radiusJSlider.createStandardLabels(10));
//JPanel panel = new JPanel(new BorderLayout());
//panel.add(diameter, BorderLayout.CENTER);
JPanel labels = new JPanel(new FlowLayout(FlowLayout.LEFT) );
labels.add(radius);
labels.add(diameter);
add(radiusJSlider, BorderLayout.SOUTH);
//add(radius, BorderLayout.CENTER);
//add(diameter, BorderLayout.CENTER);
layout.addLayoutComponent(labels, BorderLayout.CENTER);
radiusJSlider.addChangeListener(
new ChangeListener() // anonymous inner class
{
// handle change in slider value
@Override
public void stateChanged(ChangeEvent e) {
int results;
int diameterResults;
results = radiusJSlider.getValue();
diameterResults = results * 2;
radius.setText(String.format("radius: %2d", radiusJSlider.getValue()));
diameter.setText(String.format("diameter: %2d", diameterResults));
}
}
);
}
}
我不确定你为什么要这样做layout.addLayoutComponent(labels, BorderLayout.CENTER);
,这不是你与布局管理器交互的方式
因为JFrame
默认使用BorderLayout
,所以不需要创建一个新的,相反,只需对滑块做同样的事情,将面板添加到容器
add(labels); // BorderLayout.CENTER is the default position
我正在尝试使用带有一些 JLabel 的滑块来制作一个简单的程序,该程序从滑块获取半径并输出以标记周长和直径。我的问题是我似乎无法弄清楚如何使用边框布局来布置标签。滑块将显示得很好,但我希望能够在一排标签的左侧将标签排成一行。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class Slider extends JFrame {
private final JSlider radiusJSlider;
private final JLabel radius;
private final JLabel diameter;
public Slider() {
super("Slider Demo");
BorderLayout layout = new BorderLayout();
radius = new JLabel("radius");
diameter = new JLabel("diameter");
radiusJSlider
= new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
radiusJSlider.setMajorTickSpacing(10); // create tick every 10
radiusJSlider.setPaintTicks(true);
radiusJSlider.setPaintLabels(true);// paint ticks on slider
radiusJSlider.setLabelTable(radiusJSlider.createStandardLabels(10));
//JPanel panel = new JPanel(new BorderLayout());
//panel.add(diameter, BorderLayout.CENTER);
JPanel labels = new JPanel(new FlowLayout(FlowLayout.LEFT) );
labels.add(radius);
labels.add(diameter);
add(radiusJSlider, BorderLayout.SOUTH);
//add(radius, BorderLayout.CENTER);
//add(diameter, BorderLayout.CENTER);
layout.addLayoutComponent(labels, BorderLayout.CENTER);
radiusJSlider.addChangeListener(
new ChangeListener() // anonymous inner class
{
// handle change in slider value
@Override
public void stateChanged(ChangeEvent e) {
int results;
int diameterResults;
results = radiusJSlider.getValue();
diameterResults = results * 2;
radius.setText(String.format("radius: %2d", radiusJSlider.getValue()));
diameter.setText(String.format("diameter: %2d", diameterResults));
}
}
);
}
}
我不确定你为什么要这样做layout.addLayoutComponent(labels, BorderLayout.CENTER);
,这不是你与布局管理器交互的方式
因为JFrame
默认使用BorderLayout
,所以不需要创建一个新的,相反,只需对滑块做同样的事情,将面板添加到容器
add(labels); // BorderLayout.CENTER is the default position