2017-10-05 56 views
0

我有一個多重JFrames的程序。我只想在其中一個上畫線。這是一個簡單的hang子手程序,所以只有實際的遊戲頁面應該包含這些行。 (這些行將組成絞索部分,因此應該每次都立即繪製)。試圖基於我發現的其他一些類似的QA將畫家類添加到有問題的JFrame,但似乎沒有工作。使用JFrames和JPanels的繪圖行

package hangman; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import javax.swing.*; 
import javax.swing.JPanel; 
import javax.swing.BorderFactory; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 


class Painter extends JPanel{ 

    public Painter(){ 

    } 
    @Override 
    protected void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.drawString("This is a test.",50,50); 
     g.drawLine(20, 80, 20, 200); 

    } 
} 

public class Hangman{ 

    public static void main(String[] args) throws InterruptedException{ 
     hangman(); 
    } 

    // method: hangman() 
    // purpose: create windows that make the Hangman game 
    private static void hangman() { 

     //Timer 
     final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
     int interval = 1000; // 1000 ms 

     Calendar now = Calendar.getInstance(); 
     JLabel time = new JLabel(dateFormat.format(now.getTime())); 
     time.setBounds(450, -50, 200, 125); 

     Timer timer = new Timer(interval, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       Calendar now = Calendar.getInstance(); 
       time.setText(dateFormat.format(now.getTime())); 
      } 
     }); 

     timer.start(); 


     //Frames 
     JFrame titleFrame = new JFrame("Hangman"); 
     JFrame mainMenuFrame = new JFrame("Hangman"); 
     JFrame creditsFrame = new JFrame("Hangman"); 
     JFrame highScoreFrame = new JFrame("Hangman"); 
     JFrame playGame = new JFrame("Hangman"); 

     creditsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     titleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     mainMenuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     highScoreFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     playGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Labels 
     final JLabel titleText = new JLabel(); 
     final JLabel teamText = new JLabel(); 
     final JLabel jennaBarrett = new JLabel(); 
     final JLabel lennyYang = new JLabel(); 
     final JLabel rachelFrodsham = new JLabel(); 
     final JLabel titleCredit = new JLabel(); 
     final JLabel highScores = new JLabel(); 
     final JLabel hangmanTitle = new JLabel(); 

     ImageIcon pastaIcon = new ImageIcon("Images/pasta.png"); 
     JLabel pastaImage = new JLabel(pastaIcon); 

     titleText.setText("CS245 Quarter Project"); 
     titleCredit.setText("Credits"); 
     teamText.setText("By: Pasta Party"); 
     jennaBarrett.setText("Jenna Barret, 010805821"); 
     lennyYang.setText("Lenny Yang, 010265034"); 
     rachelFrodsham.setText("Rachel Frodsham, 009922783"); 
     highScores.setText("High Scores"); 
     hangmanTitle.setText("Hangman"); 

     titleText.setFont(new Font("Arial", Font.BOLD, 30)); 
     titleCredit.setFont(new Font("Arial", Font.BOLD, 20)); 
     highScores.setFont(new Font("Arial", Font.BOLD, 20)); 
     hangmanTitle.setFont(new Font("Serif", Font.BOLD, 40)); 

     //Buttons 
     JButton playButton = new JButton("Play"); 
     JButton highScoreButton = new JButton("High Score"); 
     JButton creditsButton = new JButton("Credits"); 
     JButton backButtonCF = new JButton("Back"); 
     JButton backButtonHS = new JButton("Back"); 

     //Positioning 
     titleText.setBounds(130,50,500,150); 
     teamText.setBounds(250,300,200,50); 
     teamText.setBounds(250,300,150,50); 
     playButton.setBounds(400,185,150,30); 
     highScoreButton.setBounds(400,235,150,30); 
     creditsButton.setBounds(400,285,150,30); 
     backButtonHS.setBounds(25,300,95,30); 
     backButtonCF.setBounds(25,300,95,30); 
     jennaBarrett.setBounds(250,125,200,100); 
     lennyYang.setBounds(250,150,200,100); 
     rachelFrodsham.setBounds(250,175,200,100); 
     pastaImage.setBounds(10, 0, 400, 400); 
     titleCredit.setBounds(250,75,100,100); 
     highScores.setBounds(250,10,300,150); 
     hangmanTitle.setBounds(25,-10,500,100); 

     //Listeners 
     new java.util.Timer().schedule(new java.util.TimerTask() { 
       @Override 
       public void run() { 
        titleFrame.setVisible(false); 
        mainMenuFrame.setVisible(true); 
       } 
      }, 
      4500 
     ); 

     highScoreButton.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       mainMenuFrame.setVisible(false); 
       highScoreFrame.setVisible(true); 
      } 
     }); 

     playButton.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       mainMenuFrame.setVisible(false); 
       playGame.setVisible(true); 
      } 
     }); 

     creditsButton.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       mainMenuFrame.setVisible(false); 
       creditsFrame.setVisible(true); 
      } 
     }); 

     backButtonCF.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       creditsFrame.setVisible(false); 
       mainMenuFrame.setVisible(true); 
      } 
     }); 

     backButtonHS.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       highScoreFrame.setVisible(false); 
       mainMenuFrame.setVisible(true); 
      } 
     });   

     //Title Frame 
     titleFrame.setSize(600,400); 
     titleFrame.add(titleText); 
     titleFrame.add(teamText); 
     titleFrame.setLayout(null); 
     titleFrame.setLocationRelativeTo(null); 
     titleFrame.setVisible(true); 


     //Main Frame 
     mainMenuFrame.setSize(600,400); 
     mainMenuFrame.add(playButton); 
     mainMenuFrame.add(highScoreButton); 
     mainMenuFrame.add(creditsButton);     
     mainMenuFrame.add(pastaImage);   
     mainMenuFrame.setLayout(null); 
     mainMenuFrame.setLocationRelativeTo(null); 

     //Play Frame 
     playGame.setSize(600,400); 
     playGame.add(hangmanTitle); 
     playGame.setLayout(null); 
     playGame.add(time); 
     playGame.setLocationRelativeTo(null); 

     Painter p = new Painter(); 
     p.setVisible(true); 
     playGame.add(p); 


     //Credits Frame 
     creditsFrame.setSize(600,400); 
     creditsFrame.add(titleCredit); 
     creditsFrame.add(jennaBarrett); 
     creditsFrame.add(lennyYang); 
     creditsFrame.add(rachelFrodsham); 
     creditsFrame.add(backButtonCF); 
     creditsFrame.setLayout(null); 
     creditsFrame.setLocationRelativeTo(null); 

     //High Scores Frame 
     highScoreFrame.setSize(600,400); 
     highScoreFrame.add(highScores); 
     highScoreFrame.add(backButtonHS); 
     highScoreFrame.setLayout(null); 
     highScoreFrame.setLocationRelativeTo(null); 

    } 
} 
// 
+0

'「我有複式JFrames程序」' - 強制性鏈接到一個關鍵的問題有關:[多JFrames,好/壞實踐中的運用?](http://stackoverflow.com/questions/9554636) –

回答

3

playGame.setLayout(null);是搞亂你,使用空佈局全面負責組件的完整的位置和大小時,因爲你不設置大小或者「界限」的畫家的JPanel。評論這一行看看我的意思。

但是也不應該應該您應該設置它的大小,因爲一般來說,不要使用null佈局,而是學會使用佈局管理器。雖然Swing的新手可能看起來像是創建複雜GUI的最簡單也是最好的方式,但更多Swing GUI的創建使用它們時會遇到更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。


「我有複式JFrames程序」

強制性鏈接到一個關鍵的問題有關:The Use of Multiple JFrames, Good/Bad Practice?。所以如果你不想完全惹惱你的應用程序的用戶,請在這裏使用CardLayout。


其他問題:

  • 沒有啓動您的Swing事件線程(EDT或 「事件分派線程」)
  • 使用java.util.Timer中,而不是使用javax對GUI。 swing.Timer。需要注意的是,後者是更多的Swing線程安全
+0

謝謝,註釋了null佈局工作。很高興這是一個簡單的改變。是的,多個JFrames和絕對定位實際上是由我的合作伙伴設置的,而不是我所做的,但是在這一點上我必須使用我的工作。 – JRHB