Java 如何使用View、LayoutManager为Android重新设计基于AWT的Swing应用程序

Java 如何使用View、LayoutManager为Android重新设计基于AWT的Swing应用程序,java,android,swing,view,Java,Android,Swing,View,我有一个使用Java Swing、AWT类编写的应用程序原型,用于执行常规操作,例如允许输入JTexfield和JButton,以允许从多个值中选择一个,然后将所选按钮值附加到JTexfield(以便构造一个数学方程)。作为Android的新手,我正在努力将JavaSwing中的资源映射到Android等价物。我想使用它基于xml的功能尽可能地定义UI,并通过编程控制行为。寻找关于如何在这两个框架之间进行翻译的建议 import javax.swing.*; import javax.swing

我有一个使用Java Swing、AWT类编写的应用程序原型,用于执行常规操作,例如允许输入JTexfield和JButton,以允许从多个值中选择一个,然后将所选按钮值附加到JTexfield(以便构造一个数学方程)。作为Android的新手,我正在努力将JavaSwing中的资源映射到Android等价物。我想使用它基于xml的功能尽可能地定义UI,并通过编程控制行为。寻找关于如何在这两个框架之间进行翻译的建议

import javax.swing.*;
import javax.swing.text.JTextComponent;

import java.awt.*;
import java.awt.event.*;

public class MathYr1GUI extends JFrame
{
    private static final String TITLE="Number Bonds to 10 ";
    private static final int WIDTH=450;
    private static final int HEIGHT=600;

    private Container content;
    private JLabel result;
    private JButton[] cells;
    private JButton exitButton;
    private JButton initButton;
    private JButton enterButton;
    private CellButtonHandler[] cellHandlers;
    private ExitButtonHandler exitHandler;
    private InitButtonHandler initHandler;
    private EnterButtonHandler enterHandler;

    private boolean noughts;
    private boolean gameOver;

    private JTextField answer;
    private double testnumber;
    private double twenty=20;
    public double ten=10;

    public MathYr1GUI()
    {
        //Necessary initialization code
        setTitle(TITLE);
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //Get content pane
        content=getContentPane();
        content.setBackground(Color.blue.darker());

        answer = new JTextField(10);



        //Set layout
        content.setLayout(new GridLayout(4,3));

        //Create cells and handlers
        cells=new JButton[9];
        cellHandlers=new CellButtonHandler[9];
        for(int i=0; i<9; i++)
        {
            char ch=(char)('0'+i+1);
            cells[i]=new JButton(""+ch);
            cellHandlers[i]=new CellButtonHandler();
            cells[i].addActionListener(cellHandlers[i]);
        }

        //Create init and exit buttons and handlers
        enterButton = new JButton("Submit");
        enterHandler = new EnterButtonHandler();
        enterButton.addActionListener(enterHandler);

        exitButton=new JButton("EXIT");
        exitHandler=new ExitButtonHandler();
        exitButton.addActionListener(exitHandler);
        initButton=new JButton("CLEAR");
        initHandler=new InitButtonHandler();
        initButton.addActionListener(initHandler);


        //Create result label
        result=new JLabel("Noughts", SwingConstants.CENTER);
        result.setForeground(Color.white);

        //Add elements to the grid content pane
        for(int i=0; i<9; i++)
        {
            content.add(cells[i]);
        }
        content.add(initButton);
        content.add(result);
        content.add(exitButton);
        content.add(answer, BorderLayout.SOUTH);
        content.add(enterButton);
        //Initialize
        init();
    }

    public void init()
    {
        //Initialize booleans
        noughts=true;
        gameOver=false;

        //Initialize text in buttons
        for(int i=0; i<9; i++)
        {
            char ch=(char)('0'+i+1);
            cells[i].setText(""+ch);
        }

        //Initialize result label
        result.setText("Noughts");

        setVisible(true);
        answer.setText("0");
    }



    public static void main(String[] args)
    {
        //Create TicTacToe object
        MathYr1GUI gui=new MathYr1GUI();

    }

    private class CellButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            //If game over, ignore
            if(gameOver)
            {
                return;
            }

            //Get button pressed
            JButton pressed=(JButton)(e.getSource());

            //Get text of button
            String text=pressed.getText();

            //If noughts or crosses, ignore
            if(text.equals("O") || text.equals("X"))
            {
                return;
            }

            //Add nought or cross
            if (testnumber + Double.parseDouble(text) ==ten)
            {
                //End of game
                gameOver=true;
                result.setText("WELL DONE!");

                //Display winner message
            }
            else
            {
                result.setText("OOPS! TRY AGAIN");

            }

        }
    }

    private class EnterButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e) {

            testnumber = Double.parseDouble(answer.getText());
            result.setText( (answer.getText().trim() + " + __ = 20").toString() ) ;

        }

    }

    private class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }


    private class InitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            init();
        }


    }
}
import javax.swing.*;
导入javax.swing.text.JTextComponent;
导入java.awt.*;
导入java.awt.event.*;
公共类MathYr1GUI扩展了JFrame
{
私有静态最终字符串TITLE=“编号为10”;
专用静态最终整数宽度=450;
专用静态最终内部高度=600;
私人容器内容;
私有JLabel结果;
私有JButton[]单元;
私有JButton exitButton;
私有JButton initButton;
私有JButton enterButton;
私有CellButtonHandler[]CellHandler;
私有ExitButtonHandler exitHandler;
私有InitButtonHandler initHandler;
私有EnterButtonHandler enterHandler;
私有布尔零;
私有布尔gameOver;
私有JTextField应答;
专用双测试号码;
私人双二十=20;
公共双十=10;
公共MathYr1GUI()
{
//必要的初始化代码
片名(片名);
设置尺寸(宽度、高度);
setDefaultCloseOperation(关闭时退出);
//获取内容窗格
content=getContentPane();
content.setBackground(Color.blue.darker());
答案=新的JTextField(10);
//集合布局
setLayout(新的GridLayout(4,3));
//创建单元格和处理程序
单元格=新的JButton[9];
cellHandlers=新的CellButtonHandler[9];

对于(int i=0;iAs),您已经了解到Swing框架和Android UI框架是两种不同的框架,我不相信您会发现任何可以为您实现自动转换的东西。相反,我建议您查看Android.com上的文档,例如“构建简单的用户界面”其中介绍了一些概念:以及描述如何使用基本小部件的“输入控件”子页面:谢谢,这些链接看起来非常适合我想在Android上启动的应用程序类型。