我有一个作业,我必须允许一个用户使用二次方程来绘制一个图形。
我设法绘制了图形的骨架,现在我正在尝试显示“控制面板”供用户输入值。
我有4个文件:
graph.java
< li>
panel.java
< Li > < code > panelb . Java < Li > < code > panelc . Java
我的问题是,当我运行代码时,它只显示panel.java
,即使在容器中,它也应该显示其他两个面板。
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JPanel;
public class panel extends JPanel {
public panel(){
this.setBackground(Color.yellow);
}
}
有人能建议我应该做什么改变来解决这个问题吗?
我对图形.java文件进行了一些更改:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;
public class GraphApplet extends JApplet{
public GraphApplet(){
raph p = new Graph();//graph
p.setPreferredSize(new Dimension(760,500));
conn.add(p,BorderLayout.CENTER);
}
现在显示的只有图表。
关于其他代码,我也对类名做了一些更改:
gnjk;. java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JPanel;
public class Graph extends JPanel {
public Graph(){
this.setBackground(Color.yellow);
}
public void paintComponent(Graphics p) {
super.paintComponent(p);
Graphics2D graph = (Graphics2D)p;
this.setBackground(Color.yellow);//set background color.
int x,y,y1,x1,a,b,p1x,p1y,p2x,p2y;
int xstart = 7;
int ystart = 1;
int xfinish = 3;
.......
BHFVHN.java
import javax.swing.*;
import java.awt.*;
import javax.swing.JPanel;
public class ControlsA extends JPanel{
public void init (Box g) {
a = Box.createVerticalBox();
a.add(new JLabel("Please enter the values below:"));
a.add(new JLabel("h"));
}
}
jknmk.java
import javax.swing.*;
import java.awt.Component;
import java.awt.Dimension;
public class ControlsB extends JPanel{
public void init (Box b) {
b = Box.createHorizontalBox();
b.add(new JLabel("a"));
JTextField f1 = new JTextField("0.0");
f1.setMaximumSize(new Dimension(100,30));
b.add(f1);
}
}
这是我项目的更新:
jkl.java
import java.awt.BorderLayout;
import java.awt.Container;
public class GraphApplet extends JApplet{
public GraphApplet() {
public void init(){
SwingUtilities.invokeLater(new Runnable() {
public void run(){
Container conn = getContentPane();
conn.setLayout(new BorderLayout());
Graph z = new Graph();
conn.add(p,BorderLayout.CENTER);
fasfae a = new ControlsA(box1);
conn.add(a,BorderLayout.LINE_START);
adsfawef b = new ControlsB(box2);
conn.add(b,BorderLayout.PAGE_END);
}
});
}
}
/*Container conn = getContentPane();
conn.setLayout(new BorderLayout());
Graph p = new Graph();//graph
p.setPreferredSize(new Dimension(460,560));
conn.add(p,BorderLayout.CENTER);
Box a = new Box(BoxLayout.Y_AXIS);
a.setPreferredSize(new Dimension(50,50));
conn.add(a,BorderLayout.EAST);
Box b = new Box(BoxLayout.X_AXIS);
b.setPreferredSize(new Dimension(201,50));
conn.add(b,BorderLayout.SOUTH);*/
//this code is commented not to loose it
VTK.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JPanel;
class Graph extends JPanel {
public Graph(){
this.setBackground(Color.yellow);
}
@Override
public Dimension getPreferredSize(){return (new Dimension(460,560)); }
public void paint(Graphics z) {
Graphics graph = (Graphics2D)z;
this.setBackground(Color.yellow).
int x,y,y1,x1,a,b,p1x,p1y,p2x,p2y;
//line co-ordinates
//the numbers represent the number of boxes on the graph
int xstart = 7;
int ystart = 1;
int x = 3;
int y = 9;
//other variables
int i = 0;
int i2 = 0;
int m = 0;
int n = 0;
int m2 = 0;
int n2 = 0;
int f2 = 0;
int g2 = 1;
//ranges
int f = 5;
int g = -5;
//change -ve num to +ve
int g3 = Math.abs(g);
int a1 = g3 + f;
int b1 = a1;
a = (Height);
f = (Width);
}
}
}
// 6 variables the user has to input
}
@Override
public Dimension getPreferredSize() {return (new Dimension(200,100));}
}
NLLKL.java
@Override
public Dimension getPreferredSize(){return (new Dimension(201,50));}
}
还是没有起色。我不明白发生了什么事。
请现在尝试运行此代码,告诉我这更接近您想要的吗?如果是,请告诉我,以便我可以删除我的答案。请注意我所做的更改:
PanelC
类中,我没有使用 init()
方法,而是创建了构造函数。import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Graph extends JApplet{
public void init(){
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Container conn = getContentPane();
conn.setLayout(new BorderLayout());
Panel p = new Panel();//graph
conn.add(p,BorderLayout.CENTER);
Box box1 = new Box(BoxLayout.Y_AXIS);
PanelB a = new PanelB(box1);//vertical
conn.add(a,BorderLayout.LINE_START);
Box box2 = new Box(BoxLayout.X_AXIS);
PanelC b = new PanelC(box2);//horizontal
conn.add(b,BorderLayout.PAGE_END);
}
});
}
}
class Panel extends JPanel {
public Panel(){
this.setBackground(Color.yellow);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(460,560));
}
public void paintComponent(Graphics p) {
super.paintComponent(p);
Graphics2D graph = (Graphics2D)p;
Dimension appletSize = this.getSize();
int appletHeight = (int)(appletSize.height);
int appletWidth = appletSize.width;
this.setBackground(Color.yellow);//set background color.
int x,y,y1,x1,a,b,p1x,p1y,p2x,p2y;
//line co-ordinates
//the numbers represent the number of boxes on the graph
int xstart = 7;
int ystart = 1;
int xfinish = 3;
int yfinish = 9;
//other variables
int i = 0;
int i2 = 0;
int m = 0;
int n = 0;
int m2 = 0;
int n2 = 0;
int f2 = 0;
int g2 = 1;
//ranges
int f = 5;
int g = -5;
//change -ve num to +ve
int g3 = Math.abs(g);
int a1 = g3 + f;
int b1 = a1;
y1 = (appletHeight);
x1 = (appletWidth);
y = (appletHeight / 2);
x = (appletWidth / 2);
a = (appletWidth / a1);
b = (appletHeight / b1);
int d = (appletWidth / a1);
int e = (appletHeight / b1);
/**
to determine the
ammount of pixles there
is in each box of the
graph, both y-axis and
x-axis
*/
int xbox = x1 / 10;
int ybox = y1 / 10;
//line variables
//the xstart, ystart, etc represent the number of boxes
//top point of the line on the graph
p1x = xbox * xstart;//start x
p1y = ybox * ystart;//start y
//lowwer point of the line on the graph
p2x = xbox * xfinish;//finish x
p2y = ybox * yfinish;//finish y
//draw y-axis numbers
//(+ve)
while(f != 0){
String s = String.valueOf(f);
p.drawString(s,(x + 5),m + 13);
m = m + b;
f = f - 1;
}
//(-ve)
m2 = y;
while(f2 != g-1){
String u = String.valueOf(f2);
p.drawString(u,(x + 5),m2 - 3);
m2 = m2 + b;
f2 = f2 - 1;
}
//draw x-axis numbers.
//(-ve)
while(g != 0){
String t = String.valueOf(g);
p.drawString(t,n,y - 5);
n = n + a;
g = g + 1;
}
//(+ve)
n2 = x + a;
while(g2 != g3+1){
String vw = String.valueOf(g2);
p.drawString(vw,n2 -10,y - 5);
n2 = n2 + a;
g2 = g2 + 1;
}
BasicStroke aLine2 = new BasicStroke(1.0F,
BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
graph.setStroke(aLine2);
//notch on numbers and grid lines
//left to right, top to bottom notches
int v2 = -5;
int v5 = 0;
while(i <= a1-1){
p.setColor(Color.lightGray);//lightgray line
p.drawLine(a,0,a,y1);//vertical lightgray
p.drawLine(0,b,x1,b);//horizontal lightgray
a = a + d;
b = b + e;
i = i + 1;
}
//notches
while(i2 <= a1){
p.setColor(Color.blue);//notch color
p.drawString("x",v2+2,y+3);//xaxis
p.drawString("x",x-4,v5+4);//yaxis
v5 = v5 + e;
v2 = v2 + d;
i2 = i2 + 1;
}
//draws the border of the graph
p.setColor(Color.black);
Rectangle2D.Float rect = new Rectangle2D.Float(0,0,x1,y1);
BasicStroke aLine = new BasicStroke(2.5F,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
graph.setStroke(aLine);
graph.draw(rect);
//draw cross
BasicStroke aLine3 = new BasicStroke(2.5F,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
graph.setStroke(aLine3);
p.drawLine(x,0,x,y1); //vertical line
p.drawLine(0,y,x1,y); //horizontal line
//display the value of graph width and graph height
String aw = String.valueOf(x1);
p.drawString("Graph Width = ", 50,90);
p.drawString(aw,150,90);
p.drawString("Graph Height = ", 50,110);
String ah = String.valueOf(y1);
p.drawString(ah,156,110);
//draw line on graph
BasicStroke aLine4 = new BasicStroke(1.5F,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
graph.setStroke(aLine4);
p.setColor(Color.red);
if(p1x <= x1 && p2x <= x1 && p1y <= y1 && p2y <= y1){
p.drawLine(p1x,p1y,p2x,p2y);
Color c = new Color(0,0,0);
p.setColor(c);
p.drawString("X", p1x-4,p1y+4);
p.drawString("X", p2x-4,p2y+4);
}
else{
p.setColor(Color.black);
p.drawRect(48,34,223,35);
p.setColor(Color.white);
p.fillRect(49,35,222,34);
p.setColor(Color.red);
p.drawString("Wrong co-ordinates!!!", 50,50);
p.drawString("Values exceede applet dimensions.", 50,65);
}
}
}
class PanelB extends JPanel{
public PanelB (Box a) {
a = Box.createVerticalBox();
a.add(new JLabel("Please enter the values below:"));
a.add(new JLabel("a"));
JTextField g1 = new JTextField("0.0");
g1.setMaximumSize(new Dimension(100,30));
a.add(g1);
a.add(new JLabel("b"));
JTextField g2 = new JTextField("0.0");
g2.setMaximumSize(new Dimension(100,30));
a.add(g2);
a.add(new JLabel("c"));
JTextField g3 = new JTextField("0.0");
g3.setMaximumSize(new Dimension(100,30));
a.add(g3);
a.add(new JLabel("d"));
JTextField g4 = new JTextField("0.0");
g4.setMaximumSize(new Dimension(100,30));
a.add(g4);
a.add(new JButton("Plot"));
a.add(new JButton("Refine"));
add(a);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(200,100));
}
}
class PanelC extends JPanel{
public PanelC (Box b) {
b = Box.createHorizontalBox();
b.add(new JLabel("a"));
JTextField f1 = new JTextField("0.0");
f1.setMaximumSize(new Dimension(100,30));
b.add(f1);
b.add(new JLabel("b"));
JTextField f2 = new JTextField("0.0");
f2.setMaximumSize(new Dimension(100,30));
b.add(f2);
b.add(new JLabel("c"));
JTextField f3 = new JTextField("0.0");
f3.setMaximumSize(new Dimension(100,30));
b.add(f3);
b.add(new JLabel("d"));
JTextField f4 = new JTextField("0.0");
f4.setMaximumSize(new Dimension(100,30));
b.add(f4);
b.add(new JButton("Plot"));
b.add(new JButton("Refine"));
add(b);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(201,50));
}
}
这是我使用的超文本标记语言文件:
<html>
<p> This file launches the 'Graph' applet: Graph.class! </p>
<applet code= "Graph.class" height = 550 width = 1000>
No Java?!
</applet>
</html>
这是我得到的输出:
该代码中的主要问题是:
面板
Panel
B 的每个实例一个
一旦这些事情得到处理,它就会出现这种情况。
其他问题。
面板
。事实上,甚至面板
也可以更改为在JLabel
BufferedImage
(图形)
panelB
和panelC
是完全冗余的,只需将Box
直接添加到所需的父组件的布局区域即可EachWordUpperCase
GraphApplet
、图形区域Graph
……我不确定最后两个面板的名称,因为它们包含相同的组件。如果只有一个,我可以将其称为<code>Controls</code>作为一个类(这一点太过分了),或者如果它是一个普通<code>JPanel</code<或<code>Box</code的实例,则称为<code>Controls</code>…我将如何更改我的小程序中的代码,以便小程序在每个面板中添加一个(?)
改变:
panel a = new panel();//vertical
致:
panelB a = new panelB(new Box(BoxLayout.Y_AXIS));//vertical
是最简单的方法。请注意,如果您决定直接添加< code>Box,它会发生变化。
Box a = new Box(BoxLayout.Y_AXIS);//vertical
System.out.println(deepArray2String(new int[][]{{1},{2},{4}})); 输出应为[1]、[2]、[4]] System.out.println(deepArray2String(new int[][]{{1},{2,3}})); System.out.println(deepArray2String(new int[][]{{5,3},{1}
构建哈夫曼树的步骤输入是唯一字符的数组及其出现频率,输出是哈夫曼树。 > 为每个唯一字符创建一个叶节点,并构建所有叶节点的最小堆(最小堆用作优先级队列。频率字段的值用于比较最小堆中的两个节点。最初,最不频繁的字符位于根) 从最小堆中以最小频率提取两个节点。 创建一个新的内部节点,其频率等于两个节点频率之和。将第一个提取的节点作为其左子节点,将另一个提取的节点作为其右子节点。将此节点添加到最小堆。
我正在使用Material-用户界面-Next,并试图以一种非常特殊的方式创建图像。 我有一些相当奇怪的问题。 1-3出现在1下面 2-2和3不会填满它们的整个空间(当突出显示时,无论图像如何,只有一半的内容空间被利用) 3-有时我会得到类似以下的东西: 为了便于参考,如果您遵循此链接,那么您将转到物料ui下一个GridList文档。在高级网格列表下,您将看到一个大图像和许多小图像。我的目标是将其
model的struct必须有一个pk Beego规定在models里面的struct必须有一个PK,如果没有的话,会提示 github.com/sunnygocms/managementCMS/models.SunnyUserAndGroup need a primary key field 这个是以bee -api 根据sunny_user_and_group这个表自动生成的为例来解释。 原生
我正在创建一个谷歌数据流管道使用Apache梁2. x 基本上,我有一个文本文件,每一行都包含一个英语句子。 我试图调用谷歌NLP(情感)API为每一个新的行/句子。 因此,我有一个调用NLP API的函数: 我用ParDo为每个句子调用这个函数。我假设,下面的ParDo将自动为文本文件中的每一行调用NLP情感api(基本上我不必遍历文本文件中的每一行!) 但在执行数据流后,我得到了这个错误: T
下面是fiddle http://jsfiddle.net/sgtrx/中的代码(应该在早些时候完成,对不起) 好的,我的导航栏在Div包装器内,在标题下,在内容区域(主体)的顶部。 我是个新手,所以请原谅我可能犯的任何错误。 当我添加边框来分隔每个块(按钮或文本)时,它很好地分隔了按钮,然而,它在导航栏的末尾(右侧)留下了一个小空格。 包装器Div是1000px,我有5个按钮,每个200px,因