简体   繁体   English

我希望我的按钮与我的 gui 中的选择按钮不在同一行? 我将如何做到这一点?

[英]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.我正在尝试放置一个空的 JLabel,但我认为它不起作用。

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这是我的 GUI 的样子:在此处输入图像描述

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)使用不同的布局管理器(除了JPanel使用的默认FlowLayout

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.另外,避免使用setPreferredSize ,让布局管理器完成他们的工作。 In the example I'm used insets (from GridBagConstraints ) and an EmptyBorder to add some additional space around the components在示例中,我使用了insets (来自GridBagConstraints )和EmptyBorder在组件周围添加一些额外的空间

Also, be careful of using AWT components (ie Choice ), they don't always play nicely with Swing.此外,请注意使用 AWT 组件(即Choice ),它们并不总是能很好地与 Swing 配合使用。 In this case, you should be using JComboBox在这种情况下,您应该使用JComboBox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为我的 GUI 实现重置按钮? - How do I implement a reset button for my GUI? 如何在我希望的时间显示按钮? - How do I make my button display at the time I want it to? 如何防止我的java GUI按钮缩小(包括GIF)? - How do I prevent my java GUI buttons from shrinking (GIF included)? 如何根据所需的输出打印数组? 逐行 - How do I print my array based on the output that I want? Row by Row 如何选择文件阵列列表中的特定文件? - How do I choose an specific file of my file Array List? 如何消除按钮周围的蓝色边框? 对于Java GUI - How do i get rid of a blue border surrounding my button? For Java GUI 按下按钮后,如何使GUI驱动程序类中的变量转移到ActionListener类中? - How do I make the the variables in my GUI Driver class to be transferred to the ActionListener class after a button is pressed? 如何为Tic Tac Toe GUI游戏制作正确的重启按钮? - How do I make a proper restart button for my Tic Tac Toe GUI game? 如何使ActionListener按钮在我的Java GUI程序中保持骰子? - how do i make an ActionListener button to hold a dice in my java GUI program? 我想通过在当前应用程序中按一个按钮来终止/关闭特定应用程序。 我怎样才能做到这一点? - I want to kill/ close a particular app by pressing a button in my current app. How can I do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM