如何将Java控制台程序传输到窗口应用程序?

时间:2017-03-11 07:35:31

标签: java swing

我一直在寻找,但却找不到足够简单的条款供我理解。我目前有一个在java控制台上运行的程序,我通过文本提供选项。我现在想把它变成一个使用按钮而不是文本选项的窗口程序。

我找到了有关如何创建JFrame的信息,但是我在查找有关如何创建调用方法的按钮以及更改显示更新的文本方面遇到问题。

任何链接或信息都会非常感激,我不知道去哪里,任何推动都是一种帮助!

3 个答案:

答案 0 :(得分:1)

您可以将类似以下代码的内容放入“public static void main(String [] args)”方法中。

    JFrame frmMain = new JFrame(); // Create our JFrame
    // Set the layout for main frame. This controls how things get arranged on the screen
    frmMain.setLayout(new BorderLayout());

    // panels are what you put everything else on
    JPanel panel1 = new JPanel(new FlowLayout()); // another layout
    JPanel panel2 = new JPanel();
    BoxLayout box = new BoxLayout(panel2, BoxLayout.PAGE_AXIS); // another layout
    panel2.setLayout(box);

    // here are a couple of buttons
    JButton btnAdd = new JButton("Add");
    JButton btnRemove = new JButton("Remove");

    // here are a couple of textboxes. They accept typed in information
    JTextField txtFirstName = new JTextField();
    JTextField txtMiddleInitial = new JTextField();
    JTextField txtLastName = new JTextField();

    // add our buttons to panel1. It has a FlowLayout, so they will be centered left to right as we add them
    panel1.add(btnAdd); 
    panel1.add(btnRemove);

    // Create a panel to hold First Name
    JPanel pnlFirstName = new JPanel(new BorderLayout()); // also set its layout
    // here we add a JLabel.class they just display text, they don't allow input 
    pnlFirstName.add(new JLabel("first name"), BorderLayout.WEST);
    // here we put our text box onto the First name panel
    pnlFirstName.add(txtFirstName, BorderLayout.CENTER);

    // repeat for middle initial panel
    JPanel pnlMiddleInitial = new JPanel(new BorderLayout());
    pnlMiddleInitial.add(new JLabel("M.I."), BorderLayout.WEST);
    pnlMiddleInitial.add(txtMiddleInitial, BorderLayout.CENTER);

    // repeat for last name panel
    JPanel pnlLastName = new JPanel(new BorderLayout());
    pnlLastName.add(new JLabel("last name"), BorderLayout.WEST);
    pnlLastName.add(txtLastName, BorderLayout.CENTER);

    // put a 3 pixel border arounnd panel 2 to keep things away from the edge
    panel2.setBorder(new EmptyBorder(3, 3, 3, 3));
    // add all of our input panels to panel 2, according to BoxLayout (up above)
    panel2.add(pnlFirstName);
    panel2.add(pnlMiddleInitial);
    panel2.add(pnlLastName);

    // add panel1 and panel2 to the Frame. You have to add to the .getContentPane(), or you might mess things up.
    frmMain.getContentPane().add(panel1, BorderLayout.NORTH);
    frmMain.getContentPane().add(panel2, BorderLayout.CENTER);

    // This is how we tell the program what to do when the user presses the "Add" button.
    btnAdd.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txtFirstName.setText("My First Name");
        }
    }); 
    // This is how we tell the program what to do when the user presses the "Remove" button.
    btnRemove.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txtFirstName.setText("");
        }
    });

    // pack just makes everything take up it's proper space on the screen in as tight of a package as possible
    frmMain.pack();
    // if you don't set visible to true, you won't see your Frame
    frmMain.setVisible(true);
    // what to do when the user clicks the "X" to close or used "Close" from the context menu
    frmMain.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

答案 1 :(得分:0)

转到下面的链接这里是如何使用Frame在java中使用按钮的完整示例。

在这里,您可以获得有关示例的所有详细信息。

http://www.javatpoint.com/java-jbutton

在actionPerformed()方法中编写代码,您希望在按钮单击时执行此操作。

答案 2 :(得分:-1)

转到文件菜单并将项目导出到runnable JAR文件中 比你在Windows中启动一个jar文件

相关问题