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.
source share