Reputation: 11
I'm working on a Java GUI that will tell fortunes. The code works but I have some aesthetics problems. I have looked at things on here and on other sites, but i cant seem to get my scroll pane to come up. If someone could point me in the right direction of getting these to work it would be greatly appreciated!
package FortuneTellerRunner;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* @author a3cal_000
*/
class FortuneTellerFrame extends JFrame
{
final private JPanel mainPnl, titlePnl, displayPnl, buttonPnl, imagePnl;
final private JButton quitBtn, rndBtn;
final private JLabel titleLbl;
final private JTextArea textArea;
public String[] fortune = new String [12];
int newIndex, oldIndex;
private static final int HEIGHT = 250;
private static final int WIDTH = 450;
public FortuneTellerFrame()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPnl = new JPanel();
mainPnl.setLayout(new BorderLayout());
imagePnl = new JPanel();
displayPnl = new JPanel();
buttonPnl = new JPanel();
titlePnl = new JPanel();
ImageIcon icon1 = new ImageIcon("FortuneTellerIcon.JPG");
JLabel iconLbl = new JLabel();
iconLbl.setIcon(icon1);
titleLbl = new JLabel("Fortune Teller!");
textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(300, 250));
// Create the layout of the title panel
titlePnl.setLayout(new GridLayout(2,1));
add(mainPnl);
// Set the label to the panel.
titlePnl.add(titleLbl);
titlePnl.add(iconLbl);
// add the panel to the main panel.
mainPnl.add(titlePnl, BorderLayout.NORTH);
mainPnl.add(scrollPane, BorderLayout.CENTER);
mainPnl.add(textArea, BorderLayout.CENTER);
// Create the "Get my fortune button.
rndBtn = new JButton("Get My Fortune!");
quitBtn = new JButton("Quit");
// Add the buttons to the buttonPnl in grid layout.
buttonPnl.add(rndBtn);
buttonPnl.add(quitBtn);
// Create the grid layout for the button panel.
buttonPnl.setLayout( new GridLayout(1, 2));
// Add the button panel to the grid layout, South.
mainPnl.add(buttonPnl, BorderLayout.SOUTH);
ActionListener listener = new RndButtonListener();
rndBtn.addActionListener(listener);
quitBtn.addActionListener(listener);
}
class RndButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
fortune[0] = "He who throws dirt is losing ground.";
fortune[1] = "You will find the love of your life in food.";
fortune[2] = "Do or do not, there is no try.";
fortune[3] = "Tomorrow is a better day to try anything of importance.";
fortune[4] = "Life's not about how hard you can hit, but how hard you can get hit and keep moving forward.";
fortune[5] = "You can't be late until you show up.";
fortune[6] = "If you think things can't get worse it's probably only because you lack sufficent imagination.";
fortune[7] = "If youre at the top it means you have further to fall.";
fortune[8] = "Even in last place, youre still in the race.";
fortune[9] = "The road to riches is paved on the failures of others.";
fortune[10] = "If you feel like your going no where, get off the treadmill.";
fortune[11] = "Thinking about going to the gym is just as good as going.";
Random rnd = new Random();
do
{
newIndex = (int) Math.round(Math.random() * (fortune.length - 1));
}
while(newIndex == oldIndex);
do
{
System.out.println(fortune[newIndex]);
textArea.append(fortune[newIndex] + "\n");
textArea.updateUI();
mainPnl.updateUI();
oldIndex = newIndex;
}
while(newIndex != oldIndex);
class QuitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
}
}
}
}
Thanks again!
Upvotes: 1
Views: 253
Reputation: 2148
Here's the code that creates the text area, makes it the scroll pane's client, and adds the scroll pane to a container:
textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
setPreferredSize(new Dimension(450, 110));
add(scrollPane, BorderLayout.CENTER);
Upvotes: 0
Reputation: 42176
You're adding both the scrollPane (which contains the textArea) and the textArea to your mainPn1:
mainPnl.add(titlePnl, BorderLayout.NORTH);
mainPnl.add(scrollPane, BorderLayout.CENTER);
mainPnl.add(textArea, BorderLayout.CENTER);
You should only add the scrollPane:
mainPnl.add(titlePnl, BorderLayout.NORTH);
mainPnl.add(scrollPane, BorderLayout.CENTER);
Upvotes: 4
Reputation: 111
mainPnl.add(scrollPane, BorderLayout.CENTER);
mainPnl.add(textArea, BorderLayout.CENTER);
You override the center of the borderlayout with the textarea. Remove the mainPnl.add(textArea, BorderLayout.CENTER); and it should work.
Upvotes: 3