JPanel runs on japplet

I am developing a JApplet, and basically this applet will allow the user to draw a quadratic graph of the equations, and he inserts the ranges of both the x axis and the y axis. But much remains to be done to achieve this goal.

I'm still in the interface design phase.

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); } } 
+2
source share
2 answers
  • To control the x-axis and the y-axis one above the other, you must have two panels, one of which includes labels and text fields for the x-axis in one and the y-axis in the other. Then you add them to the panel, which will be aligned vertically. ( Box.createVerticalBox() , for example)

  • You can make graph.java a ActionListener "Plot" and "Refine" buttons. In the actionPerformed graph.java method, you can initiate a redraw by collecting ranges from an instance of "ControlsB".

EDIT: answer your comments ...

'how to add another panel so that I can put the x axis above the y axis'

it 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? } 

'and how to collect ranges from an instance of ControlsB ...'

You should look into the ActionListener in this tutorial to understand how to get the click events button to trigger an action in a separate class.

In addition, two criticisms:

  • in your main class, GraphApplet , you create a Box before passing it to each of the ControlsA and ControlsB constructors. In the constructor, you reassign the field you entered. I do not think you need to do this. Either create a properly aligned cell in GraphApplet , pass it and don’t reassign it, or don’t transfer anything.

  • Your ControlsA and ControlsB classes are extended by JPanel . Although you try to add your Box containers to each of them at the end of your constructors, you never add these Controls + objects to any parent container. In your current implementation, I would suggest that the JPanel extension is not required.

+4
source

Recommendations:

  • As for having one component on top of another, use a layout manager that would facilitate this, such as a vertical BoxLayout.
  • In order for your display to respond to changes in numerical data, I recommend that you design your program in such a way that it is easy to implement using the Model-View-Controller . Pressing the "plot" or "refinement" button will call up the controller, which will update the model data. The view or graphical interface will listen to model changes, and when they happen, redesign the graph based on the latest model data.
+4
source

All Articles