开发者

Java GUI not displaying my file when opened

So I have a simple GUI that can only open text files and should just display them in a text area to be edited. I know my string contains the files contents since I can print it out, but when I try and add it to my text area, it does not show up. I was wondering if this was a problem of overlapping text areas but I can't seem to find the error.

The first part of my code just creates the GUI. The other part should open a file and fill the text area with it. Where exactly is the problem and how do I fix it? Any help would be appreciated.

Here is part of my code which deals with creating the frames and panels:

public class MenuView extends JFrame {
  private JPanel centerPanel;
  private JPanel bottomPanel;
  private JMenuBar menuBar;
  private JMenu fileMenu;
  private JMenuItem openItem;
  private JMenuItem closeItem;
  private JButton setButton;
  private JTextField text;
  private JTextArea label;
  private JMenuItem fileNew;

    public MenuView(){
            super();
            setSize(500, 400);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
            setTitle("Menu Demo");

        //The center panel that will contain text
        centerPanel = new JPanel();
        centerPanel.setLayout(new FlowLayout());
        label = new JTextArea(400,500);
        centerPanel.add(label);
        add(centerPanel, BorderLayout.CENTER);
        //The bottom panel with the text field and button
        bottomPanel = new JPanel();
        bottomPanel.setLayout(new GridLayout(1, 2));
        setButton = new JButton("Set Text");
        text = new JTextField();
        bottomPanel.add(setButton);
        bottomPanel.add(text);
        add(bottomPanel, BorderLayout.SOUTH);

        //Setting up the menu   
        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        fileNew = new JMenu("New");
        openItem = new JMenuItem("Open");
        closeItem = new JMenuItem("Exit");
        fileMenu.add(openItem);
        fileMenu.add(closeItem);
        fileMenu.add(fileNew);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);   

        setButton.addActionListener(new ButtonCommand(label, text));
        closeItem.addActionListener(new QuitMenuCommand());
        openItem.addActionListener(new OpenMenuCommand(label));

    }

    public static void main(String [] args){
        MenuView v = new MenuView();
        v.setVisible(true);
    }
}

Here is the code that deals with opening the files:

public class OpenMenuCommand implements ActionListener {

    private JTextArea theLabel;
    private JFileChooser fc;
    private String k = "";

    public OpenMenuCommand(JTextArea l){
        theLabel = l;
        theLabel.getParent();
        fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("Text file", "txt"));
    }

    public void actionPerformed(ActionEvent e) {
        StringBuffer text = new StringBuffer();

        int returnValue = fc.showOpenDialog(null);
        if(returnValue == fc.APPROVE_OPTION){
            theLabel.removeAll();
            File f = fc.getSelectedFile();
            try{
                BufferedReader inFile = new BufferedReader(new FileReader(f));
                String in = inFile.readLine();
                while(in != null){
                    k = k + in;
                    in = inFile.readLine();
                }
                S开发者_JAVA技巧ystem.out.println(k);  
                theLabel.setText(k);
                inFile.close();
                theLabel.setVisible(true);
            }catch(FileNotFoundException exc){
                //Should never trigger
            }catch(IOException exc){
                theLabel.setText("Error reading in file.");
            }
        }

    }
}


Your stuff is being added to the JTextArea but you're not seeing due the size of the JTextArea. It's actually a friggin' big JTextArea:

label = new JTextArea(400, 500);

And by adding the huge JTextArea to a FlowLayout-using JPanel much of it is off the screen. To see what I mean, add this to your actionPerformed method:

System.out.println(theLabel.getBounds());

You'll see that the width and height are tremendous, and what's more, the left side is a large negative number.

Solution: Make your JTextArea more reasonable size (see in the API what these numbers mean -- row and column, not points) and add it to a JScrollPane and then add that BorderLayout.CENTER to a BorderLayout using container.

For example, the small GUI SSCCE shows your JTextArea in a FlowLayout using JPanel on the left and my JTextArea inside of a JScrollPane on the right:

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

public class MenuViewSSCCE {
   private static final Dimension APP_SIZE = new Dimension(500, 400);

   private static void createAndShowUI() {
      JTextArea label = new JTextArea(400, 500); // friggin big!
      JTextArea label2 = new JTextArea(400, 500); // friggin big!
      label.setText("Look at how big this JTextArea is!");
      label2.setText("Look at how big this JTextArea is!");

      JPanel centerPanel = new JPanel();
      centerPanel.setPreferredSize(APP_SIZE);
      centerPanel.setLayout(new FlowLayout()); // this line is redundant 
      centerPanel.add(label);

      JScrollPane myScrollpane = new JScrollPane(label2);
      myScrollpane.setPreferredSize(APP_SIZE);

      JPanel gridPanel = new JPanel(new GridLayout(1, 0));
      gridPanel.add(centerPanel);
      gridPanel.add(myScrollpane);

      JFrame frame = new JFrame("Your code on left, mine on right");
      frame.getContentPane().add(gridPanel, BorderLayout.CENTER);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜