1

我有一个小程序已经启动了多个菜单项。

单击一个项目时,我希望在框架上显示一些内容,当单击另一个项目时,第一个项目消失,第二个出现。

我可以打印到控制台,但似乎无法打印到框架。

任何建议,将不胜感激:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class melco extends JFrame{


public static void main(String[] args){
    JFrame frame = new JFrame("Salesman Resources");
    frame.setVisible(true);
    frame.setSize(1000,800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menubar = new JMenuBar();
    frame.setJMenuBar(menubar);

    JMenu file = new JMenu("File");
    menubar.add(file);
    JMenuItem exit = new JMenuItem("Exit");
    file.add(exit);

    JMenu orders = new JMenu("Orders");
    menubar.add(orders);
    JMenuItem bookedorders = new JMenuItem("Booked Orders");
    orders.add(bookedorders);
    JMenuItem backorders = new JMenuItem("BackOrders");
    orders.add(backorders);

    JMenu customers = new JMenu("Customers");
    menubar.add(customers);
    JMenuItem customersales = new JMenuItem("Customer Sales");
    customers.add(customersales);
    JMenuItem customeritems = new JMenuItem("Customer Items");
    customers.add(customeritems);
    JMenuItem customerprices = new JMenuItem("Customer Prices");
    customers.add(customerprices);
    JMenuItem customerlistings = new JMenuItem("Customer Listings");
    customers.add(customerlistings);

    JMenu inv = new JMenu("INV");
    menubar.add(inv);
    JMenuItem surplusinv = new JMenuItem("Surplus Inv");
    inv.add(surplusinv);
    JMenuItem stockinv = new JMenuItem("Stock Inv");
    inv.add(stockinv);

    JMenu search = new JMenu("Searh");
    menubar.add(search);
    JMenuItem itemsearch = new JMenuItem("Item Search");
    search.add(itemsearch);
    JMenuItem customersearch = new JMenuItem("Customer Search");
    search.add(customersearch);

    JMenu menulostsales = new JMenu("Lost Sales");
    menubar.add(menulostsales);
    JMenuItem lostsales = new JMenuItem("Lost Sales");
    menulostsales.add(lostsales);


    JMenu menumarginadvisor = new JMenu("Margin Advisor");
    menubar.add(menumarginadvisor);
    JMenuItem marginadvisor = new JMenuItem("Margin Advisor");
    menumarginadvisor.add(marginadvisor);


    JMenu menumakeandhold = new JMenu("Make and Hold");
    menubar.add(menumakeandhold);
    JMenuItem makeandhold = new JMenuItem("Make and Hold");

    menumakeandhold.add(makeandhold);

    makeandhold.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Make and Hold is pressed");
                System.out.println("Hello World");                  
// This is Make and Hold Area

            }
        }
    );          

    class exitaction implements ActionListener {
        public void actionPerformed (ActionEvent e){
        System.exit(0);
        }
    }
        exit.addActionListener(new exitaction());


}

}   
4

6 回答 6

1

我可以打印到控制台,但似乎无法打印到框架。

你到底是什么意思?

如果要在 Frame 上设置标题,可以使用 setTitle:

frame.setTitle("a title");

否则,如果你想添加一些文本,你至少要向你的 JFrame 添加一些组件。看看JLabel

JLabel label = new JLabel();
frame.getContentPane().add(label);

当您需要更改文本时:

label.setText("some text");
于 2012-08-17T15:42:44.300 回答
1
  • 除了让它处理 GUI 之外,永远不要将 EDT(事件调度程序线程)用于其他事情。main() Java Gui 中的方法在将 GUI 的构造调度到 Event Dispatcher 线程后退出。所以它是处理它的 EDT。

例如:

public static void main(String[] args){

    EventQueue.invokeLater(new Runnable(){

         public void run(){

             myframe.setVisible(true);

           } 
      }
  }
  • 您需要类似的东西JLable来添加Title之类的东西。

  • 您可以添加一个JPanelJFrame然后添加一个JTextField

于 2012-08-17T15:46:41.023 回答
1

您无法打印到框架,因为框架没有打印方法。如果您想直接将文本添加到框架中,您应该绘制它。这需要paint通过以下技术覆盖方法

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class melco extends JFrame{

  String str;
  int x = 100, y = 100;

  public melco(String title){
    super(title);
  }

  void print(String s){
    str = s;
    repaint();
  }

  public void paint(Graphics g){
    super.paint(g);
    if (str != null)
      g.drawString(str, x, y);
  }
  public static void main(String[] args){
    final melco frame = new melco("Salesman Resources");
    frame.setVisible(true);
    frame.setSize(1000,800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menubar = new JMenuBar();
    frame.setJMenuBar(menubar);

    JMenu file = new JMenu("File");
    menubar.add(file);
    JMenuItem exit = new JMenuItem("Exit");
    file.add(exit);

    JMenu orders = new JMenu("Orders");
    menubar.add(orders);
    JMenuItem bookedorders = new JMenuItem("Booked Orders");
    orders.add(bookedorders);
    JMenuItem backorders = new JMenuItem("BackOrders");
    orders.add(backorders);

    JMenu customers = new JMenu("Customers");
    menubar.add(customers);
    JMenuItem customersales = new JMenuItem("Customer Sales");
    customers.add(customersales);
    JMenuItem customeritems = new JMenuItem("Customer Items");
    customers.add(customeritems);
    JMenuItem customerprices = new JMenuItem("Customer Prices");
    customers.add(customerprices);
    JMenuItem customerlistings = new JMenuItem("Customer Listings");
    customers.add(customerlistings);

    JMenu inv = new JMenu("INV");
    menubar.add(inv);
    JMenuItem surplusinv = new JMenuItem("Surplus Inv");
    inv.add(surplusinv);
    JMenuItem stockinv = new JMenuItem("Stock Inv");
    inv.add(stockinv);

    JMenu search = new JMenu("Searh");
    menubar.add(search);
    JMenuItem itemsearch = new JMenuItem("Item Search");
    search.add(itemsearch);
    JMenuItem customersearch = new JMenuItem("Customer Search");
    search.add(customersearch);

    JMenu menulostsales = new JMenu("Lost Sales");
    menubar.add(menulostsales);
    JMenuItem lostsales = new JMenuItem("Lost Sales");
    menulostsales.add(lostsales);


    JMenu menumarginadvisor = new JMenu("Margin Advisor");
    menubar.add(menumarginadvisor);
    JMenuItem marginadvisor = new JMenuItem("Margin Advisor");
    menumarginadvisor.add(marginadvisor);


    JMenu menumakeandhold = new JMenu("Make and Hold");
    menubar.add(menumakeandhold);
    JMenuItem makeandhold = new JMenuItem("Make and Hold");

    menumakeandhold.add(makeandhold);

    makeandhold.addActionListener(
      new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
          System.out.println("Make and Hold is pressed");
          frame.print("Make and Hold is pressed");
          System.out.println("Hello World");
          frame.print("Hello World");
// This is Make and Hold Area

        }
      }
    );

    class exitaction implements ActionListener {
      public void actionPerformed (ActionEvent e){
        System.exit(0);
      }
    }
    exit.addActionListener(new exitaction());


  }

}
于 2012-08-17T16:06:06.127 回答
0

你不打印到一个框架。也许您想添加一个 JTextArea 并将数据附加到它?

于 2012-08-17T15:41:26.750 回答
0

好的,所以你需要在框架上显示一些东西,比如JLabel??

frame.setLayout(new BorderLayout());
JLabel lblMessage = new JLabel(); // You'll probably need to declare this as final
frame.add(lblMessage);

然后在你的动作监听器中可以做类似的事情

public void actionPerformed (ActionEvent e){
    label.setText("This is a message from the menu item");
}
于 2012-08-17T15:42:21.030 回答
-1

为什么不在 JMenuItems 上添加 MouseListener?您可以将最近的“消息”引用保存为成员变量,并在另一个 MouseListener 被触发时将其从 JFrame 中删除。

于 2012-08-17T15:41:28.700 回答