gpt4 book ai didi

java - 向 Java 添加标题和文本字段

转载 作者:行者123 更新时间:2023-11-30 04:42:11 25 4
gpt4 key购买 nike

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

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

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

如有任何建议,我们将不胜感激:

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


}

}

最佳答案

您无法打印到框架,因为框架没有打印方法。如果您想直接将文本添加到框架中,则应该绘制它。这需要通过以下技术重写 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());


}

}

关于java - 向 Java 添加标题和文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com