在 JApplet 上运行的 JPanel [英] JPanel running on a JApplet
问题描述
我正在设计一个 JApplet,基本上这个小程序将允许用户绘制二次方程图,并插入 x 轴和 y 轴的范围.但要达到这一点,还有很多工作要做.
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. But there is much more to do to reach that point.
我还在设计界面.
这是我的代码:
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);
}
}
推荐答案
要将 x 轴和 y 轴控件放在一起,您应该有两个面板,一个面板包括 x 轴的标签和文本字段,一个是 y 轴的标签和文本字段在另一个.然后,您可以将它们添加到垂直对齐的面板中.(例如,
Box.createVerticalBox()
)
您可以使您的 graph.java
成为Plot"和Refine"按钮的 ActionListener
.在 graph.java 的 actionPerformed
方法中,您可以启动重绘,从 'ControlsB' 实例收集范围.
You can make your graph.java
an ActionListener
of the 'Plot' and 'Refine' buttons. In the actionPerformed
method of graph.java, you can initiate a repaint, gathering the ranges from the 'ControlsB' instance.
回复您的评论...
'如何添加另一个面板以便我将 x 轴放置在 y 轴上方'
这可以像(在 ControlsB.java 中)一样简单:
this can be as simple as (in 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?
}
'以及如何从 ControlsB 实例收集范围...'
您应该查看 ActionListener>本教程了解如何在单独的类中让按钮点击事件触发动作.
You should look into ActionListener
s in this tutorial to understand how to make the button click events trigger action in a separate class.
另外,两个批评:
在您的主类
GraphApplet
中,您正在创建一个Box
,然后再将其传递给每个ControlsA
和ControlsB
的构造函数.在构造函数中,您然后重新分配您传入的 Box.我认为您不需要这样做.要么在GraphApplet
中创建正确对齐的框,将其传入且不重新分配,或者根本不传入任何内容.
in your main class,
GraphApplet
, you are creating aBox
before passing it into each ofControlsA
andControlsB
's constructor. In the constructor, you then reassign the Box that you've passed in. I don't think that you need to do that. Either create the correctly aligned box inGraphApplet
, pass that in and don't reassign, or don't pass anything in at all.
您的ControlsA
和ControlsB
类都扩展了JPanel
.尽管您在构造函数的末尾将 Box
容器添加到每个容器中遇到了麻烦,但您永远不会将这些 Controls+ 对象添加到任何父容器中.在您当前的实现中,我建议不需要扩展 JPanel
.
Your ControlsA
and ControlsB
classes both extend 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. In your current implementation, i would suggest that extending JPanel
is not necessary.
这篇关于在 JApplet 上运行的 JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!