简体   繁体   English

您如何将GUI添加到此Java程序?

[英]How do you add GUI to this java program?

I know only basic stuff in java. 我只知道Java中的基本知识。 And I need to create a GUI for this type of program. 我需要为此类程序创建一个GUI。 It shows your credit card info. 它显示您的信用卡信息。 It has some other classes and makes use of the rmiregistry. 它还有其他一些类,并且使用了rmiregistry。 This works fine in console but I need to show it in a GUI. 这在控制台中工作正常,但我需要在GUI中显示它。 The first thing that promps here is to enter your name (java Shopper localhost my name ). 在这里提示的第一件事是输入您的名字(java Shopper localhost 我的名字 )。 Then it shows you your credit card info. 然后,它会向您显示您的信用卡信息。 Can anyone help me? 谁能帮我? Please and thank you 谢谢,麻烦您了

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 $100");
            account.makePurchase(100.0f, 1234);
            System.out.println("Available credit is now "+account.getCreditLine());
            System.out.println("Buying a new pair of shoes for $160");
            account.makePurchase(160.0f, 1234);
            System.out.println("Cardholder: Paying off $136 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);
    }

}

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示例,以了解其工作原理,然后为您的应用程序设计更完整的示例。

The GUI editor in NetBeans is actually not bad for getting started quickly creating a GUI for a small application. 实际上, NetBeans中的GUI编辑器对于快速入门为小型应用程序创建GUI来说并不坏。 Knowing only a little about creating GUIs (and then only AWT, not Swing) I made my first Swing application in about ten minutes. 我对创建GUI知之甚少(然后仅了解AWT,而不了解Swing),我在大约十分钟的时间内完成了我的第一个Swing应用程序。

Since you're new to Java I'm guessing you haven't chosen an IDE yet. 由于您是Java的新手,所以我猜您尚未选择IDE。 NetBeans is a good place to start. NetBeans是一个不错的起点。

Start by reading the Swing Tutorial . 首先阅读Swing教程 There are plenty of example programs to learn from. 有很多示例程序可供学习。 Then you can ask specific questions if you encounter problems. 然后,如果遇到问题,您可以提出具体问题。

I don't recommend you to use NetBeans GUI Builder, it generates a lot of unnecessary code. 我不建议您使用NetBeans GUI Builder,因为它会生成很多不必要的代码。 Here is some example that I wrote to help you start with Swing. 这是我写来帮助您从Swing开始的一些示例。 This is simple example of one JFrame creation with two JButtons and one JTextField. 这是创建带有两个JButton和一个JTextField的JFrame的简单示例。 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 您可能也对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(); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM