gpt4 book ai didi

java - 多个 JSlider 相互 react 始终等于 100%

转载 作者:行者123 更新时间:2023-11-30 04:03:28 30 4
gpt4 key购买 nike

我正在尝试向 java swing 应用程序添加 3 个 JSlider,以便三个 slider 的总值(value)总和为 100。每个 slider 都是一个概率, slider A 是将值添加到队列的概率, slider B 是从队列中删除某个值的概率, slider C 是什么都不发生的概率。

示例: slider A 设置为 40, slider B 设置为 40, slider C 初始设置为 20。如果用户将 slider A 更改为 50,我希望 slider B 和 C 的值都减少 5。这样总数仍为 100。我知道当用户将值更改为 41 时可能会出现一些问题,因为我希望其他值保留为整数。

好的,在查看了有关 BoundedRangeModel 的一些信息后,我添加了一些代码。我已经设法让 slider 根据其他 slider 进行更改,但我仍然不确定如何实现初始值。此外, slider 似乎只能成对工作,而不是 3 个一组。

我会继续努力,如果我使用此方法想出一个令我满意的解决方案,我会将其发布在这里。

package stackoverflow;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Controller {

public static void main(String[] args)
{
//GUI up
Frame f = new Frame();
f.setSize(600, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

@SuppressWarnings("serial")
public static class Frame extends JFrame implements PropertyChangeListener, ChangeListener
{
//Sliders
private JSlider s1;
private JSlider s2;
private JSlider s3;

//Initial Values for fields (not sure how to implement)
private int inis1 = 45;
private int inis2 = 40;
private int inis3 = 15;

//Labels
private JLabel s1Label;
private JLabel s2Label;
private JLabel s3Label;

//Strings for labels
private static String s1String = "Probability of adding one to queue";
private static String s2String = "Probability of subtracting one from queue";
private static String s3String = "Probability of doing nothing";

public Frame()
{
//title of window
super("Sliders");

//Layout
//Label Panel, GridBag Setup
JPanel pane = new JPanel(new GridBagLayout());
this.add(pane);
GridBagConstraints c = new GridBagConstraints();

class LimitedBoundedRangeModel extends DefaultBoundedRangeModel {
BoundedRangeModel limit;

public LimitedBoundedRangeModel(BoundedRangeModel limit) {
this.limit = limit;
}

/**
* @inherited <p>
*/
@Override
public void setRangeProperties(int newValue, int newExtent, int newMin,
int newMax, boolean adjusting) {
if (limit != null) {
int combined = newValue + limit.getValue();
if (combined > newMax) {
newValue = newMax - limit.getValue();
}
}
boolean invoke =
(adjusting != getValueIsAdjusting()) && !adjusting;
super.setRangeProperties(newValue, newExtent, newMin, newMax, adjusting);
if (invoke) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
fireStateChanged();
}
});
}
super.setRangeProperties(newValue, newExtent, newMin, newMax, adjusting);
}
}

// use
LimitedBoundedRangeModel firstModel = new LimitedBoundedRangeModel(null);
LimitedBoundedRangeModel secondModel = new LimitedBoundedRangeModel(firstModel);
LimitedBoundedRangeModel thirdModel = new LimitedBoundedRangeModel(secondModel);

firstModel.limit = secondModel;
secondModel.limit = thirdModel;
thirdModel.limit = firstModel;

s1 = new JSlider(firstModel);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 5;
pane.add(s1, c);

s2 = new JSlider(secondModel);
c.gridx = 0;
c.gridy = 5;
c.gridwidth = 5;
pane.add(s2, c);

s3 = new JSlider(thirdModel);
c.gridx = 0;
c.gridy = 7;
c.gridwidth = 5;
pane.add(s3, c);

s1Label = new JLabel(s1String);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 5;
pane.add(s1Label, c);

s2Label = new JLabel(s2String);
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 5;
pane.add(s2Label, c);

s3Label = new JLabel(s3String);
c.gridx = 0;
c.gridy = 6;
c.gridwidth = 5;
pane.add(s3Label, c);

//enable 'tick' markers
s1.setMajorTickSpacing(25);
s1.setMinorTickSpacing(1);
s1.setPaintTicks(true);
s1.setPaintLabels(true);
s1.setSnapToTicks(true);
s2.setMajorTickSpacing(25);
s2.setMinorTickSpacing(1);
s2.setPaintTicks(true);
s2.setPaintLabels(true);
s2.setSnapToTicks(true);
s3.setMajorTickSpacing(25);
s3.setMinorTickSpacing(1);
s3.setPaintTicks(true);
s3.setPaintLabels(true);
s3.setSnapToTicks(true);

//Listening to sliders

s1.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent ce)
{
JSlider source = (JSlider)ce.getSource();
if (!source.getValueIsAdjusting())
{
int P = (int)source.getValue();
System.out.println("Probability of adding to queue is now: " + P);
/*
* TODO
* Would setting the values of the other sliders here,
* instead of using the BoundedRangeModel work?
* Tricky math would have to be used.
*/
// s2Slider.setValue();

}
}
});

s2.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent ce)
{
JSlider source = (JSlider)ce.getSource();
if (!source.getValueIsAdjusting())
{
int P = (int)source.getValue();
System.out.println("Probability of subtracting from queue is now: " + P);

}
}
});
s3.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent ce)
{
JSlider source = (JSlider)ce.getSource();
if (!source.getValueIsAdjusting())
{
int P = (int)source.getValue();
System.out.println("Probability of doing nothing to the queue is now: " + P);
}
}
});
}

@Override
public void propertyChange(PropertyChangeEvent evt) {
// TODO Auto-generated method stub

}

@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
}
}

}

最佳答案

一种方法,也许对某人有用:

package stackoverflow;

import java.awt.GridLayout;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ConnectedSliders
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}

private static void createAndShowGUI()
{
JSlider s0 = new JSlider(0, 100, 30);
JSlider s1 = new JSlider(0, 100, 40);
JSlider s2 = new JSlider(0, 100, 30);

SliderGroup sliderGroup = new SliderGroup();
sliderGroup.add(s0);
sliderGroup.add(s1);
sliderGroup.add(s2);

JPanel panel =new JPanel(new GridLayout(0,2));
panel.add(s0);
panel.add(createListeningLabel(s0));
panel.add(s1);
panel.add(createListeningLabel(s1));
panel.add(s2);
panel.add(createListeningLabel(s2));

panel.add(createListeningLabel(s0, s1, s2));

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private static JLabel createListeningLabel(final JSlider ... sliders)
{
final JLabel label = new JLabel("");
for (JSlider slider : sliders)
{
slider.addChangeListener(new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent e)
{
int sum = 0;
for (JSlider slider : sliders)
{
sum += slider.getValue();
}
label.setText("Sum: "+sum);
}
});
}
return label;
}

private static JLabel createListeningLabel(final JSlider slider)
{
final JLabel label = new JLabel("");
slider.addChangeListener(new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent e)
{
label.setText(String.valueOf(slider.getValue()));
}
});
return label;
}


}


class SliderGroup
{
private final Map<JSlider, Integer> values;
private final LinkedList<JSlider> candidates;

private final ChangeListener changeListener;
private boolean updating = false;

SliderGroup()
{
this.values = new HashMap<JSlider, Integer>();
this.candidates = new LinkedList<JSlider>();

changeListener = new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent e)
{
JSlider source = (JSlider)e.getSource();
update(source);
}
};
}

private void update(JSlider source)
{
if (updating)
{
return;
}
updating = true;

int delta = source.getValue() - values.get(source);
if (delta > 0)
{
distributeRemove(delta, source);
}
else
{
distributeAdd(delta, source);
}


for (JSlider slider : candidates)
{
values.put(slider, slider.getValue());
}

updating = false;
}


private void distributeRemove(int delta, JSlider source)
{
int counter = 0;
int remaining = delta;
while (remaining > 0)
{
JSlider slider = candidates.removeFirst();
counter++;

if (slider == source)
{
candidates.addLast(slider);
}
else
{
if (slider.getValue() > 0)
{
slider.setValue(slider.getValue()-1);
remaining--;
counter = 0;
}
candidates.addLast(slider);
if (remaining == 0)
{
break;
}
}
if (counter > candidates.size())
{
String message = "Can not distribute "+delta+" among "+candidates;
//System.out.println(message);
//return;
throw new IllegalArgumentException(message);
}
}
}

private void distributeAdd(int delta, JSlider source)
{
int counter = 0;
int remaining = -delta;
while (remaining > 0)
{
JSlider slider = candidates.removeLast();
counter++;

if (slider == source)
{
candidates.addFirst(slider);
}
else
{
if (slider.getValue() < slider.getMaximum())
{
slider.setValue(slider.getValue()+1);
remaining--;
counter = 0;
}
candidates.addFirst(slider);
if (remaining == 0)
{
break;
}
}
if (counter > candidates.size())
{
String message = "Can not distribute "+delta+" among "+candidates;
//System.out.println(message);
//return;
throw new IllegalArgumentException(message);
}
}
}


void add(JSlider slider)
{
candidates.add(slider);
values.put(slider, slider.getValue());
slider.addChangeListener(changeListener);
}

void remove(JSlider slider)
{
candidates.remove(slider);
values.remove(slider);
slider.removeChangeListener(changeListener);
}

}

核心是 SliderGroup 类(类似于 ButtonGroup):可以添加多个 JSlider。一个 slider 的更改将分布在其他 slider 中。正如评论中提到的,这种分布有点棘手:当一个人反复将 slider A 的值增加 1 时(例如,通过按右箭头光标键),则其他 slider 的值必须反复减少 - 但是它不应该总是选择相同的 slider 来接收此更改,否则只有一个 slider 会移动。我使用接收更改的“候选” slider 的 LinkedList 解决了这个问题。当其他 slider 的值必须减小时,更改会从左到右分布在这些候选 slider 之间。收到更改的 slider 被放置在列表的末尾,以便当它们的值必须再次减少时,收到先前减少的 slider 将是候选列表中的最后一个。 (当它们的值必须增加时,也会做同样的事情,反之亦然)。似乎工作良好,但没有经过彻底测试: slider 将被强制具有“无效”值的情况下的行为(例如,当 A 和 B 具有其 MIN 值,但 C 没有其 MAX 值时,并且增加)可能取决于应用案例。目前,在这种情况下会引发异常,但简单地“忽略”这种情况也可能是合适的......

关于java - 多个 JSlider 相互 react 始终等于 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21388255/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com