简体   繁体   中英

I'm having issues with aligning elements (Java GUI / Swing);

I'm attempting to align TextField vertically next to some labels, however the alignment isn't working for the TextField in which it does the Labels

At first I thought it was size miscalculations however that's not the case, I've tried aligning left and right panels to their according sides (EAST and WEST) however that didn't work either.

JPanel leftPanel = new JPanel();
leftPanel.setBounds(0,0, 100, 300 );
JPanel rightPanel = new JPanel();
rightPanel.setBounds(100, 0, 50, 300);

JLabel fiftyNoteLabel = new JLabel("TOTAL $50s");
JLabel twentyNoteLabel = new JLabel("TOTAL $20s");

JTextField fiftyNoteText = new JTextField("0");
JTextField twentyNoteText = new JTextField("0");

main.add( leftPanel );
main.add( rightPanel );

leftPanel.add( fiftyNoteLabel, BorderLayout.NORTH );
leftPanel.add( twentyNoteLabel, BorderLayout.NORTH );
rightPanel.add( fiftyNoteText, BorderLayout.NORTH );
rightPanel.add( twentyNoteText, BorderLayout.NORTH );

main.setVisible( true );
leftPanel.setVisible( true );
rightPanel.setVisible( true );

Ooops, turns out Java handles components being added sequentially, I was attempting to alter the position by changing the alignment of the element. However I was supposed to be adding the elements in order.

EG. LABEL -> TEXTFIELD = "Hello" [ ]

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