我正在研究一种可以告诉财富的Java GUI。代码有效,但我有一些美学问题。我已经在这里和其他网站上查看了一些内容,但我似乎无法让我的滚动窗格出现。如果有人能指出我正确的方向让这些工作,将不胜感激!
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);
}
}
}
}
}
再次感谢!
答案 0 :(得分:4)
您正在将scrollPane(包含textArea)和 textArea添加到mainPn1中:
mainPnl.add(titlePnl, BorderLayout.NORTH);
mainPnl.add(scrollPane, BorderLayout.CENTER);
mainPnl.add(textArea, BorderLayout.CENTER);
您应该只添加scrollPane:
mainPnl.add(titlePnl, BorderLayout.NORTH);
mainPnl.add(scrollPane, BorderLayout.CENTER);
答案 1 :(得分:3)
mainPnl.add(scrollPane, BorderLayout.CENTER);
mainPnl.add(textArea, BorderLayout.CENTER);
使用textarea覆盖borderlayout的中心。 删除 mainPnl.add(textArea,BorderLayout.CENTER); ,它应该可以工作。
答案 2 :(得分:0)
这是创建文本区域的代码,使其成为滚动窗格的客户端,并将滚动窗格添加到容器中:
textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
setPreferredSize(new Dimension(450, 110));
add(scrollPane, BorderLayout.CENTER);