2016-07-25 88 views
1

我一直在做這個小瑣事遊戲作爲一個項目讓我很忙,但我無法弄清楚如何讓它看起來不錯。最好,按鈕將位於頂部,並且問題將以設置的窗口大小朝向底部。儘管setSize()或setPreferredSize()不起作用。所以窗口非常薄,按鈕和問題都在頂部。如何設置卡片佈局的位置和大小?

package mainPackage; 

import javax.swing.JFrame; 

public class MainGame{ 
    public static void main(String[] args) { 
     new WindowComp(); 

    } 
} 

    package mainPackage; 

import java.awt.CardLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

@SuppressWarnings("serial") 
public class WindowComp extends JComponent implements ActionListener { 

    JFrame frame = new JFrame("trivia game"); 




    static JButton [] buttons; 
    static JLabel question; 
    JPanel panelCont = new JPanel(); 
    JPanel panelStartScreen= new JPanel(); 
    JPanel panelGame = new JPanel(); 
    JButton startGame = new JButton("Start Trivia"); 
    CardLayout cl = new CardLayout(); 




    public WindowComp(){ 
     question = new JLabel("default"); 
     buttons = new JButton[4]; 
     panelCont.setLayout(cl); 



     buttons[0] = new JButton("Answer 1 : " + "default"); 
     buttons[1] = new JButton("Answer 2 : " + "default"); 
     buttons[2] = new JButton("Answer 3 : " + "default"); 
     buttons[3] = new JButton("Answer 4 : " + "default"); 

     buttons[0].addActionListener(this); 
     buttons[1].addActionListener(this); 
     buttons[2].addActionListener(this); 
     buttons[3].addActionListener(this); 
     startGame.addActionListener(this); 
     addAll(); 
     panelCont.add(panelStartScreen, "1"); 
     panelCont.add(panelGame, "2"); 



     frame.add(panelCont); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.setSize(300, 300); 
     frame.pack(); 
     frame.setVisible(true); 



    } 


    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == buttons[0]){ 
      setQuestion("button 1"); 
      setAnswers("start", "start", "start", "start"); 
     } 
     if(e.getSource() == buttons[1]){ 
      setQuestion("button 2"); 
      setAnswers("final", "final", "final", "final"); 
     } 
     if(e.getSource() == buttons[2]){ 
      setQuestion("button 3"); 
     } 
     if(e.getSource() == buttons[3]){ 
      setQuestion("button 4"); 
     } 

     if(e.getSource()==startGame){ 
      cl.show(panelCont, "2"); 

     } 


    } 
    public void addAll(){ 

     panelGame.add(buttons[0]); 
     panelGame.add(buttons[1]); 
     panelGame.add(buttons[2]); 
     panelGame.add(buttons[3]); 
     panelGame.add(question); 
     panelStartScreen.add(startGame); 



    } 



    public static void setAnswers(String ans1, String ans2, String ans3,String ans4){ 
     buttons[0].setText("Answer 1 : " + ans1); 
     buttons[1].setText("Answer 2 : " + ans2); 
     buttons[2].setText("Answer 3 : " + ans3); 
     buttons[3].setText("Answer 4 : " + ans4); 

    } 

    public static void setQuestion(String q){ 
     question.setText("Question: " + q); 
    } 
} 
+1

我建議發佈你有什麼和你想要的圖。如果您還沒有 – copeg

回答

1

注意事項:

1.調用JFrame.pack()重新排列其組件並調整其大小JFrame中。

2.製作JPanel比較容易(我發現),調用JPanel.setPreferredSize(new Dimension(width,height)),將組件放在裏面,並將它添加到JFrame中。這樣,您仍然可以調用JFrame.pack(),並且JPanel保持其首選大小。 3.您可以刪除JFrame.pack(),調用JFrame.setPreferredSize(new Dimension(width,height))和/或JFrame.setResizable(false)。

4.您可以調用JFrame.setMinimumSize(新的Dimension(寬度,高度))以防止打包過緊。請注意,當您調用JFrame.pack()時,此方法不能保證指定的尺寸。在你的情況下,由於之後調用了JFrame.pack(),寬度會拉長一點。對於你的程序,我建議你使用JFrame.setLayout(新的BorderLayout()),並使用FlowLayout/GridLayout創建一個JPanel並添加你的按鈕。然後將JPanel放在BorderLayout.NORTH區域。然後創建另一個JPanel,使用您選擇的佈局將您的問題放入其中,並將其添加到JFrame中的BorderLayout.CENTER區域。注意到不影響組件可見性的最小大小後,配置JFrame.setMinimumSize(new Dimension(width,height))。

6.佈局管理器在調整窗口大小後,擺脫了與組件佈局有關的所有麻煩,但使用setLayout(null)是最佳選擇的罕見實例存在(請參見圖1)。如果你不關心用戶調整應用程序的大小,調用JFrame.setResizable(false)就足夠了。但是,這會導致與較低分辨率顯示器的兼容性問題。

enter image description here 圖1.以這種方式放置按鈕需要我放棄佈局管理器並根據調整窗口大小時更新的寬度和高度變量來配置這些按鈕的位置。

祝你好運!

+0

,建議查看[佈局管理器視覺指南](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)謝謝,這有助於一個。 –