我想将鼠标悬停在我的GUI(地图)上的一些JButton上,并显示该位置的名称,例如曼彻斯特和伦敦.我有一个按钮的代码,但它不适用于多个按钮,并打印out所有按钮位置的最后一条消息(因为我有10个按钮).
如果button1为true,则通过我的paintComponent()方法在指定区域中的GUI上绘制文本.
我该如何解决这个问题?
button1.addMouseMotionListener(this);
button2.addMouseMotionListener(this);
Run Code Online (Sandbox Code Playgroud)
public void mouseMoved(MouseEvent arg0)
{
if(button1.contains(arg0.getPoint()))
{
button1 = true;
out = "test 1";
repaint();
}
if(!button1.contains(arg0.getPoint()))
{
b1 = false;
out = " ";
repaint();
}//same for all 10 buttons but change variables
}
Run Code Online (Sandbox Code Playgroud) 到目前为止我已经这样做了,但是对b部分有困难.这是一份模拟试卷,对b部分的其余部分不确定.
Q)总结由下式给出的序列的元素s.valAtIndex(i).s是Seq类型.Seq是一个有方法的接口valAtIndex (integer parameter and double result).
(a)编写接口Seq.
(b)写一个几何类,实现Seq.因此,每个实例s表示如下几何系列
s.valAtIndex(0), s.valAtIndex(0)......使得第i个元素s.valAtIndex(i)等于基数b的第i个幂,即b ^ i.(回想一下b ^ 0 = 1)
(一个)
public interface Seq{
public double valAtIndex(int i);
}
Run Code Online (Sandbox Code Playgroud)
(b)中
public Geometric implements Seq{
Seq s;
private double b;
public Geometric(double a){
s = new Geometric(a);
this.b=a;
}
@Override
public double valAtIndex(int i){
return 0;//not sure how to do this method
}
Run Code Online (Sandbox Code Playgroud) 我对过去的试卷中的问题感到困难.我正在尝试将from数字乘以n数字.换句话说:from*(from + 1)(from + 2)...*n.
我需要使用while循环来解决这个问题.到目前为止我已经这样做了,不知道该怎么做.我知道代码错了,但已经卡住了一段时间.
public class Fact {
public int last;
private int factPartND(final int from, final int n) {
int fromNum = from;
int toNum = n;
int result = 1;
int c = 1;
while (fromNum <= toNum) { // e.g.5*6*7*8*9*10*11
result = (fromNum) * (fromNum + c); // calculate 5*6
final int temp = result; // store 5*6
int result1 = temp * (fromNum + c); // store 5*6*7*....
c++; // …Run Code Online (Sandbox Code Playgroud) 这是过去的试卷中的一个问题.代码在问题中给出,我需要获得值和断点的次数.我试过在eclipse中运行代码但无济于事.(如果代码执行,我可以在调试模式下找到值)
此问题还指出:该方法fact在类的实例上调用,其中n值为6.不确定我做错了什么,因为代码与问题中给出的完全相同.
public class FactLoop {
private int n;// assumed to be greater than or equal to 0
/**
* Calculate factorial of n
*
* @return n!
*/
public int fact() {
int i = 0;
int f = 1;
/**
* loop invariant 0<=i<=n and f=i!
*/
while (i < n) {// loop test (breakpoint on this line)
i = i++;
f = f * i;
}
return f;
}
// this main method …Run Code Online (Sandbox Code Playgroud) 这是过去一篇论文中的一个问题.我被要求创建一个静态方法arrayMin来查找数组中的最小值arr.
我必须使用while循环,并且在每次迭代时,变量min将返回第一个i元素中的最小数字.
有没有办法在不调用另一个方法/ for循环和严格使用while循环的情况下执行此操作,因为问题仅值4%(包括编写循环不变量和javadoc).不确定我是否过度复杂化了问题.
public class Revision {
public static int arr[] = new int[] { 5, 8, 4, 3, 6, 2 };
public static int min = 1;
public static int arrayMin() {
int i = 0;
if (arr == null) {
return 0;
} else {
while (i < arr.length) {
// some function/method call to find smallest number of arr[i]
i++;
return min;
}
}
return min;
}
public …Run Code Online (Sandbox Code Playgroud) 我已经使用该paintComponent方法在我的面板上绘制形状.但是,每次我最小化帧或调整大小时,它们都会消失.不确定要添加到我的代码中的内容.
public class ShapePanel extends JPanel implements ActionListener, MouseListener{
int a,b,c,d;
Graphics2D g2D;
private Rectangle2D rect = new Rectangle2D(a,b,c-a,d-b);
public ShapePanel(){
addMouseListener(this);
setLayout(new GridLayout());
}
public void paintComponent(Graphics g) {
g2D = (Graphics2D) g;
g2D.draw(rect);
repaint();
}
//get methods for coordinates: MousePressed, MouseReleased
Run Code Online (Sandbox Code Playgroud) 我试图计算系列1,3,9,27,81的前16个元素的总和....代码通过首先创建一个适合的TYPE Seq实例来实现.
public class Geometric implements Seq {
private double b;
public Seq s;
public double sum;
public Geometric(double a) {
this.b = a;
}
public double valAtIndex(int i) {
// TODO Auto-generated method stub
return Math.pow(b, i);
}
public double total() {
s = new Geometric(3.0);
for (int a = 0; a < 16; a++) {
int c = -1;
sum = sum + s.valAtIndex(c = c + 1);
c++;
}
return sum;
}
public double getSum() {
return …Run Code Online (Sandbox Code Playgroud)