简体   繁体   中英

Using Java AWT components

I need to build a userform that requires the use of a Canvas therefore I want to not mix swing components with the AWT ones.

I have gone through quite a few tutorials already and it seems I am missing a key idea that structures the AWT framework.

I am using the GridLayout and GridBagConstraints to set where I want my components. However, resizing and positioning don't seem to work. I want to be able to have 4 rows, the top and the bottom row being quite small in height and going across the entire screen and then to have 4 large squares in a 2x2 grid in the middle of the Form.

frame = new Frame("Test Player");   
frame.setSize(800, 800); 
frame.setLayout(new GridLayout(4, 2));

This should give me the 4x2 grid that I need. Then I want to add a panel to the entire top portion.

GridBagConstraints c = new GridBagConstraints();

// Add a panel to the top of the frame
Panel controlPanel = new Panel();
controlPanel.setBackground(Color.black);
controlPanel.setBounds(0, 0, 100, 20);
c.gridx  = 0;
c.gridy = 0;
c.gridwidth = 2;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.FIRST_LINE_START;
frame.add(controlPanel, c);  

Why doesn't the setBounds method change the size of my panel?

How can I control the size and position of components in my Form? Is this the right approach for doing this, or am I using the tools wrong?

I need to build a userform that requires the use of a Canvas

Why?

Use a JPanel and use Swing. You can add components easily to a panel. It has functionality the same as a Canvas, but Swing is more advanced and has better features than AWT. Since you are just learning, spend the time learning Swing.

The Swing tutorial has all the Swing basics and is more comprehensive than anything you will find on AWT.

Why doesn't the setBounds method change the size of my panel?

Why are you trying to use setBounds()?

Swing (and AWT) both use layout managers.

The layout manager will reset the size and location of all the components based on the rules of the layout manager. The size of the panel will then be determined by the components added to the panel.

The tutorial has a section on How to Use a GridBagLayout which contains working examples to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM