尝试添加背景图片后没有显示任何内容

时间:2015-02-26 20:18:24

标签: java swing user-interface jpeg

我正在制作Caesar Cipher程序,我正在尝试更改GUI。我已经尝试添加背景图片(来自下面用户提供的评论),但现在,没有任何显示(背景图像,文本框和按钮不显示,我只是得到一个空白窗口时我运行程序)。任何帮助表示赞赏!

 public CaesarGUI() throws IOException {
    setTitle("Caesar Cipher");
    setVisible(true);
    setDefaultCloseOperation(3);
    pack();
    setSize(1400, 990);

    BufferedImage bg = ImageIO.read(Menu.class.getResource("beach.jpg"));
    JLabel label = new JLabel(new ImageIcon(bg));
    setContentPane(label);

    Container content = getContentPane();

    //Changed rows to 0 so it would be filled up before recalculating layout; achieves the horizontal layout
    GridLayout layout = new GridLayout(0, 2, 100, 5);
    content.setLayout(layout);

    inputTA = new JTextArea("Put the word you want to encrypt or decrypt and press the button", 12, 40);
    inputTA.setLineWrap(true);
    inputTA.setWrapStyleWord(true);
    inputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    JScrollPane scroller = new JScrollPane(inputTA);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    content.add(scroller);

    outputTA = new JTextArea(12, 40);
    outputTA.setLineWrap(true);
    outputTA.setWrapStyleWord(true);
    outputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

    //Made output box uneditable so that it only displays output
    outputTA.setEditable(false);

    JScrollPane scroller2 = new JScrollPane(outputTA);
    scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    content.add(scroller2);

    JPanel box1 = new JPanel();
    box1.setLayout(new FlowLayout());
    JButton decryptButton = new JButton("Decrypt");
    JButton encryptButton = new JButton("Encrypt");
    decryptButton.addActionListener(this);
    encryptButton.addActionListener(this);
    box1.add(decryptButton);
    box1.add(encryptButton);
    box1.add(new JLabel("Shift Factor"));
    box1.add(this.shiftFactor = new JTextField(20));
    content.add(box1);

    setResizable(false);
    //pack();
}

0 个答案:

没有答案