【问题标题】:Change GridLayout in Java from horizontal to vertical with event.使用事件将 Java 中的 GridLayout 从水平更改为垂直。
【发布时间】:2013-12-06 14:20:24
【问题描述】:

我有一个正在进行的项目。我试图在按下按钮时将标志布局从水平更改为垂直。我在if "Bulgaria" 语句下添加了一行代码来改变网格布局,但是它不起作用。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Flags extends JFrame implements ActionListener {
    Container pane;
    JPanel display,displayCenter,displayBottom,displayTop,buttonArea;
    JButton Flag1,Flag2,Flag3,Flag4,Flag5,Flag6;
    public Flags () {
        setSize(600, 500);
        setTitle("Flags");
        setLocation(150, 150);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pane = getContentPane();
        pane.setLayout(new BorderLayout());
        display = new JPanel(new GridLayout(3,0));
        displayTop=new JPanel();
        display.add(displayTop);
        displayCenter=new JPanel();
        display.add(displayCenter);
        displayBottom=new JPanel();
        display.add(displayBottom);
        pane.add(display, BorderLayout.CENTER);
        Flag1 = new JButton ("Argentina");
        Flag1.addActionListener(this);
        Flag2 = new JButton ("Armenia");
        Flag2.addActionListener(this);
        Flag3 = new JButton ("Austria");
        Flag3.addActionListener(this);
        Flag4 = new JButton ("Azerbaijan");
        Flag4.addActionListener(this);
        Flag5 = new JButton ("Bolivia");
        Flag5.addActionListener(this);
        Flag6 = new JButton ("Bulgaria");
        Flag6.addActionListener(this);
        buttonArea = new JPanel();
        buttonArea.setLayout(new FlowLayout());
        buttonArea.setBackground(Color.BLACK);
        buttonArea.add(Flag1);
        buttonArea.add(Flag2);
        buttonArea.add(Flag3);
        buttonArea.add(Flag4);
        buttonArea.add(Flag5);
        buttonArea.add(Flag6);
        pane.add(buttonArea, BorderLayout.SOUTH); 
    }

    public void actionPerformed (ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("Argentina")){
            displayTop.setBackground(new Color(155,242,242));
            displayCenter.setBackground(Color.WHITE);
            displayBottom.setBackground(new Color(155,242,242));
        }
        if (command.equals("Armenia")){
            displayTop.setBackground(Color.RED);
            displayCenter.setBackground(Color.BLUE);
            displayBottom.setBackground(new Color(232,152,5));
        }
        if (command.equals("Austria")){
            displayTop.setBackground(Color.RED);
            displayCenter.setBackground(Color.WHITE);
            displayBottom.setBackground(Color.RED);
        }
        if (command.equals("Azerbaijan")){
            displayTop.setBackground(Color.BLUE);
            displayCenter.setBackground(new Color(232,84,5));
            displayBottom.setBackground(new Color(18,179,18));
        }
        if (command.equals("Bolivia")){
            displayTop.setBackground(Color.RED);
            displayCenter.setBackground(Color.YELLOW);
            displayBottom.setBackground(new Color(21,179,21));
        }
        if (command.equals("Bulgaria")){
            display = new JPanel(new GridLayout(0,3));
            displayTop.setBackground(Color.WHITE);
            displayCenter.setBackground(new Color(19,138,19));
            displayBottom.setBackground(Color.RED);
        }
    }

    public static void main (String [] args) {
        Flags demo = new Flags();
        demo.setVisible(true);
    }
}

【问题讨论】:

    标签: java swing user-interface actionlistener grid-layout


    【解决方案1】:

    解决办法:

    public void actionPerformed (ActionEvent e) {
        ...
        if (command.equals("Bulgaria")){
            displayTop.setBackground(Color.WHITE);
            displayCenter.setBackground(new Color(19,138,19));
            displayBottom.setBackground(Color.RED);
            display.setLayout(new GridLayout(1, 3));
        }
        else {
            display.setLayout(new GridLayout(3, 1));
        }
        display.doLayout();
    

    }

    解释:

    您实际上是在为display 变量设置一个新值,但这不起作用,因为它曾经引用的JPanel 已经添加到pane。此外,您正在设置displayTop、displayCenter和displayBottom的背景颜色,所有这些都已添加到display,因此您实际上修改了那些已经显示的JPanel。

    【讨论】:

      【解决方案2】:

      简答:你应该使用:

              display.setLayout(new GridLayout(0,3));
              display.revalidate();
      

      更长的答案。

      首先,在您的原始代码中,当您调用时:

      display = new JPanel(new GridLayout(0,3));
      

      您创建了一个新面板(没有子面板),它没有添加到框架中,所以基本上没有任何反应。您要做的是更改现有面板的布局(添加到框架并具有 3 个子项)。

      其次,您必须调用“revalidate”来告诉 swing 您希望它重做布局。

      第三,对于其他标志,您必须将布局设置回“3,0”大小写。我建议您重构代码以使其更简单、更具可读性。您可以使用一种方法来获取方向和 3 种颜色的标志。您还可以使用开关/案例(java 7):

      private void flag(boolean hor, Color c1, Color c2, Color c3) {
          displayTop.setBackground(c1);
          displayCenter.setBackground(c2);
          displayBottom.setBackground(c3);
          display.setLayout(new GridLayout(hor ? 0 : 3, hor ? 3 : 0));
          display.revalidate();
      }
      public void actionPerformed (ActionEvent e) {
          switch (e.getActionCommand()) {
              case "Argentina":
                  flag(false, new Color(155,242,242), Color.WHITE, new Color(155,242,242));
                  break;
              // ...
              case "Bulgaria":
                  flag(true, Color.WHITE, new Color(19,138,19), Color.RED);
              break;
          }
      } 
      

      【讨论】:

      • 好的,谢谢刚刚开始在 java 中使用 gui...这很有帮助。
      • 还有什么“?”意思是? display.setLayout(new GridLayout(hor ? 0 : 3, hor ? 3 : 0));
      • 那个?是一个内联 if。如果 hor 为真,“hor ? 0 : 3”的值为 0,如果 hor 为假,则值为 3。您不妨使用“if/else”并复制 setLayout 行。
      • 好的,谢谢,我还没有了解“?”,所以当你设置它时 (new GridLayout(hor ? 0 : 3, hor ? 3 : 0)) .... 你总是把先说真话?
      • 是的,请参阅alvinalexander.com/java/edu/pj/pj010018 了解更多示例和详细信息
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多