java 你如何向这个java程序添加GUI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1557954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:04:24  来源:igfitidea点击:

How do you add GUI to this java program?

javauser-interfacermicredit-card

提问by svlada

I know only basic stuff in java. And I need to create a GUI for this type of program. It shows your credit card info. It has some other classes and makes use of the rmiregistry. This works fine in console but I need to show it in a GUI. The first thing that promps here is to enter your name (java Shopper localhost my name). Then it shows you your credit card info. Can anyone help me? Please and thank you

我只知道 Java 中的基本内容。我需要为这种类型的程序创建一个 GUI。它显示您的信用卡信息。它还有一些其他类并使用了 rmiregistry。这在控制台中工作正常,但我需要在 GUI 中显示它。这里提示的第一件事是输入您的姓名(java Shopper localhost my name)。然后它会显示您的信用卡信息。谁能帮我?谢谢,麻烦您了

import java.rmi.*;
import javax.swing.*;

public class Shopper {
    public static void main(String args[])
    {
        CreditManager cm = null;
        CreditCard account = null;

        if(args.length<2)
        {
            System.err.println("Usage:");
            System.err.println("java Shopper <server> <accountname>");
            System.exit(1);
        }
        try
        {
            String url = new String("//"+args[0]+"/cardManager");
            System.out.println("Shopper: lookup cardManager, url="+url);
            cm = (CreditManager) Naming.lookup(url);
        }catch(Exception e)
        {
            System.out.println("Error in getting Card Manager "+e);
            System.exit(1);
        }

        try
        {
            account = cm.findCreditAccount(args[1]);
            System.out.println("Found account for "+args[1]);
        }catch(Exception e)
        {
            System.out.println("Error in getting acocunt for "+args[1]);
            System.exit(1);
        }

        try
        {

             System.out.println("Available credit is "+account.getCreditLine());
            System.out.println("Changing pin number for account");
            account.setSignature(1234);
            System.out.println("Buying a new watch for 0");
            account.makePurchase(100.0f, 1234);
            System.out.println("Available credit is now "+account.getCreditLine());
            System.out.println("Buying a new pair of shoes for 0");
            account.makePurchase(160.0f, 1234);
            System.out.println("Cardholder: Paying off 6 of balance");
            account.payTowardsBalance(136.0f);
            System.out.println("Available credit is now "+account.getCreditLine());

        }catch(Exception e)
        {
            System.out.println("Transaction error for "+args[1]);
        }

        System.exit(0);
    }

}

回答by Vincent B.

First things first, have a quick look at Awt/Swing in the Javadoc

首先,快速浏览一下Javadoc 中的Awt/Swing

According to what you need to do, you can add a gui very quickly in a first time using a JFrame and some TextArea (The text area will be your "console output"), this is the quickest way to have something visual out of your console.

根据您的需要,您可以在第一次使用 JFrame 和一些 TextArea(文本区域将是您的“控制台输出”)非常快速地添加一个 gui,这是从您的安慰。

After maybe you'll use some input for the account name in a popup window (See PopupFactory).

之后,您可能会在弹出窗口中使用帐户名称的一些输入(请参阅 PopupFactory)。

You can in a first time have a quick look at the various gui sample on sun website to understand how it works before designing a more complete one for your application.

在为您的应用程序设计更完整的图形界面之前,您可以在第一时间快速浏览 sun 网站上的各种 gui 示例,以了解其工作原理。

回答by James Cronen

The GUI editor in NetBeansis actually not bad for getting started quickly creating a GUI for a small application. Knowing only a little about creating GUIs (and then only AWT, not Swing) I made my first Swing application in about ten minutes.

NetBeans 中的 GUI 编辑器对于快速开始为小型应用程序创建 GUI 来说实际上还不错。我只知道一点关于创建 GUI(然后只知道 AWT,而不是 Swing)我在大约 10 分钟内完成了我的第一个 Swing 应用程序。

Since you're new to Java I'm guessing you haven't chosen an IDE yet. NetBeans is a good place to start.

由于您是 Java 新手,我猜您还没有选择 IDE。NetBeans 是一个很好的起点。

回答by camickr

Start by reading the Swing Tutorial. There are plenty of example programs to learn from. Then you can ask specific questions if you encounter problems.

首先阅读Swing 教程。有很多示例程序可供学习。然后遇到问题可以提出具体问题。

回答by svlada

I don't recommend you to use NetBeans GUI Builder, it generates a lot of unnecessary code. Here is some example that I wrote to help you start with Swing. This is simple example of one JFrame creation with two JButtons and one JTextField. You may be also interested in MVC pattern, you can read more about that specific topic here (http://pclc.pace.edu/~bergin/mvc/mvcgui.html) Also if you want to show results maybe you should try with JTextPane control, but that's just my opinion

我不建议您使用 NetBeans GUI Builder,它会生成很多不必要的代码。这是我编写的一些示例,旨在帮助您开始使用 Swing。这是一个使用两个 JButton 和一个 JTextField 创建一个 JFrame 的简单示例。您可能也对 MVC 模式感兴趣,您可以在此处阅读有关该特定主题的更多信息(http://pclc.pace.edu/~bergin/mvc/mvcgui.html)此外,如果您想显示结果,也许您应该尝试使用JTextPane 控件,但这只是我的意见

public class MainWindowClient implements ActionListener {

    JFrame frame;
    JTextField jtxInput;

    JButton btnConnect;
    JButton btnDisconnect;

    public MainWindowClient() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                init();
            }
        }); 
    }

    public void init() {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } 
        catch (ClassNotFoundException e) {}
        catch (InstantiationException e) {} 
        catch (IllegalAccessException e) {}
        catch (UnsupportedLookAndFeelException e) {}

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setTitle("Client");
        frame.setSize(800, 600);

        final JPanel title = new JPanel(new FlowLayout(FlowLayout.LEFT));
        title.setBackground(new Color(255, 255, 255));
        final JLabel lblAppName = new JLabel("Client Application");
        lblAppName.setFont(new Font("sansserif", Font.PLAIN, 22));
        title.add(lblAppName);
        title.setBorder(BorderFactory.createTitledBorder(""));

        final JPanel panelInputBoard = new JPanel(new GridLayout());
        panelLogArea.setBorder(BorderFactory.createTitledBorder("Input"));
        jtxInput = new JTextField("");

        panelLogArea.add(jtxInput);

        final JPanel panelCommandBoard = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panelCommandBoard.setBorder(BorderFactory.createTitledBorder("Client Commands"));

        btnConnect = new JButton("Connect");
        btnConnect.addActionListener(this);

        btnDisconnect = new JButton("Disconnect");
        btnDisconnect.addActionListener(this);

        panelCommandBoard.add(btnConnect);
        panelCommandBoard.add(btnDisconnect);

        frame.add(title, BorderLayout.NORTH);
        frame.add(panelCommandBoard, BorderLayout.SOUTH);
        frame.add(panelInputBoard, BorderLayout.NORTH);

        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        JButton eventSource = (JButton) event.getSource();
        if(eventSource.getText().equals("Connect")) {
            // Do some stuff
        }
        if(eventSource.getText().equals("Disconnect")) {
            // Do some stuff
        }       
    }


    public static void main(String[] args) {
        MainWindowClient m = new MainWindowClient(); 
    }
}