2017-05-18 6 views
0

フレームを作成しています。一部の製品の場所を示すJbuttonのリストを表示します。このリストをボタンにしたいのは、クリックすると他のフレームにリンクすることです。私はリスト全体を見ることができるスクロールバーを持つ方法を知りたいです。私は現在持っているコードは以下の通りです:複数のボタンを持つ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. 属性の可視性:例のようにしたのかどうかわかりませんが、属性は常に修飾子(public、package、privateまたはprotected)で修飾する必要があります
  3. あなたの質問には、あなたを助けようとしている人を助けてください。実際には、問題をコンパイルしています。あなたはそれにも注意を払うべきです。

あなたのコードでは、JLabel(Title)に関連付けられたviewPortでJScrollPaneを作成しようとしています。それはあまり意味がないのですか?私は、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

関連する問題