java中的swing按钮布局

bzzcjhmw  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(506)

所以我在java工作,并且设置了按钮。我现在唯一想做的就是把这么多的按钮放在屏幕的一边,把它们分开 JFrame 在世界的另一边有那么多人 JFrame . 有什么建议吗?哦,还有一个 JList 与其中一个按钮相配。
[更新]刚刚更新了我的代码,到目前为止,没有任何改进,仍然声明它不会打开gui。

package SystemandDesign.RISK;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;

//Now for the JList options;

import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;
import javax.swing.JPanel;
//Will be used to created JList options

//Colors to be used for testing purposes.

import java.awt.Color;

//panel to separate buttons.
import java.awt.GridLayout;
import java.awt.BorderLayout;

public class GamePage extends JFrame{

  private JButton surrender;
  private JButton draw;
  private JButton attackRoll;
  private JButton defendRoll;
  private JButton trade;
  private JButton main;
  private JButton quit;
  private JFrame frame;
  private JPanel east;
  private JPanel west;

  private JList terNames;//Used primarily at the beginning of the game to reinforce territories.
  //terNames is also used to reinforce current owned areas.
  private JList terAttack;//Fills up as terrritories that are able to attack
  private static final String[] terProp = {"A-bombing site", "'A'ir-defense", "AA-testing",
    "A hint", "A-field", "'A'ir Force", "A-prep", "A-landing"};
  private static final Color[] terColor = {Color.RED, Color.BLUE,Color.GREEN,Color.PINK,
    Color.ORANGE,Color.MAGENTA,Color.LIGHT_GRAY, Color.YELLOW};//The use of colors is being used to test if the List is
  //responsive. Will later be taken out and replaced to highlight territories.

  public GamePage(){

    super("Risk");
    //set up the borders

    frame.add(east,BorderLayout.EAST);
    frame.add(west,BorderLayout.WEST);
    frame.setLayout(new BorderLayout());

    east.setLayout(new GridLayout(2,1));
    west.setLayout(new GridLayout(6,1));

    //JList
    terNames = new JList(terProp);
    terNames.setVisibleRowCount(3);
    terNames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    west.add(new JScrollPane(terNames));

    terNames.addListSelectionListener(new ListSelectionListener(){
      public void valueChanged(ListSelectionEvent event){
        getContentPane().setBackground(terColor[terNames.getSelectedIndex()]);
      }
    }
                                      );

    //First set the buttons. 
    //These will include the options to surrender:
    //Trade Cards
    //Draw Cards
    //Roll your attack dice
    //Roll your defense dice

    //JButtons
    surrender = new JButton("Surrender");
    draw = new JButton("Draw");
    attackRoll = new JButton("Attack");
    defendRoll = new JButton("Defend");
    trade = new JButton("Trade");
    main = new JButton("Main Menu");
    quit = new JButton("Quit");
    east.add(quit);
    east.add(main);
    west.add(surrender);
    west.add(draw);
    west.add(attackRoll);
    west.add(defendRoll);
    west.add(trade);

    ButtonHandler handler = new ButtonHandler();
    surrender.addActionListener(handler);
    draw.addActionListener(handler);
    attackRoll.addActionListener(handler);
    defendRoll.addActionListener(handler);
    trade.addActionListener(handler);
    main.addActionListener(handler);
    quit.addActionListener(handler);
    //End of JButtons
  }

  public static void main(String[]args){
    new GamePage();
  }
  //This is to handle the Buttons.
  private class ButtonHandler implements ActionListener{

    public void actionPerformed(ActionEvent event){

      if(event.getSource().equals(surrender)){
      }
      else if(event.getSource().equals(draw)){
      }
      else if(event.getSource().equals(attackRoll)){
      }
      else if(event.getSource().equals(defendRoll)){
      }
      else if(event.getSource().equals(trade)){
      }
      else if(event.getSource().equals(quit)){
        dispose();
      }
      else if(event.getSource().equals(main)){
        dispose();
        MainPage mainPage = new MainPage();
        mainPage.setDefaultCloseOperation(EXIT_ON_CLOSE);
        mainPage.setSize(900,600);
        mainPage.setVisible(true);
      }
    }
  }
}
i7uaboj4

i7uaboj41#

您可以创建两个jpanel,您可以根据需要在其中放置不同的按钮,然后在jframe上使用合适的布局(borderlayout、gridbaglayout,有几种可能),添加面板。

gev0vcfq

gev0vcfq2#

不要再创造一个 JFrame ,因为你们班已经是 JFrame . 或者直接使用示例 JFrame 不要伸出手来 JFrame 为了班级。请参见扩展jframe与在程序内部创建jframe
您正在尝试将按钮添加到示例 JFrame 以及 JScrollPane 去上课 JFrame 你需要选择其中一个并坚持下去。
使用 JPanel 或者 JComponent 作为一个容器而不是 Container 你可以嵌套容器( JPanels )使用不同的布局管理器来获得您想要的外观。如果您感到困惑,请参阅在容器中布局组件以获取帮助。有关如何使用嵌套 JPanels 使用不同的布局管理器。
“哦,还有一个 JList 这是其中一个按钮。”—说不通。
我看到你试图处理你的主要框架和打开一个新的。别那么做。而是使用 CardLayout . 布局将“分层”面板,并允许您在其中导航。了解如何使用cardlayout。这里还有一个简单的例子

相关问题