In short, such a thing. I began to learn Java through a video course : having come to lesson 167 , I encountered the problem of how to load elements into a component of a graphical interface. We write the keyboard simulator, (in the lessons I use NetBeans, I use Eclipse, the codes when creating the JFrame form are different, but so far I have managed it). We have three classes:
MainWindow
(no import specified)ExerciseGUI
(no import specified)Lessons
public class MainWindow extends JFrame { private ExerciseGUI exercise; private JPanel contentPane; private void exit() { int result = JOptionPane.showConfirmDialog (null, "Do you really want to exit?", "Information", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE ); if(result != JOptionPane.NO_OPTION) { dispose(); System.exit(0); } } private void centered() { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = this.getSize(); if(frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if(frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } this.setLocation((screenSize.width - frameSize.width) / 2 ,(screenSize.height - frameSize.height) / 2); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainWindow frame = new MainWindow(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public MainWindow() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { exit(); } }); this.setTitle("Название программы"); this.setSize(500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.centered(); exercise = new ExerciseGUI(); exercise.pack(); exercise.validate(); exercise.reveal(new Lessons("Lessons 1", "Please type" , "fjkd fkll jklh assdf")); exercise.setResizable(true); exercise.setVisible(true); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu file_name = new JMenu("File"); file_name.setMnemonic('F'); menuBar.add(file_name); JMenuItem file_exit = new JMenuItem("Exit"); file_exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { exit(); } }); file_name.add(file_exit); JMenu help_menu = new JMenu("Help"); help_menu.setMnemonic('H'); menuBar.add(help_menu); JMenuItem help_about = new JMenuItem("About"); help_about.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showConfirmDialog (null, "Under Construction...", "Разработка", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE); } }); help_menu.add(help_about); JMenuItem help_on_typist = new JMenuItem("Help"); help_on_typist.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JOptionPane.showConfirmDialog (null, "Under Construction...", "Думаем ещё", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE); } }); help_menu.add(help_on_typist); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JDesktopPane desktopPane = new JDesktopPane(); contentPane.add(desktopPane, BorderLayout.CENTER); desktopPane.add(exercise); } }
public class ExerciseGUI extends JInternalFrame { private Lessons lessons; public void reveal (Lessons lessons) { this.lessons = lessons; // выдает ошибку title_label cannot be resolved title_label.setText(lessons.get_title()); descr_label.setText(lessons.get_instructions());//cannot be resolved reference_text_area.setText(lessons.get_text());//cannot be resolved } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ExerciseGUI frame = new ExerciseGUI(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public ExerciseGUI() { setResizable(true); setBounds(100, 100, 450, 300); JPanel top_panel = new JPanel(); getContentPane().add(top_panel, BorderLayout.NORTH); JPanel toolbar = new JPanel(); top_panel.add(toolbar); JButton restart_button = new JButton("Restart"); toolbar.add(restart_button); JToggleButton pause_button = new JToggleButton("Pause"); toolbar.add(pause_button); JPanel label_panel = new JPanel(); top_panel.add(label_panel); JLabel title_label = new JLabel("title"); label_panel.add(title_label); JLabel descr_label = new JLabel(""); label_panel.add(descr_label); JPanel text_panel = new JPanel(); getContentPane().add(text_panel, BorderLayout.CENTER); text_panel.setLayout(new CardLayout(0, 0)); JSplitPane dril_pane = new JSplitPane(); dril_pane.setOrientation(JSplitPane.VERTICAL_SPLIT); text_panel.add(dril_pane, "name_11473649550263"); JTextArea reference_text_area = new JTextArea(); dril_pane.setLeftComponent(reference_text_area); JTextArea entry_text_area = new JTextArea(); dril_pane.setRightComponent(entry_text_area); JPanel status_panel = new JPanel(); getContentPane().add(status_panel, BorderLayout.SOUTH); } }
public class Lessons { private String title; private String instructions; private String text; public Lessons (String title, String instructions, String text) { this.title = title; this.instructions = instructions; this.text = text; } public String get_title() { if (title !=null) return title; else return "Title"; } public String get_instructions() { if (instructions !=null) return instructions; else return "Instructions"; } public String get_text() { if (text !=null) return text; else return "Text"; } }
Update
private Lessons lessons; private JLabel title_label; private JLabel descr_label; private JTextArea reference_text_area; public void reveal (Lessons lessons) { this.lessons = lessons; title_label.setText(lessons.get_title()); descr_label.setText(lessons.get_instructions()); reference_text_area.setText(lessons.get_text()); }
Here so added ...