JScrollPane覆盖setHvalue

huangapple go评论72阅读模式
英文:

JScrollPane override setHvalue

问题

I would like to modify my JScrollPane so that when the horizontal position is changed, the position hvalue is sent to another class. I don't want the other class to have to retrieve this value every time as this will happen repeatedly during a task which must be finished as quickly as possible.

我想修改我的JScrollPane,以便在水平位置更改时,将位置hvalue发送到另一个类。我不希望其他类每次都必须检索此值,因为这将在必须尽快完成的任务中重复发生。

I haven't been able to find any documentation on the exact syntax used by default but I'm guessing what I want will look something like this:

我还没有找到关于默认使用的确切语法的任何文档,但我猜想我想要的东西会类似于这样:

import java.lang.Math.*;
import javax.swing.*;

public class scroll {
public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(3);
    panel p = new panel();
    
    JScrollPane sp = new JScrollPane(p,21,31) {
        @Override
        public final void setHvalue(double value) {
            hvalue = value;
            p.sendH((int) Math.round(value));
        }
    };
    
    f.add(sp);
    f.setSize(333,333);
    f.setVisible(true);
}
}

which will include a panel object governed by the class:

这将包括由类管理的面板对象:

import javax.swing.*;

public class panel extends JPanel {

private static int H;
public panel() {}

public static void sendH(int x) {
    H=x;
    System.out.println(x);
}
}

Attempting to compile this code yields the error messages:

尝试编译此代码会产生以下错误消息:

  • scroll.java.11: error: method does not override or implement a method from supertype

  • @Override

  • scroll.java.13: error: cannot find symbol

  • hvalue = value;

  • symbol: variable hvalue

which, as I understand it, is basically their way of saying I can't modify setHvalue(). Is there some way around this or an alternative way of sending hvalue when it's changed instead of retrieving it every time it's needed?

根据我的理解,这基本上是说我不能修改setHvalue()。是否有某种解决方法或在hvalue更改时发送它的替代方法,而不是每次都检索它?

英文:

I would like to modify my JScrollPane so that when the horizontal position is changed, the position hvalue is sent to another class. I don't want the other class to have to retrieve this value every time as this will happen repeatedly during a task which must be finished as quickly as possible.
I haven't been able to find any documentation on the exact syntax used by default but I'm guessing what I want will look something like this:

import java.lang.Math.*;
import javax.swing.*;

public class scroll {
public static void main(String[] args) {
	JFrame f = new JFrame();
	f.setDefaultCloseOperation(3);
	panel p = new panel();
	
	JScrollPane sp = new JScrollPane(p,21,31) {
		@Override
		public final void setHvalue(double value) {
			hvalue = value;
			p.sendH((int) Math.round(value));
		}
	};
	
	f.add(sp);
	f.setSize(333,333);
	f.setVisible(true);
}
}

which will include a panel object governed by the class:

import javax.swing.*;

public class panel extends JPanel {

private static int H;
public panel() {}

public static void sendH(int x) {
	H=x;
	System.out.println(x);
}
}

Attempting to compile this code yields the error messages:

  • scroll.java.11: error: method does not override or implement a method from supertype

  • @Override

  • scroll.java.13: error: cannot find symbol

  • hvalue = value;

  • symbol: variable hvalue

which, as I understand it, is basically their way of saying I can't modify setHvalue(). Is there some way around this or an alternative way of sending hvalue when it's changed instead of retrieving it every time it's needed?

答案1

得分: 0

当水平位置改变时,位置hvalue会被发送到另一个类。这是通过向水平滚动条添加一个AdjustmentListener来完成的。然而,如果您不需要在每次位置变化时都使用这个信息,这将是不必要的额外开销。我不建议使用这种方法。

我不希望其他类每次都要检索这个值,因为在必须尽快完成的任务中,这将会重复发生。调用滚动条的getValue()方法不会为您的任务增加可测量的开销。您的任务只需要一个对滚动窗格的引用,这样您就可以访问滚动条并获取当前值。

英文:

> when the horizontal position is changed, the position hvalue is sent to another class.

This is done by adding an AdjustmentListener to the horizontal scrollbar.

However, this is extra overhead that is NOT required if you do not use the information every time it changes.

I do not recommend using this approach.

> I don't want the other class to have to retrieve this value every time as this will happen repeatedly during a task which must be finished as quickly as possible.

Invoking the getValue() method of the scrollbar will NOT add any measurable overhead to your task.

Your task just needs a reference to the scroll pane so you can access the scrollbar and get the current value.

答案2

得分: 0

I apologize, but it seems you have requested that only code portions not be translated. Since your provided text consists mainly of code, I will refrain from translating it. If you have any specific non-code text that you would like me to translate, please provide it separately, and I will be happy to assist you.

英文:

Actually I figured out the answer, sorry about that. It turns out that the method getHvalue is only for awt ScrollPane and is not inherited by swing JScrollPane. I will post a working version here, in case someone else is interested some day (although I'm guessing that camickr's approach is better).
The JScrollPane class is:

import java.awt.*;
import javax.swing.*;
import javax.accessibility.*;

public class scroll {
public static void main(String[] args) {
	JFrame f = new JFrame();
	f.setDefaultCloseOperation(3);
	panel p = new panel() {
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(getWidth(),getHeight());
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(getWidth(),getHeight());
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(getWidth(),getHeight());
		}
	};
	
	JScrollPane sp = new JScrollPane(p,22,32);
	sp.setHorizontalScrollBar( new JScrollBar(JScrollBar.HORIZONTAL){
		@Override
		public void setValue(int value) {
			BoundedRangeModel m = getModel();
			int oldValue = m.getValue();
			m.setValue(value);

			if (accessibleContext != null) {
				accessibleContext.firePropertyChange(
				AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
				Integer.valueOf(oldValue),
				Integer.valueOf(m.getValue()));
			}
			
			p.sendH(value);
		}
	});
	
	f.add(sp);
	f.setSize(333,333);
	p.setSize(400,400);
	f.setVisible(true);
}
}

and the panel class is:

import javax.swing.*;

public class panel extends JPanel {

private static int H;
public panel() {}

public static void sendH(int x) {
	H=x;
	System.out.println(x);
}
}

huangapple
  • 本文由 发表于 2020年8月13日 22:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定