简体   繁体   中英

I want my button not in the same row as my choose buttons in my gui? How will I do that?

How do I add a break to put my "Make pokemon" buttons and textarea not in the same row as my "Pokemon choice." I'm trying to put an empty JLabel, but I don't think it works.

public class PokemonPanel extends JPanel {

 private JLabel lTitle = new JLabel("Pokemon");
 private JLabel lMsg = new JLabel("                ");
   private JButton bDone = new JButton(" Make Pokemon ");
   private JButton bClear = new JButton(" Clear ");

   private JPanel topSubPanel = new JPanel();
   private JPanel centerSubPanel = new JPanel();
   private JPanel bottomSubPanel = new JPanel();
   private GUIListener listener = new GUIListener();
   private Choice chSpe = new Choice();
   private JLabel lEmp = new JLabel("                ");
   private PokemonGUILizylf st;
   private final int capacity = 10;
   private PokemonGUILizylf[ ] stArr = new     PokemonGUILizylf[capacity];
   private int count = 0;
   private String sOut = new String("");
   private JTextArea textArea = new JTextArea(400, 500);
  private JTextArea textArea2 = new JTextArea(400, 500);


   private JScrollPane scroll = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
   JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);


   public PokemonPanel() {

  this.setLayout(new BorderLayout()); 
  this.setPreferredSize(new Dimension(400, 500));
  topSubPanel.setBackground(Color.cyan); 
  centerSubPanel.setBackground(Color.white); 
  bottomSubPanel.setBackground(Color.white); 
 
  topSubPanel.add(lTitle);
  this.add("North", topSubPanel); 

  JLabel lSpe = new JLabel("Pokemon Available: ");
  JLabel lEmp = new JLabel("         ");
  JLabel lNew = new JLabel("New Pokemon: ");

 //add choices to the choice dropdown list
  chSpe.add("Choose");
  chSpe.add("Bulbasaur");
  chSpe.add("Venusaur");
  chSpe.add("Ivysaur");
  chSpe.add("Squirtle");
  chSpe.add("Wartortle");
  chSpe.add("Blastoise");
  chSpe.add("Charmander");
  chSpe.add("Charmeleon");
  chSpe.add("Charizard");  
 
  centerSubPanel.add(lSpe);
  centerSubPanel.add(chSpe);
  centerSubPanel.add(lEmp);
  centerSubPanel.add(bDone);
  centerSubPanel.add(lNew);
  textArea.setPreferredSize(new Dimension(500, 200));
  textArea.setEditable(false);
  textArea2.setPreferredSize(new Dimension(500, 200));
  textArea2.setEditable(false);
  
  
  textArea.setBackground(Color.white);
  textArea.setEditable(false);
  scroll.setBorder(null);
  centerSubPanel.add(scroll);  //add scrollPane to panel, textArea inside.        
  scroll.getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
  add("Center", centerSubPanel);
 
  bottomSubPanel.add(lMsg);
 
  bDone.addActionListener(listener); //add listener to button
  bottomSubPanel.add(bClear);
  bClear.addActionListener(listener); //add listener to button 
 //add bottomSubPanel sub-panel to South area of main panel      
  add("South", bottomSubPanel);     

}

This is what my GUI looks like: enter image description here

But it should show like this: enter image description here

Can someone explain to me how I can do that?

Use a different layout manager (other then default FlowLayout which JPanel uses)

See Laying Out Components Within a Container for more details.

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new PokemonPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PokemonPanel extends JPanel {

        private JLabel lTitle = new JLabel("Pokemon");
//        private JLabel lMsg = new JLabel("                ");
        private JButton bDone = new JButton("Make Pokemon ");
        private JButton bClear = new JButton("Clear");

        private JPanel topSubPanel = new JPanel();
        private JPanel centerSubPanel = new JPanel(new GridBagLayout());
        private JPanel bottomSubPanel = new JPanel();
//        private GUIListener listener = new GUIListener();
        private JComboBox<String> chSpe = new JComboBox<>();
        private JLabel lEmp = new JLabel("                ");
//        private PokemonGUILizylf st;
        private final int capacity = 10;
//        private PokemonGUILizylf[] stArr = new PokemonGUILizylf[capacity];
//        private int count = 0;
//        private String sOut = new String("");
//        private JTextArea textArea = new JTextArea(400, 500);
//        private JTextArea textArea2 = new JTextArea(400, 500);
//
//        private JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
//                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        public PokemonPanel() {

            this.setLayout(new BorderLayout());
//            this.setPreferredSize(new Dimension(400, 500));
            topSubPanel.setBackground(Color.cyan);
            centerSubPanel.setBackground(Color.white);
            bottomSubPanel.setBackground(Color.white);

            topSubPanel.add(lTitle);
            this.add("North", topSubPanel);

            JLabel lSpe = new JLabel("Pokemon Available: ");
            JLabel lNew = new JLabel("New Pokemon: ");

            //add choices to the choice dropdown list
            DefaultComboBoxModel<String> chSpeModel= new DefaultComboBoxModel<>();
            chSpeModel.addElement("Choose");
            chSpeModel.addElement("Bulbasaur");
            chSpeModel.addElement("Venusaur");
            chSpeModel.addElement("Ivysaur");
            chSpeModel.addElement("Squirtle");
            chSpeModel.addElement("Wartortle");
            chSpeModel.addElement("Blastoise");
            chSpeModel.addElement("Charmander");
            chSpeModel.addElement("Charmeleon");
            chSpeModel.addElement("Charizard");
            chSpe.setModel(chSpeModel);

            centerSubPanel.setBorder(new EmptyBorder(4, 4, 4, 4));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.LINE_END;
            centerSubPanel.add(lSpe, gbc);
            
            gbc.gridx++;
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            centerSubPanel.add(chSpe);

            gbc.anchor = GridBagConstraints.NORTH;
            gbc.gridwidth = 1;
            gbc.gridx = 0;
            gbc.gridy++;            
            centerSubPanel.add(bDone, gbc);

            gbc.gridx++;
            gbc.anchor = GridBagConstraints.FIRST_LINE_END;
            centerSubPanel.add(lNew, gbc);

            gbc.gridx++;
            gbc.gridheight = gbc.REMAINDER;
            centerSubPanel.add(new JScrollPane(new JTextArea(10, 10)), gbc);

//            textArea.setEditable(false);
//            textArea2.setEditable(false);
//
//            textArea.setBackground(Color.white);
//            textArea.setEditable(false);
//            scroll.setBorder(null);
//            centerSubPanel.add(scroll);  //add scrollPane to panel, textArea inside.        
//            scroll.getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
            add("Center", centerSubPanel);

//            bottomSubPanel.add(lMsg);

//            bDone.addActionListener(listener); //add listener to button
            bottomSubPanel.add(bClear);
//            bClear.addActionListener(listener); //add listener to button 
            //add bottomSubPanel sub-panel to South area of main panel      
            add("South", bottomSubPanel);
        }
    }
}

Also, avoid using setPreferredSize , let the layout managers do their job. In the example I'm used insets (from GridBagConstraints ) and an EmptyBorder to add some additional space around the components

Also, be careful of using AWT components (ie Choice ), they don't always play nicely with Swing. In this case, you should be using JComboBox

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