gpt4 book ai didi

java - 我无法使文本文件出现在我的 JTextArea 中

转载 作者:太空宇宙 更新时间:2023-11-04 14:02:10 25 4
gpt4 key购买 nike

这是我第一次发帖提问。所以请保持友好。

我的 java 程序遇到问题,文本文件无法出现在我的 JTextArea 中,我正在使用 Eclipse Luna

这是我的程序:

public class Frame_Cashier extends JFrame {
String[] category = {"Breakfast","Drinks","Add-ons","Desserts","Happy Meal","Value Meal"};
JComboBox comboBox = new JComboBox(category);
private JPanel contentPane;
JTextArea txtrItemId;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame_Cashier frame = new Frame_Cashier();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Frame_Cashier(){

Container c = getContentPane();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 759, 469);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(new Color(178, 34, 34));
menuBar.setBounds(0, 0, 752, 21);
contentPane.add(menuBar);

JMenu mnFile = new JMenu("File");
mnFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
mnFile.setForeground(new Color(255, 255, 255));
mnFile.setBackground(new Color(255, 69, 0));
mnFile.setFont(new Font("MV Boli", Font.BOLD, 15));
JMenuItem transaction = new JMenuItem("Transaction");
transaction.setForeground(new Color(255, 255, 255));
transaction.setBackground(new Color(178, 34, 34));
transaction.setFont(new Font("MV Boli", Font.BOLD, 12));
JMenuItem logout = new JMenuItem("Logout");
logout.setForeground(new Color(255, 255, 255));
logout.setBackground(new Color(178, 34, 34));
logout.setFont(new Font("MV Boli", Font.BOLD, 12));
menuBar.add(mnFile);
mnFile.add(transaction);
mnFile.add(logout);

JLabel img_background = new JLabel("");
img_background.setBackground(new Color(255, 51, 0));

Image img = new ImageIcon(this.getClass().getResource("cashier_img.jpg")).getImage();

JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
textArea.setBackground(new Color(255, 255, 204));
textArea.setBounds(380, 32, 353, 280);
contentPane.add(textArea);

JTextArea txtrItemId = new JTextArea();
txtrItemId.setBackground(new Color(255, 255, 204));
txtrItemId.setToolTipText("Item ID / Item Name");
txtrItemId.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
txtrItemId.setBounds(20, 114, 119, 150);
txtrItemId.setEditable(false);
contentPane.add(txtrItemId);

comboBox.setSelectedIndex(1);
comboBox.setSelectedIndex(5);
comboBox.setBackground(new Color(255, 255, 204));
comboBox.setBounds(20, 61, 338, 21);
contentPane.add(comboBox);

JTextArea textArea_2 = new JTextArea();
textArea_2.setEditable(false);
textArea_2.setBackground(new Color(255, 255, 204));
textArea_2.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
textArea_2.setBounds(175, 114, 72, 150);
contentPane.add(textArea_2);

JTextArea textArea_3 = new JTextArea();
textArea_3.setEditable(false);
textArea_3.setBackground(new Color(255, 255, 204));
textArea_3.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
textArea_3.setBounds(273, 114, 72, 150);
contentPane.add(textArea_3);

JTextArea textArea_1 = new JTextArea();
textArea_1.setBackground(new Color(255, 255, 204));
textArea_1.setBorder(new LineBorder(new Color(0, 0, 0), 2));
textArea_1.setBounds(63, 290, 119, 22);
contentPane.add(textArea_1);

JTextArea txtrNoImage = new JTextArea();
txtrNoImage.setBackground(new Color(255, 255, 204));
txtrNoImage.setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
txtrNoImage.setText("No Image");
txtrNoImage.setBounds(202, 274, 144, 126);
contentPane.add(txtrNoImage);

JLabel lblPrice = new JLabel("Price");
lblPrice.setForeground(new Color(255, 204, 51));
lblPrice.setFont(new Font("MV Boli", Font.BOLD, 12));
lblPrice.setBounds(281, 89, 64, 14);
contentPane.add(lblPrice);

JLabel lblNewLabel_3 = new JLabel("Stock");
lblNewLabel_3.setForeground(new Color(255, 204, 51));
lblNewLabel_3.setFont(new Font("MV Boli", Font.BOLD, 12));
lblNewLabel_3.setBounds(186, 89, 48, 14);
contentPane.add(lblNewLabel_3);

JLabel lblNewLabel_2 = new JLabel("Item ID / Item Name");
lblNewLabel_2.setForeground(new Color(255, 204, 51));
lblNewLabel_2.setFont(new Font("MV Boli", Font.BOLD, 12));
lblNewLabel_2.setBounds(10, 89, 157, 14);
contentPane.add(lblNewLabel_2);

JLabel lblNewLabel_1 = new JLabel("Item I.D");
lblNewLabel_1.setForeground(new Color(255, 204, 0));
lblNewLabel_1.setFont(new Font("MV Boli", Font.BOLD, 12));
lblNewLabel_1.setBounds(0, 290, 64, 22);
contentPane.add(lblNewLabel_1);

JLabel lblNewLabel = new JLabel("Select Category");
lblNewLabel.setFont(new Font("MV Boli", Font.BOLD, 13));
lblNewLabel.setForeground(new Color(255, 204, 0));
lblNewLabel.setBounds(10, 32, 134, 31);
contentPane.add(lblNewLabel);

JButton btnNewButton_1 = new JButton("Void");
btnNewButton_1.setBackground(SystemColor.controlDkShadow);
btnNewButton_1.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
btnNewButton_1.setForeground(new Color(255, 215, 0));
btnNewButton_1.setFont(new Font("MV Boli", Font.BOLD, 13));
btnNewButton_1.setBounds(634, 340, 99, 31);
contentPane.add(btnNewButton_1);

JButton btnNewButton = new JButton("Pay");
btnNewButton.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
btnNewButton.setFont(new Font("MV Boli", Font.BOLD, 15));
btnNewButton.setForeground(new Color(255, 215, 0));
btnNewButton.setBackground(SystemColor.controlDkShadow);
btnNewButton.setBounds(380, 340, 99, 31);
contentPane.add(btnNewButton);

JButton btnNewButton_2 = new JButton("Edit");
btnNewButton_2.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
btnNewButton_2.setBackground(SystemColor.controlDkShadow);
btnNewButton_2.setFont(new Font("MV Boli", Font.BOLD, 13));
btnNewButton_2.setForeground(new Color(255, 215, 0));
btnNewButton_2.setBounds(513, 340, 89, 31);
contentPane.add(btnNewButton_2);

img_background.setIcon(new ImageIcon(img));
img_background.setAlignmentX(Component.CENTER_ALIGNMENT);
img_background.setBounds(0, 21, 752, 410);
contentPane.add(img_background);

combohandler1 ch1 = new combohandler1();
comboBox.addActionListener(ch1);
}

private class combohandler1 implements ActionListener {
public void actionPerformed(ActionEvent e){
String item1,item2,item3,item4,item5,item6;
if(comboBox.getSelectedItem().equals("Breakfast")){
try{
FileReader file = new FileReader("breakfast.txt");

BufferedReader br = new BufferedReader(file);

item1 = br.readLine();
item2 = br.readLine();
item3 = br.readLine();
item4 = br.readLine();
item5 = br.readLine();

System.out.println("test.txt");
txtrItemId.setText("\n"+item1+"\n"+item2+"\n"+item3+"\n"+item4+"\n"+item5+"\n");

br.close();
}catch(Exception x){
JOptionPane.showMessageDialog(null,"Unable to find");
}
}
}
}
}

最佳答案

您已声明文本区域两次,一次在构造函数中本地声明,一次全局声明

您将在 txtrItemId.setText();

处收到 nullPointerException

更改:JTextArea txtrItemId = new JTextArea();

至:txtrItemId = new JTextArea();

当您尝试在事件处理程序中访问 txtrItemId 时,您正在访问全局声明的变量,因为您已经初始化了构造函数中声明的变量,因此该变量(在构造函数中声明)的范围将仅限于该方法/构造函数。

关于java - 我无法使文本文件出现在我的 JTextArea 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29205110/

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