Placing components in Java Swing
Oh, there's hay. I didn't see you I want to know if there is a better and more effective way to format tags, panels and buttons in Java, and then I do it below This is my code. I want to put welcome, date and button on different lines The only reasonable way to do this is to create a blank label, right?
private void GeneralTab() { generalPanel = new JPanel(new FlowLayout()); String currentTime = SimpleDateFormat.getInstance().format( Calendar.getInstance().getTime()); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel("WELCOME ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel("Today's Date: " + currentTime)); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(new JLabel(" ")); generalPanel.add(createExitButton()); }
Solution
It seems that you need to know more about layoutmanagers They can help you lay out components in different ways
Check out the java tutorial They may be the best way to start using different layouts
This is a link to the tutorial:
In your case, it looks like you want to use @ R_ 149_ 2419@Layout. You should put the required components on a line in their own JPanel Then, add each JPanel to the generalpanel You need to set the layout manager of generalpanel to @ R_ 149_ 2419@Layout , as follows:
generalPane.setLayout(new @R_149_2419@Layout(generalPane,@R_149_2419@Layout.Y_AXIS));