[英]JPanel running on a JApplet
I am designing a JApplet, and basically this applet will allow a user to draw a quadratic equation graph and it inserts the ranges of both x-axis and y-axis. 我正在设计一个JApplet,基本上这个applet将允许用户绘制二次方程图,并插入x轴和y轴的范围。 But there is much more to do to reach that point. 但要达到这一点还有很多工作要做。
I am still in the phase of designing the interface. 我仍处于设计界面的阶段。
Here is my code: 这是我的代码:
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Applet extends JApplet {
JPanel p1;
JPanel p2;
JPanel p3;
JScrollPane s1;
public Applet() {
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
s1 = new JScrollPane(p3,s1.VERTICAL_SCROLLBAR_ALWAYS,s1.HORIZONTAL_SCROLLBAR_ALWAYS);
}
@Override
public void init() {
super.init();
for(int i=0;i<100;i++)
{
p3.add(new JButton("Hello"));
p3.add(new JLabel("blah"));
p3.add(new JButton("Sup"));
}
p1.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
p2.setPreferredSize(new Dimension(this.getWidth(),(int) (this.getHeight()*0.6667)));
p3.setLayout(new BoxLayout(p3,BoxLayout.PAGE_AXIS));
s1.setPreferredSize(new Dimension(this.getWidth(),(int)(this.getHeight()*0.33333)));
p1.add(p2);
p1.add(s1);
this.add(p1);
}
}
to have the x-axis and y-axis controls on top of each other, you should have two panels, one including the labels and text fields for the x-axis in one and those for the y-axis in the other. 要使x轴和y轴控制在彼此之上,您应该有两个面板,一个包括x轴的标签和文本字段,另一个包含y轴。 You would then add those to a panel that is aligned vertically. 然后,您可以将它们添加到垂直对齐的面板。 ( Box.createVerticalBox()
, for example) (例如Box.createVerticalBox()
)
You can make your graph.java
an ActionListener
of the 'Plot' and 'Refine' buttons. 你可以让你的graph.java
成为'Plot'和'Refine'按钮的ActionListener
。 In the actionPerformed
method of graph.java, you can initiate a repaint, gathering the ranges from the 'ControlsB' instance. 在graph.java的actionPerformed
方法中,您可以启动重绘,从'ControlsB'实例收集范围。
EDIT: responding to your comments... 编辑:回复你的评论......
'how to add another panel in order for me to place the x-axis above the y-axis' '如何添加另一个面板以便我将x轴放在y轴上方'
this can be as simple as (in ControlsB.java): 这可以像(在ControlsB.java中)一样简单:
b = Box.createHorizontalBox();
b.add(new JLabel("Please enter range: "));
Box b0 = Box.createVerticalBox();//create a vertical box to stack the controls
Box b1 = Box.createHorizontalBox(); // create a horizontal box for the x-axis
b1.add(new JLabel(" x-axis "));
b1.add(new JLabel("from"));
JTextField f1 = new JTextField("-5");
f1.setMaximumSize(new Dimension(100,30));
b1.add(f1);
b1.add(new JLabel(" to "));
JTextField f2 = new JTextField("5");
f2.setMaximumSize(new Dimension(100,30));
b1.add(f2);
b1.add(new JLabel(". "));
Box b2 = Box.createHorizontalBox(); // create a second horizontal box for the y-axis
b2.add(new JLabel("y-axis "));
b2.add(new JLabel("from"));
JTextField f3 = new JTextField("5");
f3.setMaximumSize(new Dimension(100,30));
b2.add(f3);
b2.add(new JLabel("to"));
JTextField f4 = new JTextField("-5");
f4.setMaximumSize(new Dimension(100,30));
b2.add(f4);
b0.add(b1); // add the x-axis to the vertical box
b0.add(b2); // add the y-axis to the vertical box
b.add(b0); // add the vertical box to the parent
b.add(new JButton("Plot"));
b.add(new JButton("Refine"));
add(b); //is this necessary?
}
' and how to gather the ranges from the ControlsB instance...' '以及如何从ControlsB实例中收集范围......'
You should look into ActionListener
s in this tutorial to understand how to make the button click events trigger action in a separate class. 您应该查看本教程中的 ActionListener
,以了解如何在单独的类中使按钮单击事件触发器操作。
Also, two critiques: 还有两个批评:
in your main class, GraphApplet
, you are creating a Box
before passing it into each of ControlsA
and ControlsB
's constructor. 在你的主类GraphApplet
,你在将它传递给ControlsA
和ControlsB
的每个构造函数之前创建一个Box
。 In the constructor, you then reassign the Box that you've passed in. I don't think that you need to do that. 在构造函数中,然后重新分配您传入的Box。我认为您不需要这样做。 Either create the correctly aligned box in GraphApplet
, pass that in and don't reassign, or don't pass anything in at all. 在GraphApplet
创建正确对齐的框,传入该框并且不重新分配,或者根本不传递任何内容。
Your ControlsA
and ControlsB
classes both extend JPanel
. 您的ControlsA
和ControlsB
类都扩展了JPanel
。 Although you go to the trouble of adding your Box
containers to each of these at the end of their constructors, you don't ever add those Controls+ objects to any parent container. 虽然您在构造函数的末尾添加Box
容器时遇到了麻烦,但是您不会将这些Controls +对象添加到任何父容器中。 In your current implementation, i would suggest that extending JPanel
is not necessary. 在您当前的实现中,我建议不要扩展JPanel
。
Recommendations: 建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.