如何将 GUI 添加到这个 java 程序中?

发布于 2024-08-07 18:30:49 字数 2119 浏览 5 评论 0原文

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

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);
    }

}

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

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);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微暖i 2024-08-14 18:30:49

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

根据您需要做的事情,您可以使用 JFrame 和一些 TextArea 在第一次快速添加 gui(文本区域将是您的“控制台输出”),这是获得视觉效果的最快方法你的控制台。

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

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

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

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.

After maybe you'll use some input for the account name in a popup window (See 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.

抹茶夏天i‖ 2024-08-14 18:30:49

NetBeans 中的 GUI 编辑器实际上对于快速开始为小型应用程序创建 GUI 来说还不错。由于只了解一点关于创建 GUI 的知识(而且只了解 AWT,而不了解 Swing),我在大约十分钟内制作了我的第一个 Swing 应用程序。

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

The GUI editor in NetBeans is 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.

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

夏尔 2024-08-14 18:30:49

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

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.

燃情 2024-08-14 18:30:49

我不建议您使用 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(); 
    }
}

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

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(); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文