我正在使用几个滑块和按钮制作视图,但即使我手动展开窗口,它也会被窗口切断!我正在使用盒子布局,而我的朋友正在做相同的任务,但不知何故,他的程序允许他适合他想要的多个滑块,并且滑块只是越来越近,因为他使窗口的高度变小。然而,即使我展开窗口,头脑也会切断最后三个滑块。这是代码:
public PlayerChefView(int belt_size) {
this.belt_size = belt_size;
listeners = new ArrayList<ChefViewListener>();
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
//setLayout(new GridLayout(10,4));
JLabel colorSelectionLabel = new JLabel("Plate color:", JLabel.CENTER);
add(colorSelectionLabel);
String[] colorSelection = {"Blue", "Red", "Green", "Gold"};
colorBox = new JComboBox(colorSelection);
colorBox.addActionListener(this);
add(colorBox);
JLabel goldPriceLabel = new JLabel("Gold plate price:", JLabel.CENTER);
add(goldPriceLabel);
priceSlider = new JSlider(500, 1000, 750);
Hashtable labelTable = new Hashtable();
labelTable.put(new Integer(500), new JLabel("$5.00"));
labelTable.put(new Integer (600), new JLabel("$6.00"));
labelTable.put(new Integer(700), new JLabel("$7.00"));
labelTable.put(new Integer(800), new JLabel("$8.00"));
labelTable.put(new Integer(900) , new JLabel("$9.00"));
labelTable.put(new Integer(1000), new JLabel("$10.00"));
//slider.setSnapToTicks(true);
priceSlider.setLabelTable(labelTable);
priceSlider.setMajorTickSpacing(100);
priceSlider.setPaintTicks(true);
priceSlider.setPaintLabels(true);
add(priceSlider);
JLabel positionLabel = new JLabel("Plate position:", JLabel.CENTER);
add(positionLabel);
Integer[] positions = new Integer[belt_size];
for (Integer i = 1; i<=belt_size; i++){
positions[i - 1] = i;
}
positionBox = new JComboBox(positions);
positionBox.addActionListener(this);
add(positionBox);
JLabel sashimiOrNigiriLabel = new JLabel("Would you like to make "
+ "Sashimi or Nigiri?", JLabel.CENTER);
add(sashimiOrNigiriLabel);
String[] sashimiOrNigiri = {"Sashimi", "Nigiri" };
sashimiOrNigiriBox = new JComboBox(sashimiOrNigiri);
sashimiOrNigiriBox.addActionListener(this);
add(sashimiOrNigiriBox);
JLabel seafoodTypeLabel = new JLabel("For sashimi or Nigiri ONLY, what seafood?", JLabel.CENTER);
add(seafoodTypeLabel);
String[] seafood = {"Tuna", "Salmon", "Eel",
"Crab", "Shrimp"};
seafoodTypeBox = new JComboBox(seafood);
seafoodTypeBox.addActionListener(this);
add(seafoodTypeBox);
JButton makeSashimiOrNigiri = new JButton("Create sashimi/nigiri!");
//System.out.println(colorBox.getSelectedItem().toString());
makeSashimiOrNigiri.setActionCommand("sashimi/nigiri");
makeSashimiOrNigiri.addActionListener(this);
add(makeSashimiOrNigiri);
JLabel blankLabel = new JLabel("", JLabel.RIGHT);
add(blankLabel);
JLabel avacadoLabel = new JLabel("The following sliders are for rolls only: select avacado amt. (oz.)", JLabel.LEFT);
add(avacadoLabel);
avocadoSlider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
amountLabels = new Hashtable();
amountLabels.put(new Integer(0), new JLabel("0.0"));
amountLabels.put(new Integer (25), new JLabel(".25"));
amountLabels.put(new Integer(50), new JLabel(".50"));
amountLabels.put(new Integer(75), new JLabel(".75"));
amountLabels.put(new Integer(100) , new JLabel("1.00"));
amountLabels.put(new Integer(125), new JLabel("1.25"));
amountLabels.put(new Integer(150), new JLabel("1.50"));
avocadoSlider.setLabelTable(amountLabels);
avocadoSlider.setMajorTickSpacing(25);
avocadoSlider.setPaintTicks(true);
avocadoSlider.setPaintLabels(true);
add(avocadoSlider);
JLabel crabLabel = new JLabel("select crab amt. (oz.)", JLabel.LEFT);
add(crabLabel);
crabSlider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
crabSlider.setLabelTable(amountLabels);
crabSlider.setMajorTickSpacing(25);
crabSlider.setPaintTicks(true);
crabSlider.setPaintLabels(true);
add(crabSlider);
JLabel eelLabel = new JLabel("select eel amt. (oz.)", JLabel.LEFT);
add(eelLabel);
eelSlider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
eelSlider.setLabelTable(amountLabels);
eelSlider.setMajorTickSpacing(25);
eelSlider.setPaintTicks(true);
eelSlider.setPaintLabels(true);
add(eelSlider);
JLabel riceLabel = new JLabel("select rice amt. (oz.)", JLabel.LEFT);
add(riceLabel);
riceSlider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
riceSlider.setLabelTable(amountLabels);
riceSlider.setMajorTickSpacing(25);
riceSlider.setPaintTicks(true);
riceSlider.setPaintLabels(true);
add(riceSlider);
JLabel salmonLabel = new JLabel("select salmon amt. (oz.)", JLabel.LEFT);
add(salmonLabel);
salmonSlider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
salmonSlider.setLabelTable(amountLabels);
salmonSlider.setMajorTickSpacing(25);
salmonSlider.setPaintTicks(true);
salmonSlider.setPaintLabels(true);
add(salmonSlider);
JLabel seaweedLabel = new JLabel("select seaweed amt. (oz.)", JLabel.LEFT);
add(seaweedLabel);
seaweedSlider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
seaweedSlider.setLabelTable(amountLabels);
seaweedSlider.setMajorTickSpacing(25);
seaweedSlider.setPaintTicks(true);
seaweedSlider.setPaintLabels(true);
add(seaweedSlider);
JLabel shrimpLabel = new JLabel("select shrimp amt. (oz.)", JLabel.LEFT);
add(shrimpLabel);
shrimpSlider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
shrimpSlider.setLabelTable(amountLabels);
shrimpSlider.setMajorTickSpacing(25);
shrimpSlider.setPaintTicks(true);
shrimpSlider.setPaintLabels(true);
add(shrimpSlider);
JLabel tunaLabel = new JLabel("select tuna amt. (oz.)", JLabel.LEFT);
add(tunaLabel);
tunaSlider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
tunaSlider.setLabelTable(amountLabels);
tunaSlider.setMajorTickSpacing(25);
tunaSlider.setPaintTicks(true);
tunaSlider.setPaintLabels(true);
add(tunaSlider);
看到这个截图,看看我的意思: Note that the slider labels are getting cut off