如何将Java控制台程序转移到窗口应用程序中?

如何将Java控制台程序转移到窗口应用程序中?,java,swing,Java,Swing,我一直在寻找,但未能找到足够简单的术语让我理解的东西。我目前有一个程序在java控制台上运行,我通过文本提供选项。我现在想把它变成一个窗口程序,它使用按钮而不是文本选项 我找到了关于如何创建JFrame的信息,但是我在查找关于如何创建一个调用方法的按钮的信息以及如何更改文本以显示更新方面遇到了问题 任何链接或信息将不胜感激,我不知道去哪里,任何轻推都是一种帮助 转到“文件”菜单并将项目导出到可运行的JAR文件中 如果您有一个jar文件要在windows中启动转到下面的链接这里是如何在java中使

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

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


任何链接或信息将不胜感激,我不知道去哪里,任何轻推都是一种帮助

转到“文件”菜单并将项目导出到可运行的JAR文件中
如果您有一个jar文件要在windows中启动

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

这里是关于这个例子的所有细节


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

您可以在“publicstaticvoidmain(String[]args)”方法中放入类似以下代码的内容


“。在查找有关如何制作调用方法的按钮的信息时遇到问题,并且文本会更改以显示更新。”将问题分解为较小的部分。e、 g.1)在框架上添加一个面板。默认情况下,面板将使用允许添加多个组件而无需任何约束的流布局。2) 向面板添加按钮和文本字段。3) 使按钮对被激活做出反应。激活时只需将字符串转储到系统中即可。4) 使用。。。。要写入文本字段的按钮操作。投票以“太宽”结束。哦,好的。那么,添加一个面板,然后将按钮和文本添加到面板?我想那可能是我错过的部分。对吗?“添加一个面板,然后将按钮和文本添加到面板?我想这可能就是我缺少的部分。对吗?”嗯,还有其他方法可以做到。1) 您可以使用框架内容窗格的默认边框布局,并将按钮和文本字段添加到内容窗格的不同约束中。2) 您可以重置框架的内容窗格以使用流布局。3) .. - 这不仅仅是既定任务的一部分,而且有多种方法可以完成。但从你的要求来看,似乎你也会从……中受益。我看不出这与将命令行应用程序转换为窗口应用程序有什么关系。对于“如何制作一个可执行的Jar?”这似乎是一个不错的(不是很好的)答案,但那就是。。这不是问题。你还假设OP使用的是一个特定的IDE。我不知道你的想法,所以我只是把它放在一起,我只需要一件不同的东西。我需要一个按钮来显示无法输入的文本,而不是一个可以输入的文本框。我用什么做这个如果有帮助的话,它特别是一个倒计时。您可以使用JLabel(称为lblCountDown),当用户单击按钮时,您可以调用lblCountDown.setText(“我的号码”);或者您仍然可以使用文本框并禁用它txtCountDown.setEnabled(false);这样用户就无法输入它,但它看起来仍然与JLabel不同。您可以用与JLable相同的方式更改文本(txtCountDown.setText(“我的号码”);太好了!非常感谢您的所有评论,这给了我很多学习的机会!
    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);