2017-05-18 37 views
0

我正在創建一個框架,它將顯示一個顯示某些產品位置的Jbuttons列表。我希望這個列表是按鈕的原因是如果它們被點擊,將它們鏈接到其他框架。我想知道一種可以通過整個列表的滾動條的方法。我公司目前擁有的代碼如下:如何在具有多個按鈕的jframe上應用滾動條

enter code here 
import javax.swing.*;//window settings 
import java.awt.*;//font settings 
import java.awt.event.*;//action settings 
public class locationFrame extends JFrame implements ActionListener 
{ 
    Font tFont=new Font("Arial",Font.BOLD,46); 
    Font bFont=new Font("Arial",Font.BOLD,52); 

    JLabel Title=new JLabel("Enid's Picks"); 
    JLabel label=new JLabel(""); 
    Image img=new 
    ImageIcon(this.getClass().getResource("/HPMLOGO.png")).getImage(); 
    JButton backButton=new JButton("Go Back"); 
    JButton AlemaniButton=new JButton("Alemania"); 
    JButton ArgentinaButton=new JButton("Argentina"); 
    JButton CaliforniaButton=new JButton("California"); 
    JButton ChileButton=new JButton("Chile"); 
    JButton EspanaButton=new JButton("Espana"); 
    JButton FranciaButton=new JButton("Francia"); 
    JButton ItaliaButton=new JButton("Italia"); 
    JButton NZButton=new JButton("New Zealand"); 
    JButton PortugalButton=new JButton("Portugal"); 
    JButton PRButton=new JButton("Puerto Rico"); 
    JButton USAButton=new JButton("USA"); 
    JScrollPane scroll=new 
    ScrollPane(Title,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
public locationFrame() 
{ 
    super("Wine Application"); 
    setLayout(null); 
    Title.setFont(tFont); 
    Title.setLocation(450,0); 
    Title.setSize(300,100); 
    label.setIcon(new ImageIcon(img)); 
    label.setBounds(350,100,500,500); 
    backButton.setFont(bFont); 
    backButton.setBounds(800,1800,400,100); 

    AlemaniButton.setFont(bFont); 
    ArgentinaButton.setFont(bFont); 
    CaliforniaButton.setFont(bFont); 
    ChileButton.setFont(bFont); 
    EspanaButton.setFont(bFont); 
    FranciaButton.setFont(bFont); 
    ItaliaButton.setFont(bFont); 
    NZButton.setFont(bFont); 
    PortugalButton.setFont(bFont); 
    PRButton.setFont(bFont); 
    USAButton.setFont(bFont); 

    AlemaniButton.setBounds(300,700,600,150); 
    ArgentinaButton.setBounds(300,1085,600,150); 
    CaliforniaButton.setBounds(300,1470,600,150); 
    ChileButton.setBounds(300,1855,600,150); 
    EspanaButton.setBounds(300,2240,600,150); 
    FranciaButton.setBounds(300,2625,600,150); 
    ItaliaButton.setBounds(300,3010,600,150); 
    NZButton.setBounds(300,3395,600,150); 
    PortugalButton.setBounds(300,3780,600,150); 
    PRButton.setBounds(300,4165,600,150); 
    USAButton.setBounds(300,4550,600,150); 

    setExtendedState(JFrame.MAXIMIZED_BOTH); 
    add(Title); 
    add(label); 
    add(backButton); 
    add(AlemaniButton); 
    add(ArgentinaButton); 
    add(CaliforniaButton); 
    add(ChileButton); 
    add(EspanaButton); 
    add(FranciaButton); 
    add(ItaliaButton); 
    add(NZButton); 
    add(PortugalButton); 
    add(PRButton); 
    add(USAButton); 

    add(scroll); 

    getContentPane().setBackground(new Color(255,255,255)); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    backButton.addActionListener(this); 
} 
@Override 
public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource()==backButton) 
    { 
    mainWineFrame mainFrame=new mainWineFrame(); 
    mainFrame.setVisible(true); 
    setVisible(false); 
    } 
} 

}

回答

0

您的代碼和問題都有很多缺陷。我會列出其中的一些,並希望我可以幫助您解決您的問題並改進您的代碼編寫。

  1. 代碼格式:您應該注意您使用的語言的代碼和命名約定。 Java採用camelCase,所以你不應該用大寫字母命名你的屬性。
  2. 屬性可見性:我不知道你是否只是作爲例子,但你的屬性應該總是通過修飾符(公共,封裝,私有或受保護)來限定
  3. 你的問題有很多代碼,真的幫助任何人試圖幫助你。實際上,它甚至有編譯問題。你也應該注意這一點。

現在爲了試圖幫助你,在你的代碼中,你試圖創建一個JSprollPane,它的viewPort綁定到JLabel(標題)。這沒有多大意義,是嗎?我認爲你應該仔細閱讀關於JScrollPane的文檔,並記住組件有一個ViewPort組件,簡單來說,當你與滾動條交互時,組件會發生變化。

我相信這至少可以給你如何建立你想要的方向。

JFrame frame = new JFrame(); 
    JPanel panel = new JPanel(); 
    for (int i = 0; i < 10; i++) { 
     panel.add(new JButton(""+i)); 
    } 

    JScrollPane scrollPane = new JScrollPane(panel); 
    frame.getContentPane().setLayout(new BorderLayout()); 
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER); 
    frame.setPreferredSize(new Dimension(100, 100)); 
    frame.pack(); 
    frame.setVisible(true); 

您可以在這裏找到參考資料:How to Use Scroll Panes

+0

感謝您的反饋意見。你是對的。我應該使用適當的代碼格式,如camalCase來簡化事情。代碼: // JScrollPane scroll = new JScrollPane(Title,JScrollPane.VERTICAL_SCROLLBAR_​​AS_NEEDED);和/ /添加(滾動);進行測試,看看自從我嘗試使用JScrollPane後會發生什麼。創建一個同時具有JScrollpane和所有按鈕的面板,然後將該面板添加到我的JFrame中會更容易嗎? –

+0

沒問題,很高興我能幫到你。不過,關於您的問題,是的,我相信如果將所有按鈕添加到JPanel並將該JPanel設置爲JScrollPane的視口,會更好。之後,您應該將JScrollPane添加到您的JFrame中,一切都會正常工作。我還在答案中列入了一些我認爲接近你想達到的結果。 – fwerther

相關問題