2014-02-05 53 views
1

我正在研究一個會告訴你命運的Java GUI。代碼有效,但我有一些美學問題。我看過這裏和其他網站上的東西,但我似乎無法讓我的滾動窗格出現。如果有人能指出我讓這些工作正確的方向,將不勝感激!在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

你最好提供一個最小的例子,當你遇到問題時,這裏沒有那麼多人,我猜想誰願意編譯和調試你的代碼。 – Predelnik

+0

謝謝,對不起,我不是故意要這樣下去的,新來的這裏還在學習禮儀。 –

回答

3
mainPnl.add(scrollPane, BorderLayout.CENTER); 
mainPnl.add(textArea, BorderLayout.CENTER); 

您使用textarea覆蓋borderlayout的中心。 刪除mainPnl.add(textArea,BorderLayout.CENTER);它應該工作。

4

要添加兩種滾動面板(包含TEXTAREA) textarea的你mainPn1:

mainPnl.add(titlePnl, BorderLayout.NORTH); 
mainPnl.add(scrollPane, BorderLayout.CENTER); 
mainPnl.add(textArea, BorderLayout.CENTER); 

只應添加滾動面板:

mainPnl.add(titlePnl, BorderLayout.NORTH); 
mainPnl.add(scrollPane, BorderLayout.CENTER); 
+0

謝謝!這工作完美,我明白我是如何搞砸。 –

0

下面的代碼創建文本區域,使其成爲滾動窗格的客戶端,並將滾動窗格添加到容器中:

textArea = new JTextArea(5, 30); 
JScrollPane scrollPane = new JScrollPane(textArea); 
setPreferredSize(new Dimension(450, 110)); 
add(scrollPane, BorderLayout.CENTER);