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

Posted

技术标签:

【中文标题】我希望我的按钮与我的 gui 中的选择按钮不在同一行?我将如何做到这一点?【英文标题】:I want my button not in the same row as my choose buttons in my gui? How will I do that? 【发布时间】:2022-01-15 13:28:33 【问题描述】:

如何添加中断以将“制作 pokemon”按钮和 textarea 与“Pokemon 选择”不在同一行。我正在尝试放置一个空的 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);     

这是我的 GUI 的样子: enter image description here

但它应该显示如下: enter image description here

有人可以向我解释我该怎么做吗?

【问题讨论】:

嵌套 JPanel,每个都使用自己的布局管理器。尝试使用各种布局管理器以获得最佳组合(并获得一些乐趣)。您可以在此处找到布局管理器教程:Layout Manager Tutorial,您可以在此处找到 Swing 教程和其他 Swing 资源的链接:Swing Info。 @HovercraftFullOfEels 谢谢!这个网站会帮助我 【参考方案1】:

使用不同的布局管理器(除 JPanel 使用的默认 FlowLayout 之外)

更多详情请见Laying Out Components Within a Container。

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);
        
    

另外,避免使用setPreferredSize,让布局管理器完成他们的工作。在示例中,我使用insets(来自GridBagConstraints)和EmptyBorder 在组件周围添加一些额外的空间

另外,小心使用 AWT 组件(即Choice),它们并不总是能很好地与 Swing 配合使用。在这种情况下,您应该使用JComboBox

【讨论】:

谢谢!我确实重做并使用了 GridBagLayouts!

以上是关于我希望我的按钮与我的 gui 中的选择按钮不在同一行?我将如何做到这一点?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 Kivy 与我的计算机通信? [关闭]

单击按钮后在同一 gui 中显示标签

如何计算与我的 CSS 选择器匹配的元素数量?

如何使用按钮小部件执行外部脚本?

如何在我的 GUI 上绘图

试图让我的播放按钮与我的暂停按钮和谐地工作