用于更改布局的Java GUI Action事件

时间:2016-01-25 21:35:13

标签: java swing

我正在尝试解决一个问题,我有一些按钮和底部面板“FlowLayout”,“BorderLayout”,“GridLayout”来切换顶部按钮的布局。到目前为止,我做到了这一点,但它没有用。需要一个解决方案。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

class Calculator extends JFrame implements ActionListener {

Container container;

JPanel p = new JPanel();
JPanel panelTop = new JPanel();
JButton b[] = new JButton[12];
JButton layoutButton[] = new JButton[3];
String sLayout[] = {"FlowLayout", "BorderLayout", "GridLayout"};
String s[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "11",    "12"};
String Screen = "";

public Calculator() {
    super("Calculator");
    container = getContentPane();
    for (int i = 0; i < sLayout.length; i++) {
        layoutButton[i] = new JButton(sLayout[i]);
        layoutButton[i].addActionListener(this);
        p.add(layoutButton[i]);
    }
    container.add(p, BorderLayout.SOUTH);

}//End Constructor

@Override
public void actionPerformed(ActionEvent event) {
    if (event.getSource() == layoutButton[2]) {
        panelTop.setLayout(new GridLayout(4, 4, 5, 5));
        for (int i = 0; i < s.length; i++) {
            b[i] = new JButton("Button " + s[i]);

            panelTop.add(b[i]);
        }

        container.add(panelTop, BorderLayout.NORTH);
    }
}

public static void main(String[] args) {
    Calculator calculator = new Calculator();
    calculator.setSize(500, 500);
    calculator.setVisible(true);
    calculator.setResizable(true);
    calculator.setLocationRelativeTo(null);
    calculator.setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
}//end class

如果我能让它按一个按钮工作,我相信我可以为其余部分做好准备。

2 个答案:

答案 0 :(得分:2)

在处理Swing布局时要记住的第一件事是,它们很懒惰。这一开始可能看起来很糟糕,但是当你开始添加/删除几十个组件时,你可以意识到在完成所有更新之前你实际上并不想要验证布局。

验证容器层次结构并不是一件容易的事情,因此越少做得越好。

基本上,一旦您对用户界面是最新的感到满意,就应该在更改的容器上调用public class sendSmsService extends IntentService { String senderNum = ""; String sms = ""; /** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public sendSmsService(String name) { super(name); } @Override protected void onHandleIntent(Intent intent) { try { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(senderNum, null, sms, null, null); Toast.makeText(getApplicationContext(), "Sms Sent", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Sms Failed", Toast.LENGTH_LONG).show(); } } ,然后调用revalidate。由于布局管理的工作方式,容器层次结构也将更新

  一个问题。如果我继续使用按钮,它会在顶部面板上重新添加我不想要的按钮。怎么解决?

你真的应该更多地引用JavaDocs,快速浏览JPanel docs会突出显示removeAll方法

答案 1 :(得分:-1)

感谢MadProgrammer,您的回答是具体和线性的。真的很喜欢它。这是完整的源代码。人们可能来这里复制粘贴:D

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Layout extends JFrame implements ActionListener {

Container container;
JPanel subPanel = new JPanel();
JPanel panelBottom = new JPanel();
FlowLayout flowLayout;

JPanel panelTop = new JPanel();
private static JButton b[] = new JButton[12];
JButton layoutButton[] = new JButton[3];
private static String sLayout[] = {"FlowLayout", "BorderLayout", "GridLayout"};
private static String s[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
String Screen = "";

public Layout() {
    super("Layout");
    container = getContentPane();

    panelTop.setLayout(new BorderLayout(3, 4));
    for (int i = 0; i < s.length; i++) {
        b[i] = new JButton("Button " + s[i]);

        subPanel.add(b[i]);
    }
    panelTop.add(subPanel);
    container.add(panelTop, BorderLayout.CENTER);
    for (int i = 0; i < sLayout.length; i++) {
        layoutButton[i] = new JButton(sLayout[i]);
        layoutButton[i].addActionListener(this);
        panelBottom.add(layoutButton[i]);
    }
    container.add(panelBottom, BorderLayout.SOUTH);

}//End Constructor

@Override
public void actionPerformed(ActionEvent event) {
    if (event.getSource() == layoutButton[2]) {//gridLayout
        panelTop.removeAll();
        panelTop.setLayout(new GridLayout(3, 4, 5, 15));
        for (int i = 0; i < s.length; i++) {
            b[i] = new JButton("Button " + s[i]);

            panelTop.add(b[i]);
        }

        container.add(panelTop, BorderLayout.NORTH);
        revalidate();
        repaint();
    }
    if (event.getSource() == layoutButton[0]) {//flowLayout
        panelTop.removeAll();
        subPanel.removeAll();
        flowLayout = new FlowLayout();
        panelTop.setLayout(flowLayout);

        for (int i = 0; i < s.length; i++) {
            b[i] = new JButton("Button " + s[i]);

            subPanel.add(b[i]);
        }
        flowLayout.setAlignment(FlowLayout.LEFT);
        flowLayout.layoutContainer(container);
        panelTop.add(subPanel);
        container.add(panelTop, BorderLayout.NORTH);
        revalidate();
        repaint();
    }
    if (event.getSource() == layoutButton[1]) {//BorderLayout
        panelTop.removeAll();
        subPanel.removeAll();
        panelTop.setLayout(new BorderLayout(3, 4));
        for (int i = 0; i < s.length; i++) {
            b[i] = new JButton("Button " + s[i]);

            subPanel.add(b[i]);
        }
        panelTop.add(subPanel);
        container.add(panelTop, BorderLayout.CENTER);

        revalidate();
        repaint();
      }
  }

}//end class

class LayoutTest {

    public static void main(String[] args) {
    Layout calculator = new Layout();
    calculator.setSize(900, 500);
    calculator.setVisible(true);
    calculator.setResizable(true);
    calculator.setLocationRelativeTo(null);
    calculator.setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
 }