簡體   English   中英

我希望我的按鈕與我的 gui 中的選擇按鈕不在同一行? 我將如何做到這一點?

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

如何添加中斷以將我的“制作口袋妖怪”按鈕和文本區域與我的“口袋妖怪選擇”不在同一行。 我正在嘗試放置一個空的 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 的樣子:在此處輸入圖像描述

但它應該顯示如下:在此處輸入圖像描述

有人可以向我解釋我該怎么做嗎?

使用不同的布局管理器(除了JPanel使用的默認FlowLayout

有關更多詳細信息,請參閱在容器中布置組件

在此處輸入圖像描述

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM