How do I insert layout managers without using swing classes / methods?

I need to make an applet in Java 6, 640 * 480 pixels, with a toolbar at the bottom using buttons, scroll bars and labels. The best way I could think of is to use BorderLayout , creating a BorderLayout.SOUTH area. GridBagLayout a GridBagLayout (which will contain the controls), and the rest of the BorderLayout null as a place for graphs using the controls. I can’t find any resources on the Internet that don’t use swing, and I don’t know anything about swing in order to deduce what they are doing or how to translate it into awt code. That's where I am now. The code suddenly ends in init() , starting from where the layouts start. Thanks for any help. Let me know if you need more information then here.

 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Bounce extends Applet implements ActionListener, AdjustmentListener { private static final long serialVersionUID = 1L; private Graphics page; //buttons String shapeButtonText = "Square"; Button shape = new Button(shapeButtonText); Button quit = new Button("Quit"); Button run = new Button("Run"); Button tail = new Button("Tail"); Button clear = new Button("Clear"); //labels Label speedLabel = new Label("Speed", Label.CENTER); Label sizeLabel = new Label("Size", Label.CENTER); //scrollbars private final int barHeight = 20; private final int SLIDER_WIDTH = 10; private final int MAXSPEED = 110; private final int MINSPEED = 0; private final int UNIT_INC = 1; private final int BLOC_INC = 10; private final int MAX_SIZE = 110; private final int MIN_SIZE = 10; Scrollbar speedBar = new Scrollbar(Scrollbar.HORIZONTAL, MINSPEED, SLIDER_WIDTH, 0, MAXSPEED); Scrollbar sizeBar = new Scrollbar(Scrollbar.HORIZONTAL, MIN_SIZE, SLIDER_WIDTH, 0, MAX_SIZE); //methods public void init() { //set up objects //speed scroll bar speedBar.setUnitIncrement(UNIT_INC); speedBar.setBlockIncrement(BLOC_INC); speedBar.setValue(MAXSPEED/2); //size scrollbar sizeBar.setUnitIncrement(UNIT_INC); sizeBar.setBlockIncrement(BLOC_INC); sizeBar.setValue(MAX_SIZE/2); //draw the window BorderLayout window = new BorderLayout(); GridBagLayout toolbar = new GridBagLayout(); //? } public void start() { } public void run() { } public void actionPerformed(ActionEvent e) { } public void adjustmentValueChanged(AdjustmentEvent e) { } public void stop() { } public void destory() { } } 
+2
source share
1 answer

Let's clarify a few things. A LayoutManager used by a Container (such as Frame , Panel or Applet ) to calculate the position and size of components inside a Container . This means that it is wrong to talk about "nesting LayoutManager s". On the other hand, you can embed the Container into each other and give each one its own LayoutManager . I believe that this is what you want to do.

Let me illustrate this with a far-fetched example:

 public class MyGUI { public static void main(String[] args) { Frame f = new Frame("Layout Example"); Panel mainPanel = new Panel(new BorderLayout()); f.add(mainPanel); Panel toolBar = new Panel(new FlowLayout()); toolBar.add(new Button("Button 1")); toolBar.add(new Button("Button 2")); mainPanel.add(tollBar.NORTH); Panel statusBar = new Panel(new FlowLayout()); statusBar.add(new Label("Status")); mainPanel.add(statusBar); f.pack(); f.show(); } } 

Note that you need to create a new Panel for each LayoutManager . Rather, every Panel you create needs a LayoutManager . In addition, this example can easily be changed from AWT to Swing, replacing Frame with JFrame , Panel with JPanel , Button with JButton and Label with JLabel .

ps The above code is not verified. However, this should illustrate the concepts presented here.

+5
source

Source: https://habr.com/ru/post/927701/


All Articles