【发布时间】:2016-11-25 20:33:53
【问题描述】:
我正在开发一个程序,该程序根据用户给出的输入绘制线性函数。 我设法创建了一种绘制“线”(很多点)的方法。
正如您在屏幕截图中看到的,右侧有一些空间。我想添加一些 JButtons、JLabels 和 JTextFields 以便用户可以输入函数的数据。
但如果我添加一些 JButton 或一些 JLabel,它们将不会显示在右侧。谁能解释我这种行为的原因?我将上传不带 JButton 的版本的源代码。 谢谢大家的帮助!
package projekt;
import javax.swing.*;
import java.awt.*;
public class ProjectFunction extends JFrame {
public ProjectFunction() {
setLayout(new BorderLayout());
setSize(1900, 1000);
setTitle("First Test");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g){
g.setColor(new Color(204, 204, 204));
g.drawLine(0, 900, 1000, 900);
g.drawLine(0, 800, 1000, 800);
g.drawLine(0, 700, 1000, 700);
g.drawLine(0, 600, 1000, 600);
g.drawLine(0, 400, 1000, 400);
g.drawLine(0, 300, 1000, 300);
g.drawLine(0, 200, 1000, 200);
g.drawLine(0, 100, 1000, 100);
g.drawLine(100, 0, 100, 1000);
g.drawLine(200, 0, 200, 1000);
g.drawLine(300, 0, 300, 1000);
g.drawLine(400, 0, 400, 1000);
g.drawLine(600, 0, 600, 1000);
g.drawLine(700, 0, 700, 1000);
g.drawLine(800, 0, 800, 1000);
g.drawLine(900, 0, 900, 1000);
g.setColor(Color.BLACK);
g.drawRect(0, 500, 1000, 1);
g.drawRect(500, 0, 1, 1000);
g.setColor(Color.RED);
linear(0.25, 1, g);
g.setColor(Color.BLUE);
linear(-3, -2.5, g);
}
public void linear(double s, double c, Graphics g) {
int Anzpunkte = 0;
c = c * 100;
int x = 500, y = 500 - (int) c;
g.drawOval(x, y, 2, 2);
y = y - (int) s;
double abtrag = s - (int) s;
System.out.println("Punkt X-Achse Y-Achse Abtrag Steigung");
Anzpunkte++;
System.out.println("" + Anzpunkte + " " + x + " " + y + " " + abtrag + " " + s);
while (x < 1000 && y < 1000 && x > 0 && y > 0) {
x++;
g.drawOval(x, y, 2, 2);
Anzpunkte++;
System.out.println("" + Anzpunkte + " " + x + " " + y + " " + abtrag + " " + s);
if (abtrag >= 1 || abtrag <= -1) {
y = y - (int) s;
y = y - (int) abtrag;
abtrag = s - (int) s;
} else {
y = y - (int) s;
abtrag = abtrag + s - (int) s;
}
}
x = 500;
y = 500 - (int) c;
while (x < 1000 && y < 1000 && x > 0 && y > 0) {
x--;
g.drawOval(x, y, 2, 2);
Anzpunkte++;
System.out.println("" + Anzpunkte + " " + x + " " + y + " " + abtrag + " " + s);
if (abtrag >= 1 || abtrag <= -1) {
y = y + (int) s;
y = y + (int) abtrag;
abtrag = s - (int) s;
} else {
y = y + (int) s;
abtrag = abtrag + s - (int) s;
}
}
}
public static void main(String[] args) {
ProjectFunction p = new ProjectFunction();
}
}
【问题讨论】:
-
在您发布的任何地方都没有添加任何按钮或标签组件的代码。
-
欢迎来到 Stack Overflow!请查看我们的SO Question Checklist 以帮助您提出一个好问题,从而得到一个好的答案。
标签: java swing jframe jpanel draw